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

Lab Manual of Operating System Cs

The document contains the lab manual for the Operating Systems course CS-502 at Jai Narain College of Technology in Bhopal, India. It lists 10 experiments related to CPU scheduling algorithms like FCFS, SJF, priority scheduling, and classical OS problems like dining philosophers, producer-consumer, and inter-process communication. It provides theory and implementation details for the first 4 experiments on FCFS scheduling with and without arrival time, and SJF and priority scheduling algorithms.

Uploaded by

Aarfa Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Lab Manual of Operating System Cs

The document contains the lab manual for the Operating Systems course CS-502 at Jai Narain College of Technology in Bhopal, India. It lists 10 experiments related to CPU scheduling algorithms like FCFS, SJF, priority scheduling, and classical OS problems like dining philosophers, producer-consumer, and inter-process communication. It provides theory and implementation details for the first 4 experiments on FCFS scheduling with and without arrival time, and SJF and priority scheduling algorithms.

Uploaded by

Aarfa Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 24

JAI NARAIN COLLAGE OF TECHNOLOGY (BHOPAL)

LAB MANUAL OF OPERATING SYSTEM CS-502

List of experiments :1.Write a program to implement FCFS CPU Scheduling algorithm.

2.Write a program to implement FCFS CPU Scheduling algorithm with arrival time.

3.Write a program to implement SJF CPU Scheduling algorithm.

4.Write a program to implement Priority CPU Scheduling algorithm.

5.Write a program to implement Round Robin CPU Scheduling algorithm.

6.Write a program to implement Dining Philosophers Problem.

7.Write a program to implement Producer-Consumer problem Problem.

8.Write a program to implement & compare various Disk & Drum scheduling algorithms.

9. Write a program to implement & compare various page replacement algorithm.

10. Write a program to implement classical inter process communication problem.

Lecturer of CSE

HOD of CSE

(Ms Shivani Maheshwari)

(Prof. Bhupesh Gour)

PRACTICAL NO. 1

Aim :- Write a program to implement FCFS CPU Scheduling Algorithm.


Theory:- A process is one of the main entities that an operating system needs to care about and is essentially synonymous with a program. A process goes through a range of states during its lifetime, as shown in the illustration, starting with new, ready, running, waiting and terminated. The two main queues that harbour the process are the ready queue and the I/O request queue. The former contains all processes that are eager and ready to execute, but are waiting to get access to the CPU play area. The latter is a queue that accommodates processes that make requests to shared devices, such as secondary storage..

Scheduling Mechanisms :A multiprogramming operating system allows more than one process to be loaded into the executabel memory at a time and for the loaded process to share the CPU using timemultiplexing.Part of the reason for using multiprogramming is that the operating system itself is implemented as one or more processes, so there must be a way for the operating system and application processes to share the CPU. Another main reason is the need for processes to perform I/O operations in the normal course of computation. Since I/O operations ordinarily require orders of magnitude more time to complete than do CPU instructions, multiprograming systems allocate the CPU to another process whenever a process invokes an I/O operation

Non-Preemptive Vs Preemptive Scheduling

Non-Preemptive: Non-preemptive algorithms are designed so that once a process enters the running state(is allowed a process), it is not removed from the processor until it has completed its service time ( or it explicitly yields the processor). context_switch() is called only when the process terminates or blocks. Preemptive: Preemptive algorithms are driven by the notion of prioritized computation. The process with the highest priority should always be the one currently using the processor. If a process is currently using the processor and a new process with a higher priority enters, the ready list, the process on the processor should be removed and returned to the ready list until it is once again the highestpriority process in the system. context_switch() is called even when the process is running usually done via a timer interrupt. First Come First Serve (FCFS) This is a Non-Premptive scheduling algorithm. FIFO strategy assigns priority to processes in the order in which they request the processor.The process that requests the CPU first is allocated the CPU first.When a process comes in, add its PCB to the tail of ready queue. When running process terminates, dequeue the process (PCB) at head of ready queue and run it.

Implementation
#include<iostream.h> #include<conio.h>

int n,i,temp; float avg_rt,avg_tat,avg_wt; int cpu_burst[10],rt[10],tat[10],wt[10];

int t[10];

