-
Notifications
You must be signed in to change notification settings - Fork 1
/
fgbg.c
90 lines (82 loc) · 2.15 KB
/
fgbg.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "env.h"
#include "headers.h"
extern int numbgChilds;
int sttoi (char * input){
int output;
char * end;
output = strtol (input, & end, 0);
return output;
}
int fg(char **parsed){
int i=0;
while(parsed[i]) i++;
if(i!=2){
printf("Error Wrong number of args expected 2 got %d\n", i-1);
return -1;
}
int num = sttoi(parsed[1]);
int pid=getidwithNum(num);
if(num>numbgChilds || pid==-1){
printf("Process with job number %d doesn't exist\n", num);
return -1;
}
Node *curProcess=getNodewithid(pid);
if(pid!=-1){
deleteNodewithid(pid);
setpgid(pid, getpgid(0));
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(STDIN_FILENO, pid);
kill(pid, SIGCONT);
int status;
do{
waitpid(pid, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status) && !WIFSTOPPED(status));
tcsetpgrp(STDIN_FILENO, getpgrp());
//for Ctrl + Z
if (WIFSTOPPED(status))
{
if (pid != -1)
createListNode(curProcess->data);
}
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
return 0;
}
return -1;
}
int bg(char **parsed){
int i=0;
while(parsed[i]) i++;
if(i!=2){
printf("Error Wrong number of args expected 1 got %d\n", i-1);
return -1;
}
int num = sttoi(parsed[1]);
int pid=getidwithNum(num);
if(num>numbgChilds || pid==-1){
printf("Process with job number %d doesn't exist\n", num);
return -1;
}
Node *curProcess=getNodewithid(pid);
kill(pid, SIGCONT);
return 0;
}
int kjob(char **parsed){
int i=0;
while(parsed[i]) i++;
if(i!=3){
printf("Error Wrong number of args expected 3 got %d\n", i-1);
return -1;
}
int num = sttoi(parsed[1]);
int sig = sttoi(parsed[2]);
int pid=getidwithNum(num);
if(num>numbgChilds || pid==-1){
printf("Process with job number %d doesn't exist\n", num);
return -1;
}
if (kill(pid, sig) == -1) {
perror("Error: ");
}
}