forked from AhmedKhaled590/OS_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_generator.c
175 lines (165 loc) · 5.25 KB
/
process_generator.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
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
#include "headers.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
void clearResources(int);
int msgQ;
struct Process *processes;
int *shmIdTerm, shmidterm; //for the running process
int main(int argc, char *argv[])
{
signal(SIGINT, clearResources);
char *fileName;
int chosenAlgorithm = 5;
int processParam = 1;
// 1. Read the chosen scheduling algorithm and its parameters, if there are any from the argument list.
/* if (argc < 3)
{
perror("Invlaid number of arguments");
exit(-1);
}
else
{
strcpy(fileName, argv[1]);
chosenAlgorithm = atoi(argv[3]);
if (argc > 3)
processParam = atoi(argv[5]);
}*/
/* key_t sharedMemKey = ftok("Makefile", 65);
shmidterm = shmget(sharedMemKey, 4000, 0666 | IPC_CREAT); // crete shared
if (shmid == -1)
{
perror("Error in creating message queue");
return -1;
}
shmIdTerm = (int *)shmat(shmid, (void *)0, 0);
*shmIdTerm = false;*/
// 5. Create a data structure for processes and provide it with its parameters.
processes = (struct Process *)malloc(100 * sizeof(struct Process));
// 2. Read the input files.
FILE *processesInput;
processesInput = fopen("processes.txt", "r");
if (processesInput == NULL)
{
perror("Error While reading processes.txt file\n");
exit(-1);
}
int a, b, c, d;
//TODO: Change this method to skip first line
char q[10], w[10], e[10], r[10];
fscanf(processesInput, "%s %s %s %s", q, w, e, r);
int numOfProcesses = 0;
while (fscanf(processesInput, "%d %d %d %d", &a, &b, &c, &d) != -1)
{
processes[numOfProcesses].id = a;
processes[numOfProcesses].arrivalTime = b;
processes[numOfProcesses].runTime = c;
processes[numOfProcesses].remningTime = c;
processes[numOfProcesses].priority = d;
numOfProcesses++;
}
char numProcesses[500];
sprintf(numProcesses, "%d", numOfProcesses);
// 3. Initiate and create the scheduler and clock processes.
int clkProcess = fork();
if (clkProcess == -1)
{
perror("Error in initializing Clock Child\n");
}
else if (clkProcess == 0)
{
execl("clk.out", "clk", NULL);
//exit(0);
}
int schedulerProcess = fork();
if (schedulerProcess == -1)
{
perror("Error in initializing schedular Child\n");
}
//TODO Send procHeaders while executing
else if (schedulerProcess == 0)
{
printf("hello i will fork scheduler...\n");
system("gcc scheduler.c -o scheduler.out");
/* if (argc > 3)
execl("scheduler.out", "scheduler", argv[3], numProcesses, argv[5], NULL);
else
execl("scheduler.out", "scheduler", argv[3], numProcesses, NULL);*/
execl("scheduler.out", "scheduler", NULL);
}
FILE *f = fopen("key", "r");
key_t key_id = ftok("key", 'a');
msgQ = msgget(key_id, 0666 | IPC_CREAT);
if (msgQ == -1)
{
perror("Error in creating message queue");
return -1;
}
else
{
printf("Process Generator:msgQ created Successfully with id = %d\n", msgQ);
}
/* procHeaders.mtype = 1;
procHeaders.algorithm = chosenAlgorithm;
procHeaders.numOfProcesses = numOfProcesses;
procHeaders.processParameter = processParam;
int val = msgsnd(msgQ, &procHeaders, sizeof(procHeaders.algorithm) + sizeof(procHeaders.numOfProcesses) + sizeof(procHeaders.processParameter), !IPC_NOWAIT);
if (val == -1)
perror("Errror in send\n");*/
// 4. Use this function after creating the clock Process to initialize clock.
initClk();
int i = 0;
int time = -1;
while (i < numOfProcesses)
{
int temp = processes[i].arrivalTime;
processes[i].valid = false;
while (time == getClk())
;
time = getClk();
if (temp > getClk())
{
printf("%d\n", getClk());
int val = msgsnd(msgQ, &processes[i], sizeof(processes[i]), !IPC_NOWAIT);
if (val == -1)
printf("Errror in send Process#%d\n", i);
}
else
{
// 6. Send the information to the scheduler at the appropriate time.
while (temp == getClk())
{
processes[i].valid = true;
processes[i].sendTime = temp;
printf("Sending Process#%d\n", i);
int val = msgsnd(msgQ, &processes[i], sizeof(processes[i]), !IPC_NOWAIT);
if (val == -1)
printf("Errror in send Process#%d\n", i);
i++;
temp = processes[i].arrivalTime;
}
}
}
// TODO Generation Main Loop
// 7. Clear clock resources
while (1)
{
printf("Here");
Process temp;
temp.mtype = 1;
temp.valid = false;
int val = msgsnd(msgQ, &processes[i], sizeof(processes[i]), !IPC_NOWAIT);
while (time == getClk())
;
time = getClk();
}
destroyClk(false);
}
void clearResources(int signum)
{
//printf("Terminate msgQ from generator\n");
//msgctl(msgQ, IPC_RMID, (struct msqid_ds *)0);
destroyClk(true);
kill(getpid(), SIGKILL);
//signal(SIGINT, clearResources);
}