-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbg_check.c
More file actions
55 lines (51 loc) · 1.59 KB
/
bg_check.c
File metadata and controls
55 lines (51 loc) · 1.59 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include "main.h"
// check whether the process is a background one
int check_background()
{
int background = 0;
for(int i=0;i<tokens_len;i++)
if (strcmp(tokens[i],"&") == 0)
{
background = 1;
tokens[i] = NULL;
tokens_len--;
}
return background;
}
//print for all background process which have ended smoothly
void print_background()
{
int s,pid;
while ( (pid = waitpid(-1, &s, WNOHANG)) > 0)
for(int i=0;i<p_len;i++)
{
if (PROC[i].pid == pid)
{
if (WIFEXITED(s))
{
if (strcmp(PROC[i].name,"remindme") == 0)
printf("Reminder: %s\n",PROC[i].statement);
printf("%s with pid: %d exited normally\n",PROC[i].name,PROC[i].pid);
PROC[i].stat = 2;
}
else
{
if (WIFSIGNALED(s))
printf("%s with pid: %d killed by signal\n",PROC[i].name,PROC[i].pid);
else if (WIFSTOPPED(s))
printf("%s with pid: %d stopped by signal\n",PROC[i].name,PROC[i].pid);
else
printf("%s with pid: %d ended due to unknown reasons\n",PROC[i].name,PROC[i].pid);
PROC[i].stat = 1;
}
break;
}
}
return ;
}