forked from burglarhobbit/contextswitching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplesimulation.cpp
123 lines (107 loc) · 1.61 KB
/
simplesimulation.cpp
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
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <process.h>
#include <stdlib.h>
#define timeslice 6
using namespace std;
int time=0;
struct PCB
{
int pid;
int beg;
int term;
PCB* nextready;
PCB* nextblocked;
} *newptr,*top,*begin,*end;
PCB* initPCB(int prid,int b,int t);
void push(PCB* ptr);
PCB* pop();
void display();
void process(PCB* ptr);
int main()
{
top=NULL;
for(int i=1; i<=10;++i)
{
newptr=initPCB(i,i*1000,(i*1000+i*1));
push(newptr);
}
while(begin!=NULL)
{
newptr=pop();
process(newptr);
}
return 0;
}
PCB* initPCB(int prid,int b,int t)
{
PCB* ptr = new PCB;
ptr->pid=prid;
ptr->beg=b;
ptr->term=t;
ptr->nextready=NULL;
ptr->nextblocked=NULL;
return ptr;
}
void push(PCB* ptr)
{
if(begin==NULL)
begin=end=ptr;
else
{
end->nextready=ptr;
end=ptr;
}
}
void display()
{
for(PCB* temp =begin; temp!=NULL; temp=temp->nextready)
{
cout<<temp->pid<<" "<<temp->beg<<endl;
}
}
PCB* pop()
{
if(begin!=NULL)
{
PCB* temp=begin;
begin=begin->nextready;
temp->nextready=NULL;
return temp;
}
}
void process(PCB* ptr)
{
int flag=0;
for(int i=0;i<timeslice;++i)
{
if(ptr->beg==ptr->term)
{
cout<<ptr->pid<<" "<<ptr->beg<<" "<<time<<endl;
flag=1;
break;
}
else
{
ptr->beg++;
}
time++;
}
if(flag==0)
{
cout<<ptr->pid<<" "<<ptr->beg<<" "<<time<<endl;
if(top==NULL)
top=ptr;
else if(flag==0)
{
push(ptr);
}
else
{
PCB* temp=top;
top=ptr;
ptr->nextblocked=temp;
}
}
}