0% found this document useful (0 votes)
3 views

Operating System Lab manual_IT-501

The document is a lab manual for an Operating Systems course, detailing various experiments related to CPU scheduling algorithms and inter-process communication. It includes a list of experiments, their objectives, algorithms, and expected outcomes, along with software requirements and reference books. The manual is designed for students in the Information Technology branch, providing structured guidance for completing practical assignments.

Uploaded by

niti.patil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Operating System Lab manual_IT-501

The document is a lab manual for an Operating Systems course, detailing various experiments related to CPU scheduling algorithms and inter-process communication. It includes a list of experiments, their objectives, algorithms, and expected outcomes, along with software requirements and reference books. The manual is designed for students in the Information Technology branch, providing structured guidance for completing practical assignments.

Uploaded by

niti.patil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

DEPARTMENT OF INFORMATION TECHNOLOGY

LAB MANUAL

Name of Student : …………………..

Name of Lab : Operating

Systems Subject Code : IT-501

Branch : Information Technology

Year/Sem : III/V

Affiliated to Rajiv Gandhi Prodyogiki Vishwavidyalaya, Bhopal (MP)


INDEX
S. Name of Experiment Date Sign Remark
No.
1. Write a program to implement FCFS CPU scheduling algorithm.
2. Write a program to implement SJF CPU scheduling algorithm.
3. Write a program to implement Priority CPU Scheduling algorithm.
4. Write a program to implement Round Robin CPU scheduling algorithm.
5 Write a program to implement classical inter process communication
problem(producer consumer).
6 Write a program to implement classical inter process communication
problem(ReaderWriters).
7 Write a program to implement classical inter process communication
problem(Dining_Philosophers).
8 Write a program to implement various page replacement algorithm.

9
List of Experiments
Operating Systems (CS-405)

1. Program to implement FCFS CPU scheduling algorithm.


2. Program to implement SJF CPU scheduling algorithm.
3. Write a program to implement Priority CPU Scheduling algorithm.
4. Write a program to implement Round Robin CPU scheduling algorithm.
5. Write a program to implement classical inter process communication
problem(producer consumer).
6. Write a program to implement classical inter process communication
problem(Reader Writers)
7. Write a program to implement classical inter process
communication problem(Dining_Philosophers).
8. Program to implement FIFO page replacement algorithm.
9. Program to implement LRU page replacement algorithm.
LAB MANUAL
Operating Systems (CS-405)
Total number of Experiments:
9 Total number of Turns: 9

S. No. Name of Experiment Turns


needed to
complete
1. Write a program to implement FCFS CPU scheduling algorithm. 1
2. Write a program to implement SJF CPU scheduling algorithm. 1
3. Write a program to implement Priority CPU Scheduling algorithm. 1
4. Write a program to implement Round Robin CPU scheduling algorithm. 1
5 Write a program to implement classical inter process communication 1
problem(producer consumer).
6 Write a program to implement classical inter process communication 1
problem(ReaderWriters).
7 Write a program to implement classical inter process communication 1
problem(Dining_Philosophers).
8 Write a Program to implement FIFO page replacement algorithm. 1
9 Write a Program to implement LRU page replacement algorithm 1
Distribution of Lab Hours: 1 Hour – 40 Minutes
Explanation of Experiment: 20 Min.
Performance of Experiment: 50 Min.
File Checking: 10 Min.
Attendance: 05 Min.
Viva/Quiz: 05 Min.
Solving of Queries: 10 Min.
DEPLOYMENT OF THE EXPERIMENT:

Turn Deployment
1. Experiment No.1
2. Experiment No.2
3. Experiment No.3
4. Experiment No.4
5. Experiment No.5
6. Experiment No.6
7. Experiment No.7
8. Experiment No.8
9. Experiment No.9

SOFTWARE REQUIREMENT:

o GCC

BATCH DISTRIBUTION:

Total Dedicated Individual Computer System: 30 Systems


(Per Batch of 30 Students)

REFERENCE BOOKS:

S. No. Title of the Book Authors Publication


1. Operating System Concepts 9/E Silberschatz, Galvin, Gagne Wiley
2. Operating Systems William stalling PHI
EXPERIMENT NO.1
Unit/Topic: 2/CPU Scheduling
PROBLEM DEFINITION:
Write a program to implement FCFS CPU scheduling algorithm.

OBJECTIVE:
To understand FCFS CPU scheduling algorithm.

