-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
210 lines (193 loc) · 4.25 KB
/
main.c
File metadata and controls
210 lines (193 loc) · 4.25 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
200
201
202
203
204
205
206
207
208
209
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
struct bg_info
{
char status;
char * arg;
pid_t pid;
struct bg_info * next;
};//to keep each job's information
int main ( void )
{
struct bg_info * head = NULL;
struct bg_info * current = NULL;
char cwd[1024] ="shell>";
for (;;)
{
//check if there are process terminated,
if(head != NULL){
pid_t pt = waitpid(0, NULL, WNOHANG);
//if the pid is greater than zero, that means the child process has been terminated
while(pt>0){
if (head->pid == pt){
printf("%s has terminated\n", head->arg);
head = head->next;
}
else {
struct bg_info * tmp = NULL;
struct bg_info * pre = NULL;
tmp = head;
while(tmp->pid != pt){
pre = tmp;
tmp = tmp->next;
}
printf("%s has terminated\n", tmp->arg);
pre->next = tmp->next;
}
pt = waitpid(0, NULL, WNOHANG);
}
}
char *cmd = readline (cwd); //read the command line
printf ("Got: [%s]\n", cmd);
pid_t p;
char** args=(char**)malloc(sizeof(char*)*15); //allocate the memory to a string array
args[0]= strtok(cmd, " "); //tokenize the command line and store in a string array
int i = 0;
while(args[i]!=NULL){
args[i+1]= strtok(NULL, " ");
i++;
}
if(args[0]==NULL){
continue;
}
char* s=(char*)malloc(1024); //a string to store the jobs
int j = 1;
while(args[j]!=NULL){
strcat(s, args[j]);
strcat(s, " ");
j++;
}
//if the command is "cd"
if (strcmp(args[0], "cd")==0){
if(args[1]==NULL||strcmp(args[1],"~")==0)
{
chdir(getenv("HOME"));
}
else{
chdir(args[1]);
}
getcwd(cwd,1024);
strcat(cwd, ">");
}
else if (strcmp(args[0], "bg")==0)
{
args++;
p = fork();
//create a child process to run the job
if(p == 0){
int rc;
rc = execvp(args[0], args);
}
else {
//store the information of processes in the list
if(head == NULL) {
struct bg_info * tmp=(struct bg_info*) malloc(sizeof(struct bg_info));;
tmp->status = 'R';
tmp->arg = s;
tmp->pid = p;
tmp->next = NULL;
head = tmp;
current = tmp;
}
//append the list
else {
struct bg_info * tmp=(struct bg_info*) malloc(sizeof(struct bg_info));;
tmp->status = 'R';
tmp->arg = s;
tmp->pid = p;
tmp->next = NULL;
current->next = tmp;
current = tmp;
}
}
}
//the part of background list, print all the information of the list
else if (strcmp(args[0], "bglist")==0)
{
int i = 0;
current = head;
while(current != NULL){
printf("%d[%c]: %s\n", i, current->status, current->arg);
current = current->next;
i++;
}
printf("Total Background jobs: %d\n", i);
}
//the part of killing background jobs
else if (strcmp(args[0], "bgkill")==0)
{
int x =atoi(args[1]);
struct bg_info * pre = NULL;
int i = 0;
current = head;
while(current != NULL){
if (x == i) {
kill(current->pid, SIGKILL);
break;
}
current = current->next;
i++;
}
}
//the part of restarting jobs
else if (strcmp(args[0], "start")==0)
{
int x =atoi(args[1]);
struct bg_info * pre = NULL;
int i = 0;
current = head;
while(current != NULL){
if (x == i) {
if(current->status == 'S') //check if the job is currently runnung
{
kill(current->pid, SIGCONT);
break;
}
else
{
printf("the job is currently running\n");
break;
}
i++;
}
}
current->status = 'R';
}
else if (strcmp(args[0], "stop")==0)
{
int x =atoi(args[1]);
struct bg_info * pre = NULL;
int i = 0;
current = head;
while(current != NULL){
if (x == i) {
if(current->status == 'R') ////check if the job is currently stopped
{
kill(current->pid, SIGSTOP);
break;
}
else{
printf("the job is currently stopped\n");
break;
}
i++;
}
}
current->status = 'S';
}
else {
p = fork();
if ( p == 0) {
int rc;
rc = execvp(args[0], args);
}
else {
waitpid(p, NULL, 0);
}
}
free(cmd);
}
}