void main() { clrscr(); cout<<"Enter the no. of Processes(not more than 10) : "; cin>>n; cout<<"\nAssuming all Processes submitted at time t=0 ...\n\n"; for(i=0;i<n;i++) { cout<<"CPU BURST for Process P"<<i<< " : "; cin>>cpu_burst[i]; }

temp=0;

for(i=0;i<=n;i++) { t[i]=temp;

//FCFS SCHEDULING //t[i] is timing variable for Scheduling

temp=temp+cpu_burst[i]; }

for(i=0;i<n;i++) { rt[i]=t[i]; tat[i]=t[i+1];

//Calculating TAT,WT,RT

wt[i]=tat[i]-cpu_burst[i]; }

cout<<"\n";

for(i=0;i<n;i++) { cout<<"P"<<i<<": TAT = "<<tat[i]<<" , RT = "<<rt[i]<<" , WT = "<<wt[i]<<"\n"; }

cout<<"\n"; int total_tat=0,total_rt=0,total_wt=0; for(i=0;i<=n;i++) //Summing up for calculating Average { total_tat=total_tat+tat[i]; total_rt =total_rt +rt[i]; total_wt =total_wt +wt[i]; }

avg_tat=(float)total_tat/n; avg_wt=(float)total_wt/n; avg_rt=(float)total_rt/n;

cout<<"Avg TAT : "<<avg_tat; cout<<"\nAvg WT : "<<avg_wt; cout<<"\nAvg RT : "<<avg_rt;

// TIMINGS FOR GANTT CHART cout<<"\n\n";

for(i=0;i<=n;i++) { cout<<t[i]<<" "; }

getch(); }

PRACTICAL NO. 2 Aim:- Write a program to implement FCFS CPU Scheduling algorithm with arrival time. Theory :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. Since context switches only occur upon process termination, and no reorganization of the process queue is required, scheduling overhead is minimal. Throughput can be low, since long processes can hog the CPU Turnaround time, waiting time and response time can be high for the same reasons above No prioritization occurs, thus this system has trouble meeting process deadlines. The lack of prioritization means that as long as every process eventually completes, there is no starvation. In an environment where some processes might not complete, there can be starvation.

Implementation:#include<iostream.h> #include<conio.h>

int n,i; float temp; float avg_rt,avg_tat,avg_wt; float cpu_burst[10],rt[10],tat[10],wt[10],at[10]; float t[10];

void main() { clrscr(); cout<<"Enter the no. of Processes(not more than 10) : "; cin>>n; cout<<"\nEnter as per increasing AT...\n\n"; for(i=0;i<n;i++) { cout<<"CPU BURST for Process P"<<i<< " : "; cin>>cpu_burst[i]; } for(i=0;i<n;i++) { cout<<"AT for Process P"<<i<< " : "; cin>>at[i]; }

temp=0;

for(i=0;i<=n;i++) { t[i]=temp;

//FCFS SCHEDULING //t[i] is timing variable for Scheduling

temp=temp+cpu_burst[i]; }

for(i=0;i<n;i++) { rt[i]=t[i]-at[i]; tat[i]=t[i+1]-at[i];

//Calculating TAT,WT,RT

wt[i]=tat[i]-cpu_burst[i]; }

cout<<"\n";

for(i=0;i<n;i++) { cout<<"P"<<i<<": TAT = "<<tat[i]<<" , RT = "<<rt[i]<<" , WT = "<<wt[i]<<"\n"; }

cout<<"\n"; float total_tat=0,total_rt=0,total_wt=0; for(i=0;i<=n;i++) //Summing up for calculating Average { total_tat=total_tat+tat[i]; total_rt =total_rt +rt[i]; total_wt =total_wt +wt[i]; }

avg_tat=(float)total_tat/n; avg_wt=(float)total_wt/n;

avg_rt=(float)total_rt/n;

cout<<"Avg TAT : "<<avg_tat; cout<<"\nAvg WT : "<<avg_wt; cout<<"\nAvg RT : "<<avg_rt;

/* // TIMINGS FOR GANTT CHART cout<<"\n\n"; for(i=0;i<=n;i++) { cout<<t[i]<<" "; } */

getch() }

PRACTICAL NO. 3

