Operating System Lab manual_IT-501
Operating System Lab manual_IT-501
LAB MANUAL
Year/Sem : III/V
9
List of Experiments
Operating Systems (CS-405)
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:
REFERENCE BOOKS:
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:
OBJECTIVE:
To understand SJF CPU scheduling algorithm
ALGORITHM:
Algorithm:
INPUT SET:
OUTPUT SET:
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
OBJECTIVE:
To understand Priority CPU Scheduling algorithm.
ALGORITHM:
INPUT SET:
OUTPUT SET:
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:
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:
INPUT SET:
OUTPUT SET:
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 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
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
} 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 {
readcnt--;
// that is, no reader is left in the critical section,
if (readcnt == 0)
signal(wrt); // writers can enter
} 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: