-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-shell.c
More file actions
358 lines (321 loc) · 9.46 KB
/
simple-shell.c
File metadata and controls
358 lines (321 loc) · 9.46 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define BUF_SIZE 1024
// structure where scheduler proccess information is stored
typedef struct schedInfo
{
int pid;
char name[1000];
int execTime;
int waitTime;
int priority;
} schedInfo;
// structure that stores the information of the child process
typedef struct execInfo
{
int pid;
time_t timeExecuted;
float duration;
int exitStatus;
} execInfo;
// shm buffer
typedef struct shmbuf
{
int isRunning;
sem_t sem_lock;
sem_t sem_check;
int ncpu;
int tslice;
int pid_cnt;
schedInfo pids[BUF_SIZE];
int completed_cnt;
schedInfo completed[BUF_SIZE];
int pids_completed_cnt;
int pids_completed[BUF_SIZE];
} shmbuf;
// an array for child process info along with a value that tracks the arrays length
int currentInfo = 0;
execInfo infoArray[1000];
// an array with history of commands along with a value that tracks the arrays length
int currentHistory = 0;
char historyArray[1000][1000];
//
int shm_fd;
struct shmbuf *shmPointer;
void shm_init(char *shmpath, int ncpu, int tslice)
{
// Following lecture slides, we use shm_open, ftruncate, mmap
// Create shared memory object
shm_unlink(shmpath);
shm_fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR, 0600);
if (shm_fd == -1)
{
printf("Failed to create shared memory object\n");
return;
}
// Allocate memory for the shared object
if (ftruncate(shm_fd, sizeof(struct shmbuf)) == -1)
{
printf("ftruncate failed\n");
return;
}
// Map shared memory
shmPointer = mmap(NULL, sizeof(*shmPointer), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (shmPointer == MAP_FAILED)
{
printf("Mapping memory failed\n");
return;
}
// Initialise semaphores
if (sem_init(&shmPointer->sem_lock, 1, 1) == -1 || sem_init(&shmPointer->sem_check, 1, 0) == -1)
{
printf("Semaphore init failed\n");
return;
}
// some info
shmPointer->ncpu = ncpu;
shmPointer->tslice = tslice;
shmPointer->isRunning=1;
printf("Waiting for scheduler\n");
sem_wait(&shmPointer->sem_check);
printf("Scheduler hooked\n");
}
// prints the process information in the infoArray
void printProcessInfo()
{
printf("\n\nCOMMANDS RAN: %d\n", currentInfo);
for (int i = 0; i < currentInfo; i++)
{
printf("\n");
printf("PID: %d\n", infoArray[i].pid);
struct tm *info = localtime(&infoArray[i].timeExecuted);
printf("TIME STARTED: %s", asctime(info));
printf("DURATION: %f\n", infoArray[i].duration);
printf("EXIT STATUS: %d\n", infoArray[i].exitStatus);
printf("\n");
}
printf("\n\nSCHEDULED COMMANDS RAN: %d\n", shmPointer->completed_cnt);
for (int i = 0; i < shmPointer->completed_cnt; i++)
{
printf("\n");
printf("Name: %s\n", shmPointer->completed[i].name);
printf("PID: %d\n", shmPointer->completed[i].pid);
printf("Execution Time: %d\n", shmPointer->completed[i].execTime);
printf("Wait Time: %d\n", shmPointer->completed[i].waitTime);
}
}
// prints the history of commands executed
void history()
{
for (int i = 0; i < currentHistory; i++)
{
printf("%s", historyArray[i]);
}
}
// creates child proccesses and runs them
void create_process_and_run(char **args, int argscount)
{
int isScheduled = 0;
// if process isScheduled
if (strcmp(args[0], "submit") == 0)
{
isScheduled = 1;
// remove "submit" from the args as it is not the executable itself
for (int i = 1; i < 1000; i++)
{
args[i - 1] = args[i];
}
}
// creates a child process
int status = fork();
if (status < 0)
{
printf("Something bad happened\n");
exit(1);
}
if (status == 0)
{
if (isScheduled)
{
int status2 = fork();
if (status2 < 0)
{
printf("Something bad happened\n");
}
else if (status2 == 0)
{
// shm is locked
sem_wait(&shmPointer->sem_lock);
schedInfo *process = (schedInfo*) malloc(sizeof(schedInfo*));
// pid of the current process is appended to the process table
process->pid = getpid();
process->execTime = 0;
process->waitTime = 0;
if (args[1] != NULL)
{
process->priority = atoi(args[1]);
} else {
process->priority = 1;
}
shmPointer->pids[shmPointer->pid_cnt] = *process;
shmPointer->pid_cnt++;
// shm is unlocked
sem_post(&shmPointer->sem_lock);
// stops the process
kill(getpid(), SIGSTOP);
int executionStatus = execvp(args[0], args);
if (executionStatus == -1)
{
printf("Invalid command\n");
exit(1);
}
}
else
{
int ret;
int pid = wait(&ret);
if (!WIFEXITED(ret))
{
printf("Abnormal termination of %d", pid);
}
sem_wait(&shmPointer->sem_lock);
shmPointer->pids_completed[shmPointer->pids_completed_cnt] = pid;
shmPointer->pids_completed_cnt++;
sem_post(&shmPointer->sem_lock);
}
}
// history command can be executed
else if (strcmp(args[0], "history") == 0)
{
history();
exit(0);
}
// any valid exec command can be executed
else
{
int executionStatus = execvp(args[0], args);
if (executionStatus == -1)
{
printf("Invalid command\n");
exit(1);
}
}
}
else
{
if (!isScheduled)
{
// variables required for processInfo
execInfo processInfo;
clock_t start_time;
clock_t end_time;
time_t timeExecuted;
// waiting and information that is required to calculate some execInfo
time(&timeExecuted);
start_time = clock();
int ret;
int pid = wait(&ret);
end_time = clock();
double duration = (double)(end_time - start_time) / CLOCKS_PER_SEC;
// checks if the command is safely executed
if (!WIFEXITED(ret))
{
printf("Abnormal termination of %d\n", pid);
return;
}
// saves all the execInfo
processInfo.pid = pid;
processInfo.timeExecuted = timeExecuted;
processInfo.duration = duration;
processInfo.exitStatus = WEXITSTATUS(ret);
// adds the processInfo to infoArray and updates its size
infoArray[currentInfo] = processInfo;
currentInfo++;
} else
{
}
}
}
void launch(char *userInput)
{
// saving history
if (currentHistory >= 1000)
{
// if history size is depleted it starts from the first history entry
printf("Maximum history size reached! Resetting from 0.\n");
currentHistory = 0;
}
// copies the userInput to historyArray
strcpy(historyArray[currentHistory], userInput);
currentHistory++;
// creates arguments by using tokenizer so that it can be passed into the execvp function
char *args[1000] = {NULL};
char *token = strtok(userInput, " \n\t\r\a\"");
int count = 0;
// loops through the input using the tokenizer until it is completely tokenized
while (token != NULL && count < 999)
{
if (strlen(token) > 0)
{
args[count] = token;
count++;
}
token = strtok(NULL, " \n\t\r\a\"");
}
// checks if args exists
if (args[0] != NULL)
{
// executes the user input
create_process_and_run(args, count);
}
else
{
// redudant print statment (just letting you know this wasn't an oversight)
printf("\n");
}
}
// Ctrl+C input is redirected to this function
void exit_program()
{
shmPointer->isRunning=0;
printProcessInfo();
shm_unlink("Queue");
munmap(shmPointer, sizeof(*shmPointer));
exit(0);
}
int main()
{
int ncpu;
int tslice;
printf("Enter number of CPUs: ");
scanf("%d", &ncpu);
printf("Enter tslice in milliseconds: ");
scanf("%d", &tslice);
tslice *= 1000;
char *path = "Queue";
shm_init(path, ncpu, tslice);
// reads the ctrl+C input and redirects it to exit_program
signal(SIGINT, exit_program);
// intialized userInput to be read
char userInput[1000];
do
{
// reads the input with a nice terminal interface
printf("> ");
fgets(userInput, sizeof(userInput), stdin);
// runs every command user has inputted
launch(userInput);
// previously i had added a new line but it doesn't make sense because bash never does it either
} while (true);
return 0;
}