Aim:- Write a program to implement SJF CPU Scheduling algorithm.

Theory :-

Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process) with the smallest estimated run-time-to-completion is run next. In other words, when CPU is available, it is assigned to the process that has smallest next CPU burst. The SJF scheduling is especially appropriate for batch jobs for which the run times are known in advance. Since the SJF scheduling algorithm gives the minimum average time for a given set of processes, it is probably optimal.

The SJF algorithm favors short jobs (or processors) at the expense of longer ones. The obvious problem with SJF scheme is that it requires precise knowledge of how long a job or process will run, and this information is not usually available.

Implementation :#include<iostream.h> #include<conio.h> int n,i,j,temp; float avg_rt,avg_tat,avg_wt; int cpu_burst[10],rt[10],tat[10],wt[10]; int t[10],p[10]; void main() { clrscr(); cout<<"Enter the no. of Processes(not more than 10) : "; cin>>n; cout<<"\nAssuming all Processes submitted at time t=0\n\n"; for(i=0;i<n;i++) { cout<<"CPU BURST for Process P"<<i<< " : "; cin>>cpu_burst[i]; p[i]=i; } // SORTING wrt cpu_burst for(i=0;i<n;i++) {for(j=i;j<n;j++) { //holding pid too...

if(cpu_burst[i]>cpu_burst[j]) {temp=cpu_burst[i]; cpu_burst[i]=cpu_burst[j]; cpu_burst[j]=temp; temp=p[i]; p[i]=p[j]; p[j]=temp; } } } //FCFS wrt cpu_burst (as already ordered as per burst above...) temp=0; //Ordering process nos...

for(i=0;i<=n;i++) { t[i]=temp;

//FCFS SCHEDULING //t[i] is timing variable for Scheduling

temp=temp+cpu_burst[i]; }

for(i=0;i<n;i++) { rt[i]=t[i]; tat[i]=t[i+1];

//Calculating TAT,WT,RT

wt[i]=tat[i]-cpu_burst[i]; }

cout<<"\n";

for(i=0;i<n;i++) { cout<<"P"<<i<<": TAT = "<<tat[p[i]]<<" , RT = "<<rt[p[i]]<<" , WT = "<<wt[p[i]]<<"\n"; }

cout<<"\n"; int total_tat=0,total_rt=0,total_wt=0; for(i=0;i<=n;i++) //Summing up for calculating Average { total_tat=total_tat+tat[i]; total_rt =total_rt +rt[i]; total_wt =total_wt +wt[i]; }

avg_tat=(float)total_tat/n; avg_wt=(float)total_wt/n; avg_rt=(float)total_rt/n;

cout<<"Avg TAT : "<<avg_tat; cout<<"\nAvg WT : "<<avg_wt; cout<<"\nAvg RT : "<<avg_rt;

/* // TIMINGS FOR GANTT CHART cout<<"\n\n"; for(i=0;i<=n;i++) { cout<<t[i]<<" "; } */ getch();

PRACTICAL NO. 4

Aim :- Write a program to implement Priority CPU Scheduling algorithm.

Theory :The basic idea is straightforward: each process is assigned a priority, and priority is allowed to run. Equal-Priority processes are scheduled in FCFS order. The shortest-Job-First (SJF) algorithm is a special case of general priority scheduling algorithm.

An SJF algorithm is simply a priority algorithm where the priority is the inverse of the (predicted) next CPU burst. That is, the longer the CPU burst, the lower the priority and vice versa. Priority can be defined either internally or externally. Internally defined priorities use some measurable quantities or qualities to compute priority of a process.

Implementation :#include<iostream.h> #include<conio.h> int n,i,j,temp; float avg_rt,avg_tat,avg_wt; int cpu_burst[10],rt[10],tat[10],wt[10],pri[10]; int t[10],p[10]; void main() { clrscr(); cout<<"Enter the no. of Processes(not more than 10) : "; cin>>n; cout<<"\nAssuming all Processes submitted at time t=0\n\n"; for(i=0;i<n;i++) { cout<<"CPU BURST for Process P"<<i<< " : "; cin>>cpu_burst[i]; p[i]=i; } for(i=0;i<n;i++) { //holding pid too...

