-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.c
87 lines (71 loc) · 1.66 KB
/
exec.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
#include "header.h"
int fl;
void exec(char ** arg,int bg_flag)
{
globalProcessFlag=0;
int i,background=bg_flag;
int red=0,piping=0;
red=checkRedirect(arg);
piping=checkPiping(arg);
if(piping && !red)
{
handlePipes(arg);
return;
}
else if(piping && red)
{
handlePipesandRedirection(arg);
return;
}
else if(red && !piping)
{
redirect(arg);
return;
}
pid_t pid, wpid;
int state;
pid = fork();
int flag=0;
if (pid < 0) {
perror("ERROR");
flag=1;
}
else if(pid==0)
{
int x = execvp(arg[0], arg);
if(x < 0)
{
perror("ERROR");
flag=1;
}
exit(0);
}
else{
signal(SIGINT,SIG_IGN);
strcpy(globalProcessName,arg[0]);
if(background==-1 ){
signal(SIGCHLD, SIG_IGN);
signal(SIGINT,ctrl_c);
signal(SIGTSTP,handle);
globalPid = pid;
siginfo_t status;
waitid(P_PID,pid, &status, (WUNTRACED|WNOWAIT));
}
// printf("%d\n", pid);
else{
signal(SIGCHLD, exitprocess);
signal(SIGINT,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
generateoj();
printf("[%d] %d\n",oj_count+1,pid);
globalPid=0;
strcpy(process_names[pid].str,arg[0]);
process_names[pid].status = 1;
process_names[pid].init = time(NULL);
}
}
// printf("%s\n", process_names[pid].str);
signal(SIGINT,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
signal(SIGCHLD,SIG_DFL);
}