-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbg.c
More file actions
58 lines (47 loc) · 1.28 KB
/
bg.c
File metadata and controls
58 lines (47 loc) · 1.28 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
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "config.h"
void add_bg(int pid, char *proc_name) {
int i = 0;
for(i=0; i<1023; ++i)
{
if(bg_procs[i]==-1)
{
bg_procs[i] = pid;
bg_procs_name[i] = proc_name;
return;
}
}
fprintf(stderr, "Error: Too many processes running in the background\nThe shell will exit now\n");
exit(0);
return;
}
void check_process() {
pid_t rpid;
int status;
while((rpid=waitpid(-1, &status, WNOHANG))>0)
{
int i;
for(i=0; i<1023; i++)
{
if(bg_procs[i]==rpid)
{
bg_procs[i] = -1;
fprintf(stderr, "%s ", bg_procs_name[i]);
bg_procs_name[i] = "Process";
if(WIFEXITED(status)) fprintf(stderr, "with pid %d exited normally\n", rpid);
if(WIFSIGNALED(status)) fprintf(stderr, "with pid %d exited because of signal\n", rpid);
if(WIFSTOPPED(status)) fprintf(stderr, "with pid %d was killed\n", rpid);
break;
}
}
}
return;
}