-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.cpp
More file actions
executable file
·143 lines (122 loc) · 3.62 KB
/
process.cpp
File metadata and controls
executable file
·143 lines (122 loc) · 3.62 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
#include <bits/stdc++.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/msg.h>
#include <signal.h>
#include <sys/shm.h>
using namespace std;
#define PROCESS_OVER -9
#define TO_SCHEDULER 10
#define FROM_SCHEDULER 20
#define TO_MMU 10
#define FROM_MMU 20
#define PAGE_FAULT -1
#define INVALID_PAGE_REFERENCE -2
vector<int> pages;
struct MQ3_sendbuf{ //Send msg to MMU via MQ3
long mtype;
int id;
int pageno;
};
struct MQ3_recvbuf{ //Receive msg from MMU via MQ3
long mtype;
int frameno;
};
struct MQ1buf{ //Send/recv msg to scheduler via MQ1
long mtype;
int id;
};
void conv_ref_pages(char *ref) //Get the page numbers to be accessed from the page reference string
{
char *token;
token=strtok(ref," ");
while(token!=NULL)
{
pages.push_back(atoi(token)); //Split by " "
token=strtok(NULL," ");
}
}
int main(int argc, char *argv[])
{
if (argc<5)
{
perror("Invalid Number of Arguments\n"); //Invalid number of args
exit(1);
}
int pid,MQ1id, MQ3id;
int i;
pid = atoi(argv[1]); //get various ids
MQ1id = atoi(argv[2]);
MQ3id = atoi(argv[3]);
conv_ref_pages(argv[4]);
cout<<"Process id = "<<pid<<endl; //Process id
struct MQ1buf msg_to_scheduler;
msg_to_scheduler.mtype=TO_SCHEDULER;
msg_to_scheduler.id=pid;
int length=sizeof(struct MQ1buf)-sizeof(long);
if (msgsnd(MQ1id,&msg_to_scheduler,length,0)==-1) //Send the id to scheduler via MQ1 i.e ready queue enqueue is done
{
perror("Error in sending message to scheduler");
exit(1);
}
struct MQ1buf msg_from_scheduler; //Receive msg from scheduler (just done as a standard to wake up process)
length=sizeof(struct MQ1buf)-sizeof(long);
if (msgrcv(MQ1id,&msg_from_scheduler,length,FROM_SCHEDULER+pid,0)==-1)
{
perror("Error in receiving message");
exit(1);
}
MQ3_sendbuf msg_to_mmu;
MQ3_recvbuf msg_from_mmu;
for(i=0;i<pages.size();)
{
cout<<"Sending request for Page "<<pages[i]<<".\n"; //Send request for a page to mmu
msg_to_mmu.mtype=TO_MMU;
msg_to_mmu.id=pid;
msg_to_mmu.pageno=pages[i];
length=sizeof(struct MQ3_sendbuf)-sizeof(long);
if(msgsnd(MQ3id,&msg_to_mmu,length,0)==-1)
{
perror("Error in sending message");
exit(1);
}
length=sizeof(struct MQ3_recvbuf)-sizeof(long);
if(msgrcv(MQ3id,&msg_from_mmu,length,FROM_MMU+pid,0)==-1) //Receive the msg from mmu
{
perror("Error in receiving message");
exit(1);
}
if(msg_from_mmu.frameno>=0) //If frame number is found
{
cout<<"MMU responded with frame number for process "<<pid<<": "<<msg_from_mmu.frameno<<endl;
i++;
}
else if(msg_from_mmu.frameno==PAGE_FAULT) //In case of page fault, now wait for scheduler to send a msg to process via MQ1
{
cout<<"Page Fault detected for process "<<pid<<endl;
length=sizeof(struct MQ1buf)-sizeof(long);
if (msgrcv(MQ1id,&msg_from_scheduler,length,FROM_SCHEDULER+pid,0)==-1)
{
perror("Error in receiving message");
exit(1);
}
}
else if (msg_from_mmu.frameno==INVALID_PAGE_REFERENCE) //Invalid page reference: terminate
{
cout<<"Invalid Page Reference for Process "<<pid<<". Terminating the Process...\n";
exit(1);
}
}
cout<<"Process "<<pid<<" completed successfully"<<endl; //Completion of process, send -9 to MMU
msg_to_mmu.pageno=PROCESS_OVER;
msg_to_mmu.id=pid;
msg_to_mmu.mtype=TO_MMU;
length=sizeof(struct MQ3_sendbuf)-sizeof(long);
if (msgsnd(MQ3id,&msg_to_mmu,length,0)==-1)
{
perror("Error in sending message");
exit(1);
}
return 0;
}