-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell_project.c
More file actions
200 lines (73 loc) · 3.26 KB
/
Shell_project.c
File metadata and controls
200 lines (73 loc) · 3.26 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "job_control.h" // remember to compile with module job_control.c
#define MAX_LINE 256 /* 256 chars per line, per command, should be enough. */
job * job_list;
// -----------------------------------------------------------------------
// MAIN
// -----------------------------------------------------------------------
int main(void)
{
char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2]; /* command line (of 256) has max of 128 arguments */
// probably useful variables:
int pid_fork, pid_wait; /* pid for created and waited process */
int status; /* status returned by wait */
enum status status_res; /* status processed by analyze_status() */
int info; /* info processed by analyze_status() */
ignore_terminal_signals();//this way we make sure that our shell leaves the terminal to foreground tasks
job_list = new_list("job list");
while (1) /* Program terminates normally inside get_command() after ^D is typed*/
{
printf("COMMAND->");
fflush(stdout);
get_command(inputBuffer, MAX_LINE, args, &background); /* get next command */
if(args[0]==NULL) continue; // if empty command
if(strcmp(args[0],"cd")==0&&args[1]!=NULL){
chdir(args[1]);
continue;
}
// (1) fork a child process using fork()
pid_fork = fork();
if (pid_fork < 0){
printf("Error t fork\n");
exit(-1);//then the shell exits
}else if (pid_fork == 0) { //child
//(2) the child process will invoke execvp()
// we have to create the child in an independent group
pid_t mypid = getpid();
new_process_group(mypid);
if(!background){
set_terminal(mypid);// the procces is in foreground so it has to be the unique owner of the terminal
}
restore_terminal_signals();//we restore the defaoult beheabour
execvp(args[0], args);
printf("the command %s wasnt executed in a right way \n", args[0]);
exit(-1);//execvp only returns if an error ocurr
}else{//father
//(3) if background == 0, the parent will wait, otherwise continue
new_process_group(pid_fork);
if(!background){
set_terminal(pid_fork);
/* we make the father wait for the child in waitpid
also we add the option WUNTRACED
*/
pid_t wait_pid = waitpid(pid_fork, &status, WUNTRACED);//this way we take into account if the child proccess its suspended
set_terminal(getpid()); //the father retake the ownership of the terminal
int info;
enum status st = analyze_status(status, &info);
//(4) Shell shows a status message for processed command
printf("Foreground pid: %d, command: %s, %s, info: %d\n", pid_fork, args[0], status_strings[st], info);
fflush(stdout);
if (st == SUSPENDED){
job * aux = new_job(pid_fork, args[0],STOPPED);
add_job(job_list, aux);
}
}else{ // Background process
job * aux = new_job(pid_fork, args[0],BACKGROUND);
add_job(job_list, aux);
printf("Background job runnning...pid: %d, command: %s\n", pid_fork, args[0]);
fflush(stdout);
}
}
} // end whil
}