Professional Documents
Culture Documents
ETCS 352
Serial No: 31
Enrolment No:06715002717
Class: CSE-2(B)
Aim:
Write a program to implement CPU scheduling for first come first serve.
Theory:
First in, first out (FIFO), also known as first come, first served (FCFS), is the
simplest scheduling algorithm. FIFO simply queues processes in the order that
they arrive in the ready queue. In this, the process that comes first will be
executed first and next process starts only after the previous gets fully executed.
● Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time
● Waiting Time (W.T): Time Difference between turn around time and burst
time.
Waiting Time = Turn Around Time – Burst Time
Program Code:
#include<bits/stdc++.h>
using namespace std;
//class to store each process details
class gObject{
public:
int start;
int id;
int end;
int wt;
int tat;
2
int bt;
int at;
};
int main(){
//input
int n,cur_time = 0;
cout<<"Enter no of process : ";
queue<gObject *>gantt;
vector<gObject *>list;
cin>>n;
for(int j =0 ; j < n;j++){
gObject *temp = new gObject();
cin>>temp->id>>temp->bt>>temp->at;
temp->start = cur_time;
temp->wt = cur_time - temp->at;
temp->tat = temp->wt + temp->bt;
cur_time += temp->bt;
temp->end = cur_time;
//Inset each process into queue on the basis of fcfs
gantt.push(temp);
list.push_back(temp);
}
cout<<"\nGantt Chart\n";
while(gantt.size() > 0){
cout<<gantt.front()->start<<" |P"<<gantt.front()->id<<"| ";
if(gantt.size() == 1){
cout<<gantt.front()->end;
}
gantt.pop();
}
float avg_wt = 0,avg_tat = 0;
3
cout<<"\nTable "<<endl;
cout<<"Process BT\t AT\t WT \t TAT"<<endl;
for(int j = 0; j < list.size();j++){
avg_wt += list[j]->wt;
avg_tat += list[j]->tat;
cout<<"P"<<list[j]->id<<" \t"<<list[j]->bt<<"\t"<<list[j]->at<<"\t"<<list[j]
- >wt<<"\t"<<list[j]->tat<<endl;
}
cout<<"\nAverage wating time : "<<avg_wt/n;
cout<<"\nAverage turn around time : "<<avg_tat/n;
return 0;
}
Output:
4
5
Experiment – 2
Aim:
a.
Theory:
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the
waiting process with the smallest execution time to execute next. SJN is a non-
preemptive algorithm.
Advantages:
Disadvantages:
Program Code:
#include<bits/stdc++.h>
class process{
6
public :
};
struct mH{
if(p1->at == p2->at){
return true;
};
struct minpq{
};
class gObject{
public:
int start;
int id;
int end;
7
};
int main(){
int n;
vector<process *>list;
vector<gObject *>gantt;
cin>>n;
cin>>temp->id>>temp->bt>>temp->at;
temp->bt_start = temp->bt;
temp->last_seen = temp->at;
list.push_back(temp);
sort(list.begin(),list.end(),mH());
int process_solved = 0;
pq = priority_queue<process*,vector<process*>,minpq>();
if(list[j]->bt != 0){
8
pq.push(list[j]);
pq.top()->bt = 0;
process_solved++;
temp->id = pq.top()->id;
temp->start = cur_time;
cur_time += pq.top()->bt_start;
temp->end = cur_time;
gantt.push_back(temp);
cout<<"Gantt Chart"<<endl;
cout<<gantt[j]->end;
cout<<"\nTable "<<endl;
9
for(int j = 0; j < list.size();j++){
avg_tat += tat;
avg_wt += list[j]->wt;
cout<<"P"<<list[j]->id<<" \t"<<list[j]->bt_start<<"\t"<<list[j]->at<<"\t"<<list[j]
->wt<<"\t"<<tat<<endl;
return 0;
Output:
10
b.
Theory:
SRTF, Which Stands for Shortest Remaining Time First is a scheduling algorithm
used in Operating Systems, which can also be called as the preemptive version of
the SJF scheduling algorithm. The process which has the least processing time
remaining is executed first. In SRTF, processor is allocated to the job closest to
completion.
In SRTF, the execution of the process can be stopped after certain amount of
time. At the arrival of every process, the short-term scheduler schedules the
process with the least remaining burst time among the list of available processes
and the running process.
Advantages:
SRTF algorithm makes the processing of the jobs faster than SJN
algorithm, given its overhead charges are not counted.
Disadvantages:
The context switch is done a lot more times in SRTF than in SJN, and
consumes CPU’s valuable time for processing. This adds up to its
processing time and diminishes its advantage of fast processing.
Program Code:
#include<bits/stdc++.h>
class process{
public :
11
int bt_start;
int last_seen;
};
struct mH{
if(p1->at == p2->at){
return true;
};
struct minpq{
};
class gObject{
public:
};
int main(){
int n;
12
vector<process *>list;
vector<gObject *>gantt;
cin>>n;
cin>>temp->id>>temp->bt>>temp->at;
temp->bt_start = temp->bt;
temp->last_seen = temp->at;
list.push_back(temp);
sort(list.begin(),list.end(),mH());
pq = priority_queue<process*,vector<process *>,minpq>();
i++;
cur_time = list[i]->at;
int time_alloted;
13
temp->end = cur_time;
temp->start = prev_time;
temp->id = prev_process->id;
prev_process->bt = 0;
}else{
prev_time = cur_time;
prev_process->last_seen = cur_time;
gantt.push_back(temp);
}else{
if(list[j]->bt != 0){
pq.push(list[j]);
14
prev_process = pq.top();
if(i == 0){
i++;
prev_time = cur_time;
continue;
i++;
prev_time = cur_time;
cur_time += pq.top()->bt;
temp->start = prev_time;
temp->id = pq.top()->id;
temp->end = cur_time;
pq.top()->last_seen = cur_time;
pq.top()->bt = 0;
gantt.push_back(temp);
}else{
15
gantt[gantt.size() - 1]->end = temp->end;
pq.pop();
cout<<"Gantt Chart"<<endl;
cout<<gantt[j]->end;
cout<<"\nTable "<<endl;
avg_wt += list[j]->wt;
avg_tat += tat;
cout<<"P"<<list[j]->id<<" \t"<<list[j]->bt_start<<
"\t"<<list[j]->at<<"\t"<<list[j]->wt<<"\t"<<tat<<endl;
16
return 0;
Output:
17
Experiment – 3
Aim:
Theory:
Example:
Process Details:
Gantt Chart:
18
Table:
Program:
import java.util.*;
class process{
int n = s.nextInt();
19
for(int i = 0; i < n;i++){
np.id = s.nextInt();
np.bt = s.nextInt();
np.at = s.nextInt();
np.pr = s.nextInt();
lop.add(np);
Collections.sort(lop,new Comparator<process>(){
});
int process_solved = 0;
int cur_time = 0;
(new Comparator<process>() {
});
20
for(int i = 0; i < n;i++){
pq.add(lop.get(i));
present_.start = cur_time;
present_.tat = present_.bt;
cur_time += present_.bt;
present_.bt = 0;
present_.end = cur_time;
present_.tat += present_.wt;
avg_tat += (float)(present_.tat/n);
avg_wt += (float)(present_.wt/n);
gt.add(present_);
process_solved++;
System.out.print("0");
System.out.println("Table : \nId\tAT\tPR\tBT\tTAT\tWT");
21
for(int i =0 ; i < n;i++){
System.out.println(gt.get(i).id+"\t"+gt.get(i).at+"\t"+gt.get(i).pr+
"\t"+gt.get(i).bt+"\t"+gt.get(i).tat+"\t"+gt.get(i).wt);
s.close();
Output:
22
Experiment – 4
Aim:
Theory:
Example:
Process Details:
Gantt Chart:
23
Table:
Program:
import java.util.*;
class process {
24
int totalP = scn.nextInt();
temp.pid = scn.nextInt();
temp.at = scn.nextInt();
temp.bt = scn.nextInt();
temp.total = temp.bt;
pp.add(temp);
int quantum = 3;
Collections.sort(pp,new Comparator<process>(){
});
25
System.out.print("Gantt Chart\n " + 0);
if(pp.get(i).at <= time && pp.get(i).at > ptime && pp.get(i).bt > 0){
status.addFirst(pp.get(i));
gtstart.add(time);
rem.bt -= quantum;
ptime = time;
time += quantum;
rem.start = time;
status.addLast(rem);
}else{
ptime = time;
time += rem.bt;
rem.bt = 0;
rem.wt -= rem.at;
26
solved ++;
gtid.add(rem);
float avgtat = 0;
float avgbt = 0;
);
avgtat += (float)(gtid.get(i).tat);
avgbt += (float)(gtid.get(i).wt);
Output:
27
28
Experiment – 5
Aim:
a) LRU
b) FIFO
c) Optimal
a)
Theory:
In Least Recently Used (LRU) algorithm is a Greedy algorithm where the page to
be replaced is least recently used. The idea is based on locality of reference, the
least recently used page is not likely.
Let say the page reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 . Initially we have 4
page slots empty.
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4
Page faults
0 is already their so —> 0 Page fault.
When 3 came it will take the place of 7 because it is least recently used —>1
Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are
already available in the memory.
29
Program:
import java.util.*;
int n = scn.nextInt();
page[i] = scn.nextInt();
int hit = 0;
if(fm.contains(cur)){
30
System.out.println(cur + " : Hit");
hit++;
}else{
fm.add(cur);
}else{
if(fm.contains(dq.get(x))){
fm.remove(dq.get(x));
fm.add(cur);
break;
if(dqSet.contains(cur)){
dq.remove(new Integer(cur));
dq.add(cur);
}else{
dq.add(cur);
dqSet.add(cur);
31
}
Output:
b)
32
Theory:
Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty
slots
—> 3 Page Faults.
When 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e
1.—>1 Page Fault.
Finally 6 comes, it is also not available in memory so it replaces the oldest page
slot i.e
3 —>1 Page Fault.
Sototalpagefaults=5.
Program:
import java.util.*;
int n = scn.nextInt();
33
for(int i = 0 ; i < n;i++){
page[i] = scn.nextInt();
int hit = 0;
if(fm.contains(cur)){
hit++;
}else{
fm.add(cur);
dq.addLast(cur);
}else{
fm.remove(rem);
dq.addLast(cur);
fm.add(cur);
34
}
Output:
c)
Theory:
In this algorithm, pages are replaced which would not be used for the longest
35
duration of time in the future. Optimal page replacement is perfect, but not
possible in practice as the operating system cannot know future requests. The
use of Optimal Page replacement is to set up a benchmark so that other
replacement algorithms can be analyzed against it.
Program:
import java.util.*;
int n = scn.nextInt();
36
for(int i = 0 ; i < n;i++){
page[i] = scn.nextInt();
int hit = 0;
while(iix.hasNext()){
int vv = (int)iix.next();
System.out.println();
if(fm.contains(cur)){
hit++;
}else{
fm.add(cur);
}else{
37
for(int x = i + 1; x < n;x++){
if(dq.size() == frames){
break;
if(fm.contains(page[x])){
if(!dqSet.contains(page[x])){
dq.addFirst(page[x]);
dqSet.add(page[x]);
if(dq.size()==0){
int vv = fm.iterator().next();
dq.addFirst(vv);
if(dq.size()==1){
int fn = dq.peek();
Iterator ii = fm.iterator();
while(ii.hasNext()){
int vv = (int)ii.next();
if(vv != fn){
dq.addFirst(vv);
38
}
fm.remove(new Integer(dq.getFirst()));
fm.add(cur);
Output:
39
40
Experiment – 6
Aim:
Write a program to implement first fit, best fit and worst fit algorithm for memory
management.
First Fit:
Theory:
In the first fit approach is to allocate the first free partition or hole large enough
which can accommodate the process. It finishes after finding the first suitable
free partition.
Advantage:
Fastest algorithm because it searches as little as possible.
Disadvantage:
The remaining unused memory areas left after allocation become waste if
it is too smaller. Thus request for larger memory requirement cannot be
accomplished.
Program:
import java.util.*;
41
}
42
}else{
System.out.println(assigned[i]);
}
}
}
}
Output:
Best Fit:
Theory:
The best fit deals with allocating the smallest free partition which meets the
requirement of the requesting process. This algorithm first searches the entire
list of free partitions and considers the smallest hole that is adequate. It then
tries to find a hole which is close to actual process size needed.
Advantage:
Memory utilization is much better than first fit as it searches the smallest
free partition first available.
Disadvantage:
43
It is slower and may even tend to fill up memory with tiny useless holes.
Program:
import java.util.*;
public class pract{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter total no of blocks: ");
int b = scn.nextInt();
System.out.println("Enter blocks size : ");
int[] block = new int[b];
int[] bs = new int[b];
for (int i = 0 ; i < b ; i++){
block[i] = scn.nextInt();
bs[i] = block[i];
}
44
if(process[id] <= block[j]){
if(block[j] - process[id] < least){
selected = j;
least = block[j] - process[id];
}
}
}
if(selected == -1){
assigned[id] = -1;
}else{
assigned[id] = selected + 1;
block[selected] -= process[id];
}
id++;
}
System.out.println("PId\tProcess Size Alotted Block");
for(int i = 0; i < n;i++){
System.out.print((i + 1) +"\t" + process[i]+"\t\t" );
if(assigned[i] == -1){
System.out.println("Not assigned");
}else{
System.out.println(assigned[i]);
}
}
}
}
Output:
45
Worst Fit:
Theory:
In worst fit approach is to locate largest available free portion so that the
portion left will be big enough to be useful. It is the reverse of best fit.
Advantage:
Reduces the rate of production of small gaps.
Disadvantage:
If a process requiring larger memory arrives at a later stage then it cannot
be accommodated as the largest hole is already split and occupied.
Program:
import java.util.*;
46
System.out.println("Enter blocks size : ");
int[] block = new int[b];
int[] bs = new int[b];
for (int i = 0 ; i < b ; i++){
block[i] = scn.nextInt();
bs[i] = block[i];
}
47
}else{
assigned[id] = selected + 1;
block[selected] -= process[id];
}
id++;
}
System.out.println("PId\tProcess Size Alotted Block");
for(int i = 0; i < n;i++){
System.out.print((i + 1) +"\t" + process[i]+"\t\t" );
if(assigned[i] == -1){
System.out.println("Not assigned");
}else{
System.out.println(assigned[i]);
}
}
}
}
Output:
48
Experiment – 7
Aim:
Theory:
49
//release lock
signal(m);
/* perform the reading operation */
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
}
Program:
import java.util.concurrent.Semaphore;
@Override
try {
readLock.acquire();
readCount++;
if (readCount == 1) {
50
writeLock.acquire();
readLock.release();
Thread.sleep(2000);
readLock.acquire();
readCount--;
if(readCount == 0) {
writeLock.release();
readLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
@Override
try {
writeLock.acquire();
51
System.out.println(Thread.currentThread().getName() + " is WRITING");
Thread.sleep(3000);
writeLock.release();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
t1.setName("Client 1");
t2.setName("Client 2");
t3.setName("Client 3");
t4.setName("Client 4");
t1.start();
t3.start();
52
t2.start();
t4.start();
Output:
53
Experiment – 8
Aim:
Theory:
Example:
Result:
54
Program:
import java.util.*;
class process{
int[] allocated;
int pid;
int[] available;
int[] maximum;
int n = scn.nextInt();
55
int[] remain = new int[res];
remain[j] = scn.nextInt();
np.pid = scn.nextInt();
np.allocated[j] = scn.nextInt();
np.maximum[j] = scn.nextInt();
list.add(np);
remain[j] -= list.get(i).allocated[j];
56
int solved = 0;
flag = false;
break;
if(flag){
idx = i;
break;
if(idx == -1){
state = false;
break;
}else{
57
remain[j] += list.get(idx).allocated[j];
list.remove(idx);
solved++;
if(state){
System.out.println(ans);
}else{
Output:
58
59