Lab Manual of Operating System Cs
Lab Manual of Operating System Cs
2.Write a program to implement FCFS CPU Scheduling algorithm with arrival time.
8.Write a program to implement & compare various Disk & Drum scheduling algorithms.
Lecturer of CSE
HOD of CSE
PRACTICAL NO. 1
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: 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 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;
temp=temp+cpu_burst[i]; }
//Calculating TAT,WT,RT
wt[i]=tat[i]-cpu_burst[i]; }
cout<<"\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]; }
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;
temp=temp+cpu_burst[i]; }
//Calculating TAT,WT,RT
wt[i]=tat[i]-cpu_burst[i]; }
cout<<"\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;
getch() }
PRACTICAL NO. 3
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;
temp=temp+cpu_burst[i]; }
//Calculating TAT,WT,RT
wt[i]=tat[i]-cpu_burst[i]; }
cout<<"\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]; }
PRACTICAL NO. 4
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; } } }
{ t[i]=temp;
temp=temp+cpu_burst[i]; }
//Calculating TAT,WT,RT
wt[i]=tat[i]-cpu_burst[i]; }
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];
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];
//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 ";
//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";
} } } getch(); }