ALGORITHM:
1. Start the process
2. Get the number of processes to be inserted
3. Get the value for burst time of each process from the user
4. Having allocated the burst time(bt) for individual processes , Start with the first process from
it‟s initial position let other process to be in queue
5. Calculate the waiting time(wt) and turnaround time(tat) as
Wt(pi) = wt(pi-1) + tat(pi-1) (i.e wt of current process = wt of previous process + tat of previous
process)
tat(pi) = wt(pi) + bt(pi) (i.e tat of current process = wt of current process + bt of current process)
6. Calculate the total and average waiting time and turnaround time
7. Display the values
8. Stop the process

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


Q.1 What is FCFS algorithm?
Q.2 When is CPU Scheduling?

NAME OF FACULTY: Prof. Priyanka Kumrawat


SIGNATURE:
DATE:
EXPERIMENT NO.2

Unit/Topic: 2/CPU Scheduling


PROBLEM DEFINITION:
Write a program to implement SJF CPU scheduling algorithm.

OBJECTIVE:
To understand SJF CPU scheduling algorithm
ALGORITHM:
Algorithm:

1. Start the process


2. Get the number of processes to be inserted
3. Sort the processes according to the burst time and allocate the one with shortest burst to
execute first
4. If two processes have same burst length then FCFS scheduling algorithm is used
5. Calculate the total and average waiting time and turnaround time
6. Display the values
7. Stop the process

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


Q.1 What is ready queue
Q.2 When is scheduler?

NAME OF FACULTY:
SIGNATURE:
DATE:
SJF Program:
#include<iostream>
using namespace std;
int main(){
int n, bt[20],p[20],wt[20],tat[20],total=0,i,j,pos,temp;
float avwt,avtat;
cout<<"Enter total number of processes:";
cin>>n;
cout<<"\nEnter Process Burst Time:\n";
for(i=0;i<n;i++){
cout<<"P["<<i+1<<"]:";
cin>>bt[i];
p[i]=i+1;
}
for(i=0;i<n;i++)
{ pos=i;
for(j=i+1;j<n;j++)
{ if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++) wt[i]
+=bt[j];
total+=wt[i];
}
avwt=total/n;
total=0;
cout<<endl<<"Process\t\tBurst Time\tWaiting Time\tTurnaround Time"<<endl; for(i=0;i<n;i+
+){
tat[i]=wt[i]+bt[i];
total+=tat[i];
cout<<"P["<<p[i]<<"]\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i]<<endl;
}
avtat=total/n;
cout<<endl<<"Average waiting Time:"<<avwt<<endl;
cout<<"Average Turnaround Time:"<<avtat<<endl;
return 0;
}
Output:
EXPERIMENT NO.3

Unit/Topic: 3/CPU Scheduling


PROBLEM DEFINITION:
Write a program to implement Write a program to implement Priority CPU Scheduling algorithm.

OBJECTIVE:
To understand Priority CPU Scheduling algorithm.

ALGORITHM:

1. Start the process


2. Get the number of processes to be inserted
3. Get the corresponding priority of processes
4. Sort the processes according to the priority and allocate the one with highest priority to
execute first
5. If two process have same priority then FCFS scheduling algorithm is used
6. Calculate the total and average waiting time and turnaround time
7. Display the values
8. Stop the process

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


Q.1 What is long term scheduler?
Q.2 what is priority scheduling?

