You are on page 1of 16

MASTER OF COMPUTER APPLICATIONS I Year II Semester OPERATING SYSTEMS LAB List of Sample Problems/Experiments: 1.

Simulate the following CPU scheduling algorithms a) Round Robin b) SJF c) FCFS d) Priority 2. Simulate all file allocation strategies a) Sequential b) Indexed c) Linked 3. Simulate MVT and MFT 4. Simulate all File Organization Techniques a) Single level directory b) Two level c) Hierarchical d) DAG 5. Simulate Bankers Algorithm for Dead Lock Avoidance 6. Write a C program to create a child process and allow the parent to display Helloand the child to display Welcome on the screen. 7. Simulate all page replacement algorithms a) FIFO b) LRU c) LFU Etc 8. Simulate Paging Technique of memory management. 9. Write C programs that make a copy of a file using i)standard I/O and ii) system calls. 10. Write C programs that count the number of blanks in a text file using i) standard I/O and ii) system calls. TEXT BOOKS: 1.Operating Systems,P.P. Choudhury, PHI Learning Private Ltd. 2.Operating Systems,R.Chopra,S.Chand and Company Ltd.

1. Simulate the following CPU scheduling algorithms a) Round Robin b) SJF c) FCFS d) Priority ======================================================== Prog No:1(a) ROUND ROBIN SCHEDULING

Aim: Write a C program to implement the various process scheduling mechanisms such
as Round Robin Scheduling. Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating system. As the term is generally used, time slices are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive). Round-robin scheduling is simple, easy to implement, and starvation-free. Round-robin scheduling can also be applied to other scheduling problems, such as data packet scheduling in computer networks. The name of the algorithm comes from the round-robin principle known from other fields, where each person takes an equal share of something in turn.

Algorithm for RR
Step 1: Start the process Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time Step 4: Calculate the no. of time slices for each process where No. of time slice for process(n) = burst time process(n)/time slice Step 5: If the burst time is less than the time slice then the no. of time slices =1. Step 6: Consider the ready queue is a circular Q, calculate (a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-1 ) + the time difference in getting the CPU from process(n-1) (b) Turn around time for process(n) = waiting time of process(n) + burst time of process(n)+ the time difference in getting CPU from process(n). Step 7: Calculate (a) Average waiting time = Total waiting Time / Number of process (b) Average Turnaround time = Total Turnaround Time / Number of process Step 8: Stop the process

/* ROUND ROBIN SCHEDULING ALGORITHM */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char p[10][5]; int et[10],wt[10],timer,count,pt[10],rt,i,j,totwt=0,t,n,found=0,m; float avgwt; clrscr(); printf("\t\t ROUND ROBIN SCHEDULING \n"); printf("Enter the number of Processors \n"); scanf("%d",&n); printf("\nEnter the Timeslice \n"); scanf("%d",&timer); for(i=0;i<n;i++) { printf("enter the process name : "); scanf("%s",&p[i]); printf("enter the processing time : "); scanf("%d",&pt[i]); } m=n; wt[0]=0; i=0; do { if(pt[i]>timer) { rt=pt[i]-timer;

strcpy(p[n],p[i]); pt[n]=rt; et[i]=timer; n++; } else { et[i]=pt[i]; } i++; wt[i]=wt[i-1]+et[i-1]; }while(i<n); count=0; for(i=0;i<m;i++) { for(j=i+1;j<=n;j++) { if(strcmp(p[i],p[j])==0) { count++; found=j; } } if(found!=0) { wt[i]=wt[found]-(count*timer); count=0; found=0; } } for(i=0;i<m;i++)

