-
Notifications
You must be signed in to change notification settings - Fork 2
/
foreground.c
46 lines (43 loc) · 856 Bytes
/
foreground.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "headers.h"
void foreground(char st[][100], int k)
{
char *com[k + 1];
int status;
int i = 0;
for (i = 0; i < k; i++)
{
com[i] = st[i];
}
com[i] = NULL;
int pid = fork();
if (pid < 0)
{
printf("Error: Fork Failed\n");
return;
}
else if (pid == 0)
{
setpgid(0, 0);
if (execvp(com[0], com) == -1)
{
printf("no such command\n");
exit(EXIT_FAILURE);
}
}
else
{
int x;
childpid = pid;
char name[100];
strcpy(name, com[0]);
for (i = 1; i < (k - 1); i++)
{
strcat(name, " ");
strcat(name, st[i]);
}
fore.pid = pid;
strcpy(fore.name, name);
fore.is_back = 0;
waitpid(-1, NULL, WUNTRACED);
}
}