-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemprocess.c
73 lines (68 loc) · 1.98 KB
/
systemprocess.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
70
71
72
73
#include "systemprocess.h"
void childHandler(int signum)
{
int status;
pid_t completedPid;
while ((completedPid = waitpid(-1, &status, WNOHANG)) > 0)
{
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL)
{
perror("Memory allocation error");
exit(EXIT_FAILURE);
}
newNode->pid = completedPid;
newNode->status = status;
strncpy(newNode->process_name, s, 100 - 1);
newNode->process_name[100 - 1] = '\0'; // Ensure null-termination
newNode->next = completedList;
completedList = newNode;
}
}
void systemproc(char *tokens[], int num_tokens, int proccessid)
{
char *command = strdup(tokens[0]);
char *argv[num_tokens + 1];
for (int i = 0; i < num_tokens; i++)
{
argv[i] = strdup(tokens[i]);
}
if (argv[num_tokens - 1][strlen(argv[num_tokens - 1]) - 1] == '\n')
argv[num_tokens - 1][strlen(argv[num_tokens - 1]) - 1] = '\0';
argv[num_tokens] = NULL;
double start_time, end_time;
start_time = time(NULL);
int rc = fork();
processList = addProcess(processList, rc, tokens, num_tokens);
if (!proccessid)
foregroundProcessPid = rc;
if (rc < 0)
perror("creating child process failed\n");
else if (rc == 0)
{
if (proccessid)
setpgid(0, 0);
if (execvp(command, argv) == -1)
{
check = 1;
fprintf(stderr, "invalid command\n");
}
}
else if (rc > 0)
{
if (!proccessid)
waitpid(rc, NULL, WUNTRACED);
else
printf("child with pid [%d] sent to background\n", rc);
strcpy(s, tokens[0]);
strcpy(command_pid[rc], tokens[0]);
signal(SIGCHLD, childHandler);
end_time = time(NULL);
int duration = (int)(end_time - start_time);
if (duration > 2.0 && !proccessid)
{
tim = duration;
strcpy(com, command);
}
}
}