{ totwt+=wt[i]; } avgwt=(float)totwt/m; printf("\nProcess Name\t Cpu Burst time\t\tWaiting time"); for(i=0;i<m;i++) { printf("\n%s\t\t\t%d\t\t\t%d",p[i],pt[i],wt[i]); } printf("\ntotal waiting time %d\n",totwt); printf("total avgtime %f",avgwt); } OUTPUT: ROUND ROBIN SCHEDULING Enter the number of Processors 3 Enter the Timeslice 4 enter the process name : p1 enter the processing time : 24 enter the process name : p2 enter the processing time : 3 enter the process name : p3 enter the processing time : 3 Process Name p1 p2 p3 total waiting time 17 total avgtime 5.666667 Cpu Burst time 24 3 3 Waiting time 6 4 7

Prog. No: 1(b)

SJF SCHEDULING

Aim: Write a C program to implement the various process scheduling mechanisms such as SJF Scheduling .

Algorithm for SJF


Step 1: Start the process Step 2: Accept the number of processes in the ready Queue Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to highest burst time. Step 5: Set the waiting time of the first process as 0 and its turnaround time as its burst time. Step 6: For each process in the ready queue, calculate (a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1) (b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n) Step 6: Calculate (c) Average waiting time = Total waiting Time / Number of process (d) Average Turnaround time = Total Turnaround Time / Number of process Step 7: Stop the process

/* #include<conio.h> #include<stdio.h> void main() {

SJF SCHEDULING ALGORITHM */

int i, j, n, process[10], total=0, wtime[10], ptime[10], temp, ptemp,tt[10],ttat=0; float atat=0,avg=0; clrscr(); printf("\nEnter number of Processes:"); scanf("%d", &n); for(i=0;i<n;i++) { printf("\nEnter Process %d ID:",i+1); scanf("%d", &process[i]); printf("\nEnter Process %d Wait Time:",i+1); scanf("%d",&ptime[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(ptime[i]>ptime[j]) { temp = ptime[i]; ptime[i] = ptime[j]; ptime[j] = temp; ptemp = process[i]; process[i] = process[j]; process[j] = ptemp; } }

} wtime[0]=0; for(i=1;i<n;i++) { wtime[i]=wtime[i-1]+ptime[i-1]; total=total+wtime[i]; } avg=(float)total/n; for(i=0;i<n;i++) { tt[i]=ptime[i]+wtime[i];

} for(i=0;i<n;i++) { ttat=ttat+tt[i]; } atat=(float)ttat/n; printf("\nP_ID\t P_TIME\t W_TIME\t tt\n"); for(i=0;i<n;i++) printf("%d\t %d\t %d \t %d\n",process[i],ptime[i],wtime[i],tt[i]); printf("\nTotal Waiting Time: %d \nAverage Waiting Time: %f \n Average Turnaround time: %f", total, avg , atat); getch(); }

OUTPUT: Enter number of Processes:4 Enter Process 1 ID:1

Enter Process 1 Wait Time:6

Enter Process 2 ID:2

Enter Process 2 Wait Time:8

Enter Process 3 ID:3

Enter Process 3 Wait Time:7

Enter Process 4 ID:4

Enter Process 4 Wait Time:3

P_ID 4 1 3 2

P_TIME 3 6 7 8

W_TIME 0 3 9 16

turn around time 3 9 16 24

Total Waiting Time: 28 Average Waiting Time: 7.000000 Average Turnaround time: 13.000000

Ex. No: 1(c)

FCFS SCHEDULING

Aim: Write a C program to implement the various process scheduling mechanisms such Algorithm for FCFS scheduling: Step 1: Start the process Step 2: Accept the number of processes in the ready Queue Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time Step 4: Set the waiting of the first process as 0 and its burst time as its turn around time Step 5: for each process in the Ready Q calculate (c) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1) (d) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n) Step 6: Calculate (e) Average waiting time = Total waiting Time / Number of process (f) Average Turnaround time = Total Turnaround Time / Number of process Step 7: Stop the process

/* FCFS SCHEDULING ALGORITHM */


