-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsignalHandlers.c
60 lines (49 loc) · 1.54 KB
/
signalHandlers.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
#include "main.h"
// For checking if a process ended or not.
void sigchildHandler() {
pid_t pid;
int status;
pid = waitpid(-1, &status, WNOHANG);
// Check if its a valid process
if (pid < 0) {
printf(stderr,"Invalid process ID\n");
return;
}
else {
for(ll i = 0; i < totalNoOfJobs; i++){
if((ll)pid == myJobs[i].pid) {
// check if its a background process.
if (myJobs[i].jobsStatus != -1) {
myJobs[i].jobsStatus = -1;
printf("%s with pid %lld exited normally with exit status %d\n", myJobs[i].jobsNames, myJobs[i].pid, status);
break;
}
}
}
}
return;
}
// This is the signal controller for CTRL+C and CTRL+Z
void signalControl(int signal)
{
if(fgJob.pid > 0)
{
myJobs[totalNoOfJobs].pid = fgJob.pid;
strcpy(myJobs[totalNoOfJobs].jobsNames,fgJob.jobsNames);
myJobs[totalNoOfJobs].jobsIndex = totalNoOfJobs;
myJobs[totalNoOfJobs].jobsStatus = 1;
myJobsTemp[totalNoOfJobs].pid = fgJob.pid;
strcpy(myJobsTemp[totalNoOfJobs].jobsNames, fgJob.jobsNames);
myJobsTemp[totalNoOfJobs].jobsIndex = totalNoOfJobs;
myJobsTemp[totalNoOfJobs].jobsStatus = 1;
totalNoOfJobs++;
kill(fgJob.pid , signal);
fgJob.pid = -1;
printf("\n");
}
else
{
printf("\nNo foreground process found\n");
printf("Press [ENTER] to continue\n");
}
}