-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.c
69 lines (62 loc) · 1.58 KB
/
process.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "headers.h"
extern int tt;
extern BG fore;
extern List cProc[100];
void Process(char *input, int t_out) // foreground processes
{
if (t_out == -1)
t_out = dup(STDOUT_FILENO);
if (strlen(input) == 0)
return;
int z = 0;
char *argv[10], str[N], toll[N];
strcpy(toll, input);
time_t begin, end; // compute time
char *temp = strtok(input, " ");
strcpy(str, temp);
while (temp != NULL)
{
argv[z++] = temp;
temp = strtok(NULL, " ");
}
argv[z] = NULL;
pid_t id = fork(); // create a new process
if (id == -1)
{
char buf[N];
sprintf(buf, BLUE);
write(t_out, buf, strlen(buf));
sprintf(buf, "could not create new process\n");
write(t_out, buf, strlen(buf));
sprintf(buf, WHITE);
write(t_out, buf, strlen(buf));
return;
}
begin = time(NULL);
if (id == 0) // child
{
int ret = execvp(str, argv);
if (ret == -1)
{
char buf[2 * N];
sprintf(buf, RED);
write(t_out, buf, strlen(buf));
sprintf(buf, "error: %s command not found\n", toll);
write(t_out, buf, strlen(buf));
sprintf(buf, WHITE);
write(t_out, buf, strlen(buf));
exit(0);
}
}
else // parent
{
strcpy(fore.pname, toll);
fore.id = id;
int status;
waitpid(id, &status, WUNTRACED | WCONTINUED);
end = time(NULL);
if (end > begin)
tt += end - begin;
fore.id = -1;
}
}