cout<<"Priority for Process P"<<i<< " : "; cin>>pri[i]; } // SORTING wrt priority for(i=0;i<n;i++) {for(j=i;j<n;j++) { if(pri[i]>pri[j]) {temp=pri[i]; pri[i]=pri[j]; pri[j]=temp; temp=p[i]; p[i]=p[j]; p[j]=temp; temp=cpu_burst[i]; //ordering burst... //Ordering process nos...

cpu_burst[i]=cpu_burst[j]; cpu_burst[j]=temp; } } }

//FCFS wrt pri (as already ordered as per pri above...)

temp=0; for(i=0;i<=n;i++) //FCFS SCHEDULING

{ t[i]=temp;

//t[i] is timing variable for Scheduling

temp=temp+cpu_burst[i]; }

for(i=0;i<n;i++) { rt[i]=t[i]; tat[i]=t[i+1];

//Calculating TAT,WT,RT

wt[i]=tat[i]-cpu_burst[i]; }

cout<<"\n"; j=0; for(i=n-1;i>=0;i--) { cout<<"P"<<j++<<": TAT = "<<tat[p[i]]<<" , RT = "<<rt[p[i]]<<" , WT = "<<wt[p[i]]<<"\n"; }

cout<<"\n"; int total_tat=0,total_rt=0,total_wt=0; for(i=0;i<=n;i++) //Summing up for calculating Average { total_tat=total_tat+tat[i]; total_rt =total_rt +rt[i];

total_wt =total_wt +wt[i]; }

avg_tat=(float)total_tat/n; avg_wt=(float)total_wt/n; avg_rt=(float)total_rt/n;

cout<<"Avg TAT : "<<avg_tat; cout<<"\nAvg WT : "<<avg_wt; cout<<"\nAvg RT : "<<avg_rt;

/* // TIMINGS FOR GANTT CHART cout<<"\n\n"; for(i=0;i<=n;i++) { cout<<t[i]<<" "; }*/getch();

PRACTICAL NO. 5

Aim :- Write a program to implement Round Robin CPU Scheduling algorithm. Theory :- One of the oldest, simplest, fairest and most widely used algorithm is round robin
(RR). In the round robin scheduling, processes are dispatched in a FIFO manner but are given a limited amount of CPU time called a time-slice or a quantum. If a process does not complete before its CPU-time expires, the CPU is preempted and given to the next process waiting in a queue. The preempted process is then placed at the back of the ready list. Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in time-sharing

environments in which the system needs to guarantee reasonable response times for interactive users. The only interesting issue with round robin scheme is the length of the quantum. Setting the quantum too short causes too many context switches and lower the CPU efficiency. On the other hand, setting the quantum too long may cause poor response time and appoximates FCFS.

Implementation :#include<iostream.h> #include<conio.h> #include<dos.h> void main() { clrscr(); int numberOfProcesses; int i; int timeSlot; int hover = 0; //number of processes cout << "\nNUMBER OF PROCESSES: "; cin >> numberOfProcesses; int burstTime[10];

//time slot cout << "TIME SLOT:"; cin >> timeSlot;

//burst time for(i=0; i<numberOfProcesses; i++){ cout << "\nPROCESS " << i+1 << "\n"; cout << "\tBURST TIME: ";

cin>>burstTime[i]; }

//display burst time cout << "\n================================\n"; cout << " PROCESS\t BURST TIME\t ";

for(i=0; i<numberOfProcesses; i++){ cout << "\n P"<<i+1<<"\t\t"<<burstTime[i];

hover += burstTime[i]; } cout << "\n================================\n\n";

//start process execution int sec = timeSlot; while(hover != 0){ for(i=0; i<numberOfProcesses; i++){ if(burstTime[i] > 0){ if(burstTime[i] > timeSlot){ sleep(timeSlot); burstTime[i] -= timeSlot; hover -= timeSlot; cout << "at " << sec <<"s\tPROCESS P"; cout << i+1 << " is running ......\n"; sec += timeSlot; }else{ sleep(burstTime[i]); hover -= burstTime[i]; cout << "at " << sec <<"s\tPROCESS P";

cout << i+1 <<" has completed\n"; sec += burstTime[i]; burstTime[i] = 0; }

} } } getch(); }

You might also like