-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfore.c
More file actions
89 lines (87 loc) · 1.87 KB
/
fore.c
File metadata and controls
89 lines (87 loc) · 1.87 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
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
#include"fore.h"
// #define LIST_MAX 2000
char temp[MAX];
char* word;
pid_t pid;
void foreground(char *cmd)
{
char *S[LIST_MAX];
int status;
word=strtok(cmd," ");
strcpy(FORE.S,word);
int i=0;
while(word!=NULL){
S[i++]=word;
word=strtok(NULL," ");
}
S[i++]=NULL;
pid=fork();
FORE.PID=pid;
if(pid<0)
{
printf("Fork error!\n");
}
else if(pid>0)
{
// waitpid(pid,&status,0);
// do{
waitpid(pid,&status,WUNTRACED);
// }while(!WIFSIGNALED(status) && !WIFSTOPPED(status));
// wait (NULL);
printf("watover \n");
}
else
{ // child.
execvp(S[0],S);
printf("ERROR! Unable to run the command!\n");
exit(1);
}
}
void sigchld_handler(int sig){
int status;
pid=waitpid(-1,&status,WNOHANG);
if(pid>0){
printf("\n%s with pid %d exited\n",temp,pid);
}
}
void background(char * cmd){
// pid cmd flag.
char *S[LIST_MAX];
int status;
word=strtok(cmd," ");
strcpy(temp,word);
int i=0;
while(word!=NULL){
S[i++]=word;
word=strtok(NULL," ");
}
S[i++]=NULL;
pid=fork();
if(pid<0){
printf("Fork error!\n");
}
else if(pid>0){
// printf("exist");
Jobs[Job_pos].PID=pid;
strcpy(Jobs[Job_pos++].S,cmd);
;
}
else if(!pid && execvp(S[0],S)<0){
execvp(S[0],S);
printf("Error!Unable to run the command.\n");
exit(1);
}
else if(!pid){
// printf("DONE\n");
if(execvp(S[0],S)<0){
execvp(S[0],S);
printf("Error!Unable to run the command.\n");
exit(1);
}
// else{
// Jobs[Job_pos].PID=pid;
// strcpy(Jobs[Job_pos++].S,cmd);
// }
}
// printf("///%d///",Job_pos);
}