NAME OF FACULTY:
SIGNATURE:
DATE:
Program: Priority CPU Scheduling
include<iostream>
using namespace std;
int main()
{
int bt[20], wt[20],p[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter total Number of process:";
cin>>n;
cout<<"\nEnter Burst Time and Priority\n";
for(i=0;i<n;i++){ cout<<"\nP["<<i+1<<"]\
n";
cout<<"Burst Time";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++){
if(pr[j]<pr[pos])
pos=j;

}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;
total=0;
cout<<"\nProcess\tBurst Time\tWaiting Time\tTurnaround Time"; for(i=0;i<n;i+
+){
tat[i]=wt[i]+bt[i];
total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t" <<wt[i]<<"\t\t"<<tat[i];
}
avg_tat=total/n;
cout<<"\n\nAverage waiting Time:"<<avg_wt; cout<<"\
n\nAverage Turnaround Time:"<<avg_tat; return 0;
}
EXPERIMENT NO.4
Unit/Topic: 2/CPU Scheduling

PROBLEM DEFINITION:
Write a program to implement Round Robin CPU scheduling algorithm.

OBJECTIVE:
To understand Round Robin CPU scheduling algorithm.
ALGORITHM:
1. Start the process
2. Get the number of elements to be inserted
3. Get the value for burst time for individual processes
4. Get the value for time quantum
5. Make the CPU scheduler go around the ready queue allocating CPU to each process for the
time interval specified
6. Make the CPU scheduler pick the first process and set time to interrupt after quantum. And
after it's expiry dispatch the process
7. If the process has burst time less than the time quantum then the process is released by the CPU
8. If the process has burst time greater than time quantum then it is interrupted by the OS and
the process is put to the tail of ready queue and the schedule selects next process from head of
the queue
9. Calculate the total and average waiting time and turnaround time
10. Display the results
11. Stop the process

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


Q.1 What is preemptive scheduling?
Q.2 what is time slice in scheduling?

NAME OF FACULTY:
SIGNATURE:
DATE:
#include<iostream>
using namespace std;
int main(){
int i,n,time,remain,temps=0,time_quantum;
int wt=0,tat=0;
cout<<"Enter the total number of process="<<endl;
cin>>n;
remain=n;
int at[n];
int bt[n];
int rt[n];
cout<<"Enter the Arrival time,Burst time for all the processes"<<endl; for(i=0;i<n;i+
+)
{
cout<<"Arrival time for process"<<i+1<<endl;
cin>>at[i];
cout<<"Burst time for process"<<i+1<<endl;
cin>>bt[i];
rt[i]=bt[i];
}
cout<<"Enter the value of time quantum:"<<endl;
cin>>time_quantum;
cout<<"\n\nProcess\t:Turnaround time:Waiting Time\n\n";
for(time=0,i=n;remain!=0;){
if(rt[i]<=time_quantum && rt[i]>0)
{ time +=rt[i];
rt[i]=0;
temps=1;
}
else if(rt[i]>0)
{

rt[i] -=time_quantum;
time +=time_quantum;
}
if(rt[i]==0 && temps==1){

remain--;
printf("Process{%d}\t:\t%d\t:\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
cout<<endl;
wt +=time-at[i]-bt[i];
tat +=time-at[i];
temps=0;

}
if(i==n-1)
i=0;
else if(at[i+1]<=time)
i++;
else
i=0;

}
cout<<"Average waiting time"<<wt*1.0/n<<endl;
cout<<"Average turnaround time"<<tat*1.0/n<<endl;
return 0;
}
EXPERIMENT NO.5
Unit/Topic: 3/IPC
PROBLEM DEFINITION:
Write a program to implement classical inter process communication problem(producer consumer).

OBJECTIVE:
To understand classical inter process communication problem (producer consumer).

ALGORITHM:

The producer-consumer problem illustrates the need for synchronization in systems where many
processes share a resource. In the problem, two processes share a fixed-size buffer. One process
produces information and puts it in the buffer, while the other process consumes information from
the buffer. These processes do not take turns accessing the buffer, they both work concurrently.
Here in lies the problem. What happens if the producer tries to put an item into a full buffer? What
happens if the consumer tries to take an item from an empty buffer?
In order to synchronize these processes, we will block the producer when the buffer is full, and
we will block the consumer when the buffer is empty. So the two processes, Producer and
Consumer, should work as follows:

(1) The producer must first create a new widget.


(2) Then, it checks to see if the buffer is full. If it is, the producer will put itself to sleep until
the consumer wakes it up. A "wakeup" will come if the consumer finds the buffer empty.
(3) Next, the producer puts the new widget in the buffer. If the producer goes to sleep in
step (2), it will not wake up until the buffer is empty, so the buffer will never overflow.
(4) Then, the producer checks to see if the buffer is empty. If it is, the producer assumes that
the consumer is sleeping, an so it will wake the consumer. Keep in mind that between any
of these steps, an interrupt might occur, allowing the consumer to run.
(1) The consumer checks to see if the buffer is empty. If so, the consumer will put itself to sleep
until the producer wakes it up. A "wakeup" will occur if the producer finds the buffer empty
after it puts an item into the buffer.
(2) Then, the consumer will remove a widget from the buffer. The consumer will never try to
remove a widget from an empty buffer because it will not wake up until the buffer is full.
(3) If the buffer was full before it removed the widget, the consumer will wake the producer.
(4) Finally, the consumer will consume the widget. As was the case with the producer, an
interrupt could occur between any of these steps, allowing the producer to run.

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


Q.1 What is critical section?
Q.2 What is concurrent execution?

NAME OF FACULTY: Prof. Priyanka Kumrawat


SIGNATURE:
DATE:
Code: Producer-Consumer
#include<iostream>
using namespace std;
int mutex=1, full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int); cout<<"\n1.Producer\
n2.Consumer\n3.Exit"; while(1)
{
cout<<"\nEnter your choice:";
cin>>n;
switch(n)
{
case 1:if((mutex==1)&&(empty!=0))
producer();
else
cout<<"Buffer is full!!";
break;
case 2:if((mutex==1)&&(full!=0))
consumer();
else
cout<<"Buffer is empty!!";
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{

return(--s);
}
int signal(int s)
{
return(++s);

}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty); x+
+;
cout<<"\nProducer produces the item",x;
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
cout<<"\nConsumer consumes an item",x;
x--;
mutex=signal(mutex);
}

Output:
Enter your cho1ce 2

on sumer consumes ar■1tem


Enter your cho1ce 2

on sumer consumes .air■ 1tem


Enter your cho1ce 2

on sumer n s
co um es ar■ 1tem
Enter your cho1ce 2
u-f=-f=e r ::i... s empty !
Enter your cho1ce
EXPERIMENT NO.6
Unit/Topic: 3/IPC
PROBLEM DEFINITION:
Write a program to implement classical inter process communication problem (Reader Writers).
OBJECTIVE:
To understand classical inter process communication problem (Reader Writers).

ALGORITHM:
Consider a situation where we have a file shared between many people.
 If one of the people tries editing the file, no other person should be reading or writing at
the same time, otherwise changes will not be visible to him/her.
 However if some person is reading the file, then others may read it at the same
time. Precisely in OS we call this situation as the readers-writers problem

Problem parameters:
 One set of data is shared among a number of processes
 Once a writer is ready, it performs its write. Only one writer may write at a time
 If a process is writing, no other process can read it
 If at least one reader is reading, no other process can write
 Readers may not write and only read
Solution when Reader has the Priority over Writer
Here priority means, no reader should wait if the share is currently opened for reading.
Three variables are used: mutex, wrt, readcnt to implement solution
1. semaphore mutex, wrt; // semaphore mutex is used to ensure mutual exclusion
when readcnt is updated i.e. when any reader enters or exit from the critical section and
semaphore wrt is used by both readers and writers
2. int readcnt; // readcnt tells the number of processes performing read in the
critical section, initially 0

Functions for sempahore :


– wait() : decrements the semaphore value.
– signal() : increments the semaphore value.

Writer process:
1. Writer requests the entry to critical section.
2. If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed,
it keeps on waiting.
3. It exits the critical section.

do {
// writer requests for critical section
wait(wrt);
// performs the write

// leaves the critical section


signal(wrt);

} while(true);

Reader process:
1. Reader requests the entry to critical section.
2. If allowed:
 it increments the count of number of readers inside the critical section. If this
reader is the first reader entering, it locks the wrt semaphore to restrict the entry of
writers if any reader is inside.
 It then, signals mutex as any other reader is allowed to enter while others are
already reading.
 After performing reading, it exits the critical section. When exiting, it checks if no
more reader is inside, it signals the semaphore “wrt” as now, writer can enter the critical
section.
3. If not allowed, it keeps on waiting.

do {

// Reader wants to enter the critical section


wait(mutex);

// The number of readers has now increased by 1


readcnt++;

// there is atleast one reader in the critical section


// this ensure no writer can enter if there is even one reader
// thus we give preference to readers here
if (readcnt==1)
wait(wrt);

// other readers can enter while this current reader is inside


// the critical section
signal(mutex);

// current reader performs reading here


wait(mutex); // a reader wants to leave

readcnt--;
// that is, no reader is left in the critical section,
if (readcnt == 0)
signal(wrt); // writers can enter

signal(mutex); // reader leaves

} while(true);

Thus, the semaphore „wrt„ is queued on both readers and writers in a manner such that preference
is given to readers if writers are also there. Thus, no reader is waiting simply because a writer has
requested to enter the critical section.

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


Q.1 What is critical region?
Q.2 what is mutual exclusion and progress?

NAME OF FACULTY: Prof. Priyanka Kumrawat


SIGNATURE:
DATE:
Code: Reader-Writer Problem
#include<semaphore.h>
#include<iostream>
#include<pthread.h>
using namespace std;
sem_t x,y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount;
void *reader(void* param)
{
sem_wait(&x);
readercount++;
if(readercount==1)
sem_wait(&y);
sem_post(&x);
cout<<"\nReader is inside",readercount;
sem_wait(&x);
readercount--;
if(readercount==0)
{
sem_post(&y);
}
sem_post(&x);
cout<<"\nReader is leaving",readercount+1;
}
void *writer(void* param)
{
cout<<"\nWriter is trying to enter";
sem_wait(&y);
cout<<"\nWriter has entered";
sem_post(&y); cout<<"\
nWriter is leaving";
}
int main()
{
int n2,i;
cout<<"Enter the number of readers:";
cin>>n2;
int n1[n2];
sem_init(&x,0,1);
sem_init(&y,0,1);
for(i=0;i<n2;i++)
{
pthread_create(&writerthreads[i],NULL,reader,NULL);
pthread_create(&readerthreads[i],NULL,writer,NULL);
}
for(i=0;i<n2;i++)
{
pthread_join(writerthreads[i],NULL);
pthread_join(readerthreads[i],NULL);
}}
Output:

You might also like