-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsched.cpp
More file actions
executable file
·102 lines (87 loc) · 2.3 KB
/
sched.cpp
File metadata and controls
executable file
·102 lines (87 loc) · 2.3 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
#include <bits/stdc++.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
using namespace std;
#define FROM_PROCESS 10 //Various #defines for msg queues
#define TO_PROCESS 20
#define PAGE_FAULT_HANDLED 1
#define TERMINATED 2
struct MQ2buf{
long mtype;
char mbuf[1];
};
struct MQ1buf{
long mtype;
int id;
};
int k;
int main(int argc , char * argv[])
{
if (argc<5)
{
cout<<"Invalid Number of Arguments\n";
exit(1);
}
int MQ1id,MQ2id,master_pid,length,curr_pid;
MQ1id=atoi(argv[1]); //get the id of the message queues MQ1 and MQ2
MQ2id=atoi(argv[2]);
k=atoi(argv[3]);
master_pid=atoi(argv[4]);
struct MQ1buf msg_to_process,msg_from_process;
struct MQ2buf msg_from_mmu;
int n_terminated=0;
while (1)
{
length=sizeof(struct MQ1buf)-sizeof(long);
if(msgrcv(MQ1id,&msg_from_process,length,FROM_PROCESS,0)==-1) //select the 1st process from the ready queue
{
perror("Error in receiving message");
exit(1);
}
curr_pid=msg_from_process.id;
msg_to_process.mtype=TO_PROCESS+curr_pid;
msg_to_process.id=curr_pid;
length=sizeof(struct MQ1buf)-sizeof(long);
if (msgsnd(MQ1id,&msg_to_process,length,0)==-1) //send message to the selected process to start execution
{
perror("Error in sending message");
exit(1);
}
length=sizeof(struct MQ2buf)-sizeof(long);
if(msgrcv(MQ2id,&msg_from_mmu,length,0,0)==-1) //receive message from MMU
{
perror("Error in receiving message");
exit(1);
}
if (msg_from_mmu.mtype==PAGE_FAULT_HANDLED)
{
//if message type if PAGE_FAULT_HANDLED, then add the current process to the end of ready queue
msg_from_process.mtype=FROM_PROCESS;
msg_from_process.id=curr_pid;
length=sizeof(struct MQ1buf)-sizeof(long);
if (msgsnd(MQ1id,&msg_from_process,length,0)==-1)
{
perror("Error in sending message");
exit(1);
}
}
else if(msg_from_mmu.mtype==TERMINATED)
{
n_terminated++; //increment the number of processes terminated
}
else
{
cout<<"Incorrect Message Received\n";
exit(1);
}
if (n_terminated==k) break; //if all processes have terminated, then break
}
kill(master_pid,SIGUSR1); //send signal to Master to terminate all the modules
pause(); //wait for signal from Master
cout<<"Terminating Scheduler\n";
exit(1);
}