-
Notifications
You must be signed in to change notification settings - Fork 2
/
fg_bg.c
148 lines (116 loc) · 3.32 KB
/
fg_bg.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "def.h"
int zFlag1 = 0, savePid = 0;
void handleZ1(int signal)
{
zFlag1 = 1;
return;
}
void fg_bg(char curCommand[], int flag, int **proc_size, process proc[])
{
// printf("%s curCommand\n", curCommand);
char **store = (char**) malloc(sizeof(char*) * MAX_SIZE);
char saveName[MAX_SIZE];
strcpy(saveName, curCommand);
if(flag)
{
strcpy(proc[**proc_size].name, curCommand);
}
char *token = strtok(curCommand, " \t\n");
int cnt = 0;
while(token != NULL)
{
store[cnt] = token;
// printf("%s *\n", store[cnt]);
// printf("%s *** %d\n", token, cnt);
// store[cnt][strlen(token)] = '\0';
cnt++;
token = strtok(NULL, " \t");
// printf("%s token\n", token);
}
store[cnt++] = NULL;
// If '&' was present, then run in background
if(flag)
{
int pid = fork();
if(pid < 0)
{
perror(store[0]);
}
if(pid == 0)
{
// printf("*\n");
setpgid(0, 0);
// close(STDERR_FILENO); // So that processes like firefox does not print error after closing
if(execvp(store[0], store) == -1)
{
printf("%s: Command Not Found\n", store[0]);
// exit(0);
}
}
if(pid > 0)
{
setpgid(pid, pid);
proc[**proc_size].pid = pid;
strcpy(proc[**proc_size].name, saveName);
(**proc_size)++;
}
}
// Run in foreground
else
{
// printf("* %s\n", store[0]);
// int pid = 7;
int pid = fork();
if(pid < 0)
{
perror(store[0]);
}
else if(pid == 0)
{
setpgid(0, 0);
// printf("hello ***\n");
// fflush(stdout);
if(execvp(store[0], store) == -1)
{
// printf("******\n");
printf("%s: Command Not Found\n", store[0]);
exit(0);
}
}
else
{
savePid = pid;
// signal(SIGTSTP, handleZ1);
// int status;
// waitpid(pid, &status, WUNTRACED);
// if(zFlag1 == 1)
// {
// kill(savePid, SIGSTOP);
// proc[**proc_size].pid = savePid;
// strcpy(proc[**proc_size].name, saveName);
// (**proc_size)++;
// zFlag1 = 0;
// }
int shellPid = getpid();
int status;
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
tcsetpgrp(0, pid);
tcsetpgrp(1, pid);
// printf("**\n");
waitpid(savePid, &status, WUNTRACED);
// printf("**\n");
tcsetpgrp(0, getpgid(shellPid));
tcsetpgrp(1, getpgid(shellPid));
signal(SIGTTOU, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
if(WIFSTOPPED(status))
{
kill(savePid, SIGSTOP);
proc[**proc_size].pid = savePid;
strcpy(proc[**proc_size].name, saveName);
(**proc_size)++;
}
}
}
}