#include<stdio.h> void main() { int i,n,sum,wt,tat,twt,ttat; int t[10]; float awt,atat; clrscr(); printf("Enter number of processors:\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter the Burst Time of the process %d:\t",i+1); scanf("\n %d",&t[i]); } printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n"); printf("\n Process ID \t Waiting Time \t Turn Around Time \n"); printf("1 \t\t 0 \t\t %d \n",t[0]); sum=0; twt=0; ttat=t[0]; for(i=1;i<n;i++) { sum+=t[i-1]; wt=sum; tat=sum+t[i]; twt=twt+wt; ttat=ttat+tat; printf("\n %d \t\t %d \t\t %d",i+1,wt,tat); printf("\n\n"); }

awt=(float)twt/n; atat=(float)ttat/n; printf("\n Average Waiting Time %4.2f",awt); printf("\n Average Turnaround Time %4.2f",atat); getch(); }

OUTPUT: Enter number of processors: 3 Enter the Burst Time of the process 1: 24 Enter the Burst Time of the process 2: 3 Enter the Burst Time of the process 3: 3 FIRST COME FIRST SERVE SCHEDULING ALGORITHM Process ID 1 2 3 Waiting Time Turn Around Time 0 24 27 24 27 30

Average Waiting Time 17.00 Average Turnaround Time 27.00

Ex. No: 1(d)

PRIORITY SCHEDULING

Aim: Write a C program to implement the various process scheduling mechanisms such
as Priority Scheduling.

Algorithm for Priority Scheduling:


Step 1: Start the process Step 2: Accept the number of processes in the ready Queue Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time Step 4: Sort the ready queue according to the priority number. Step 5: Set the waiting of the first process as 0 and its burst time as its turn around time Step 6: For each process in the Ready Q calculate (e) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1) (f) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n) Step 7: Calculate (g) Average waiting time = Total waiting Time / Number of process (h) Average Turnaround time = Total Turnaround Time / Number of process Step 8: Stop the process

/* PRIORITY SCHEDULING */ #include <stdio.h> #include <conio.h> void main() { int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0; float awt,atat; clrscr(); printf("\n-----------PRIORITY SCHEDULING--------------\n"); printf("Enter the No of Process: "); scanf("%d", &n); for (i=0;i<n;i++) { pid[i] = i+1; printf("Enter the Burst time of Pid %d : ",i+1); scanf("%d",&bt[i]); printf("Enter the Priority of Pid %d : ",i+1); scanf ("%d",&pr[i]); } // Sorting start for (i=0;i<n;i++) for(j=i+1;j<n;j++) { if (pr[i] > pr[j] ) { t = pr[i]; pr[i] = pr[j]; pr[j] = t;

t = bt[i]; bt[i] = bt[j]; bt[j] = t;

t = pid[i]; pid[i] = pid[j]; pid[j] = t; } } // Sorting finished tat[0] = bt[0]; wt[0] = 0; for (i=1;i<n;i++) { wt[i] = wt[i-1] + bt[i-1]; tat[i] = wt[i] + bt[i]; } printf("\n---------------------------------------------------------------\n"); printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n"); printf("\n--------------------------------------------------------------\n"); for(i=0;i<n;i++) { printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]); } for(i=0;i<n;i++) { ttat = ttat+tat[i]; twt = twt + wt[i]; } awt = (float)twt / n; atat = (float)ttat / n; printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat); getch(); }

OUTPUT: -----------PRIORITY SCHEDULING-------------Enter the No of Process: 3 Enter the Burst time of Pid 1 : 5 Enter the Priority of Pid 1 : 2 Enter the Burst time of Pid 2 : 6 Enter the Priority of Pid 2 : 3 Enter the Burst time of Pid 3 : 8 Enter the Priority of Pid 3 : 1 --------------------------------------------------------------Pid Priority Burst time WaitingTime TurnArroundTime

---------------------------------------------------------------------------------------3 1 2 1 2 3 8 5 6 0 8 13 8 13 19

Avg.Waiting Time: 7.000000 Avg.Turn Around Time: 13.333333

You might also like