-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.c
68 lines (61 loc) · 1.92 KB
/
jobs.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
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<errno.h>
#include<string.h>
#include<dirent.h>
#include "main.h"
void sigchd_handler()
{
int status;
pid_t pid = waitpid(-1, &status, WNOHANG);
// if (!(int)WEXITSTATUS(status)) {
int i, flag=0;
for (i=0;i<job_number;i++) {
if (bg_proc[i].pid == (int) pid) {
bg_proc[i].pstatus = 0;
flag = 1;
break;
}
}
if (flag == 1) {
nof_bg_proc--;
int exit_status = WEXITSTATUS(status);
stpd_process[nof_bg_stpd_proc].pid = (int)pid;
stpd_process[nof_bg_stpd_proc].pname = bg_proc[i].pname;
stpd_process[nof_bg_stpd_proc].pstat = malloc(10*sizeof(char));
stpd_process[nof_bg_stpd_proc].pstat = "Stopped";
stpd_process[nof_bg_stpd_proc].pjob_num = bg_proc[i].pjob_num;
stpd_process[nof_bg_stpd_proc].pstatus = bg_proc[i].pstatus;
nof_bg_stpd_proc++;
if (exit_status == 0) fprintf(stderr, "[%d] Done normally\t\t%s\n",(int)pid, bg_proc[i].pname);
else if(exit_status == 1) fprintf(stderr, "[%d] Done with impermisable opertaion errors \t\t%s\n",(int)pid, bg_proc[i].pname);
else if (exit_status == 126) fprintf(stderr, "[%d] Done with permission problem \t\t%s\n",(int)pid, bg_proc[i].pname);
}
// }
}
void jobs() {
// for (i=0;i<nof_bg_proc;i++)
// {
// wt_pid = waitpid((pid_t)bg_proc[i].pid, bg, WUNTRACED);
// waitpid(bg_proc[i].pid, bg_proc[i].pstatus, WNOHANG);
// waitpid(bg_proc[i].pid, &val, WNOHANG);
// printf("%d, val -> %d\n",*bg_proc[i].pstatus, val );
// // if (WIFEXITED(*bg_proc[i].pstatus) || WIFSIGNALED(*bg_proc[i].pstatus)) {
// printf("came here\n");
// // if (WIFEXITED(*bg_proc[i].pstatus)) {
// if (WIFEXITED(val)) {
// printf("%d Done\n",bg_proc[i].pid);
// }
// printf("come\n");
// pid_t pid = wait(NULL);
// if (pid!=-1) {
// printf("%d Done\n",pid);
// }
signal(SIGCHLD, sigchd_handler);
// }
// }
}