-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.c
127 lines (104 loc) · 2.74 KB
/
scheduler.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
//Shivam Kundan
//ECE493 Spring '16
//Lab5
/*This program implements a unix process scheduler that creates
N processes and schedules them in a Round Robin way for time
quantum qt. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include "queue.c"
void terminate();
queue plist; //Holds pid's of forked processes
int qt; //Time quantum
pid_t pid;
char cwd[1024];
char path[1024];
int main(int argc, char const *argv[])
{
//Variable initialization
pid_t cpid[argc-2];
queueInit(&plist);
//Get current working dir
if (getcwd(cwd, sizeof(cwd)) == NULL) perror("getcwd() error");
//Parse args
if (argc>2)
{
qt = 1000 * atoi (&argv[1][0]); //Here you need the qt
}
else
{
printf ("Incorrect format!\nUse: %s qt prog1 [prog2]...[prog N}\n",argv[0]) ;
exit(-1);
}
//Create executable files
for (int x=2;x<argc;x++)
{
printf("Message from father: Creating program %s\n",argv[x]);
if (fork()==0) execlp("gcc","gcc","./p.c","-o",argv[x],"-w",NULL);
}
sleep(1);
//Signal handling
signal(SIGCHLD,terminate);
//Fork processes to execute and enqueue programs
for (int i=2;i<argc;i++)
{
//Next four lines are used to obtain the complete path to the executable
strcpy(path,"");
strcpy(path,cwd);
strcat(path,"/");
strcat(path,argv[i]);
if ((cpid[i]=fork())==0)
{
execlp(path,argv[i],NULL);
exit(0);
}
else
{
queueInsert(&plist, cpid[i]); //Parent process saves pid's to plist
}
}
printf("\nprocesses created:\n");
queuePrint(plist);
sleep(1);
printf("\nI am the scheduler and I will now begin scheduling my programs:\n");
sleep(1);
while(plist.first!=NULL)
{
//Signal process to continue
kill(plist.first->pid,SIGCONT);
//Wait for time quantum
usleep(qt);
//If process has not finished
if (pid==0)
{
kill(plist.first->pid,SIGUSR1); //Signal process to stop
usleep(1000);
queueRotate(&plist); //Dequeue and re-enqueue
}
//If process has finished
else
{
printf("A child is dead ");
queuePop(&plist);
}
}
wait(NULL);
printf("All of my children died so bye...\n");
exit(0);
}
void terminate()
/*Signal handler for SIGCHLD.*/
{
while (1)
{
pid = wait3 (NULL, WNOHANG,NULL );
if (pid<1) return;
}
}