Вы находитесь на странице: 1из 23

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

SRM UNIVERSITY
SRM NAGAR, KATTANKULATHUR - 603203.

SUBJECT SUBJECT CODE SEMESTER

: OPRATING SYSTEM LAB : CS0212 : II CLASS: I year CSE

EX. NO

NAME OF THE EXPERIMENTS

PAGE NO

SIGN

1 2 3 4 5 6 7 8

CPU Scheduling FCFS CPU Scheduling SJF CPU Scheduling SJF Priority CPU Scheduling Round Robin Page Replacements FIFO Page Replacements LRU Page Replacements - Optimal Dead Lock - Detection

1 3 5 8 11 14 17 20

Ex. No: 1

CPU SCHEDULING - FCFS

Aim: To write a new programs CPU Scheduling using First Come First Serve algorithm in c program.

Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 : Start the program : Define maximum array value : To give the total number of Jobs value : To give the jobs time one by one up to total number of jobs : To calculate service time : To calculate individual job waiting time : To calculate total waiting time : To calculate average waiting time : To write each job in waiting time : To write continue total service time, separate job waiting time, total waiting time and average waiting time : End

Program:
#include<stdio.h> void main() { int i,n,a[20],wt[20],st=0,twt=0; float awt; clrscr(); printf("CPU Scheduling - FCFS"); printf("\n--------------------------"); printf("\nEnter the number of JOBS:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf(" Enter the JOB%d:",i); scanf("%d",&a[i]); } for(i=1;i<=n;i++) { wt[i]=st; twt=twt+wt[i]; st=st+a[i]; } awt=twt/n; for(i=1;i<=n;i++) { printf("\nWaiting Time JOB%d: %d",i,wt[i]); }

printf("\n--------------------------"); printf("\n%d-JOBS Service time\t: %d",n,st); printf("\nTotal Waiting Time\t: %d",twt); printf("\nAverage Waiting Time\t: %.2f\n",awt); getch(); }

OUTPUT:
CPU Scheduling - FCFS -------------------------Enter the number of JOBS: 5 Enter the JOB1:12 Enter the JOB2:25 Enter the JOB3:35 Enter the JOB4:48 Enter the JOB5:56 Waiting Time JOB1: 0 Waiting Time JOB2: 12 Waiting Time JOB3: 37 Waiting Time JOB4: 72 Waiting Time JOB5: 120 -------------------------5-JOBS Service time : 176 Total Waiting Time : 241 Average Waiting Time : 48.00

Result: Thus the above CPU Scheduling using First Come First Serve algorithm Program to compile and run the output was successfully completed.

Ex. No: 2

CPU SCHEDULING - SJF

Aim: To write a new program CPU Scheduling using Sort Job First algorithm in c program.

Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 Step 12 Step 13 : Start the program : Define maximum array value : To give the total number of Jobs value : To give the jobs time one by one up to total number of jobs : To sort the all jobs in ascending order : To write a sorted jobs in ascending order : To calculate service time : To calculate individual job waiting time : To calculate total waiting time : To calculate average waiting time : To write each job in waiting time : To write continue total service time, separate job waiting time, total waiting time and average waiting time : End

Program:
#include<stdio.h> void main() { int i,j,n,temp=0,a[20],b[20],wt[20],st=0,twt=0; float awt; clrscr(); printf("CPU Scheduling - SJF"); printf("\n--------------------------"); printf("\nEnter the number of JOBS:"); scanf("%d",&n); for(i=0;i<n;i++) { printf(" Enter the JOB%d:",i); scanf("%d",&a[i]); } for(i=1;i<n;i++) { temp=a[i]; for(j=i-1;j>=0&&temp<a[j];j--) { a[j+1]=a[j]; a[j]=temp; } }

printf("\nSorting..."); for(i=0;i<n;i++) { printf("\n Sort: %d",a[i]); wt[i]=st; twt=twt+wt[i]; st=st+a[i]; } awt=twt/n; for(i=0;i<n;i++) { printf("\nWaiting Time JOB%d: %d",i,wt[i]); } printf("\n--------------------------"); printf("\n%d-JOBS Service time\t: %d",n,st); printf("\nTotal Waiting Time\t: %d",twt); printf("\nAverage Waiting Time\t: %.2f\n",awt); getch(); }

OUTPUT:
CPU Scheduling - SJF -------------------------Enter the number of JOBS: 5 Enter the JOB0:20 Enter the JOB1:30 Enter the JOB2:40 Enter the JOB3:10 Enter the JOB4:60 Sorting... Sort: 10 Sort: 20 Sort: 30 Sort: 40 Sort: 60 Waiting Time JOB0: 0 Waiting Time JOB1: 10 Waiting Time JOB2: 30 Waiting Time JOB3: 60 Waiting Time JOB4: 100 -------------------------5-JOBS Service time : 160 Total Waiting Time : 200 Average Waiting Time : 40.00

Result: Thus the above CPU Scheduling using Sorted Job First algorithm Program to compile and run the output was successfully completed.

Ex. No: 3

CPU SCHEDULING SJF Priority

Aim: To write a new program CPU Scheduling using SJF Priority algorithm in c program.

Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 Step 12 Step 13

: Start the program : Define maximum array value : To give the total number of Jobs value : To give the job time and priority one by one up to total no of jobs : To sort the all jobs based on priority in ascending order : To write a sorted priority and jobs in ascending order : To calculate service time : To calculate individual job waiting time : To calculate total waiting time : To calculate average waiting time : To write each job in waiting time : To write continue total service time, separate job waiting time, total waiting time and average waiting time : End

Program:
#include<stdio.h> void main() { int i,j,n,tempp=0,tempj=0,a[20],b[20],wt[20],st=0,twt=0; float awt; clrscr(); printf("CPU Scheduling - SJF Priority"); printf("\n----------------------------"); printf("\nEnter the number of JOBS:"); scanf("%d",&n); for(i=0;i<n;i++) { printf(" Enter JOB%d:",i+1); scanf("%d",&a[i]); printf(" Enter Priority:"); scanf("%d",&b[i]); } for(i=1;i<n;i++) { tempp=b[i]; tempj=a[i]; for(j=i-1;j>=0&&tempp<b[j];j--) { b[j+1]=b[j];

b[j]=tempp; a[j+1]=a[j]; a[j]=tempj; } } printf("\nPriority Sorting..."); for(i=0;i<n;i++) { printf("\n Priority: %d \tSort: %d",b[i],a[i]); wt[i]=st; twt=twt+wt[i]; st=st+a[i]; } awt=twt/n; for(i=0;i<n;i++) { printf("\nWaiting Time JOB%d: %d",i,wt[i]); } printf("\n----------------------------"); printf("\n%d-JOBS Service time\t: %d",n,st); printf("\nTotal Waiting Time\t: %d",twt); printf("\nAverage Waiting Time\t: %.2f\n",awt); getch(); }

OUTPUT: CPU Scheduling - SJF Priority ---------------------------Enter the number of JOBS: 5 Enter JOB1: 10 Enter Priority: 5 Enter JOB2: 20 Enter Priority: 1 Enter JOB3: 60 Enter Priority: 3 Enter JOB4: 70 Enter Priority: 4 Enter JOB5: 80 Enter Priority: 6 Priority Sorting... Priority: 1 Sort: 20 Priority: 3 Sort: 60 Priority: 4 Sort: 70 Priority: 5 Sort: 10 Priority: 6 Sort: 80 Waiting Time JOB0: 0 Waiting Time JOB1: 20 Waiting Time JOB2: 80 Waiting Time JOB3: 150 Waiting Time JOB4: 160 ---------------------------5-JOBS Service time : 240 Total Waiting Time : 410 Average Waiting Time : 82.00

Result: Thus the above CPU Scheduling using SJF Priority algorithm Program to compile and run the output was successfully completed.

Ex. No: 4

CPU SCHEDULING Round Robin

Aim: To write a new program CPU Scheduling using Round Robin algorithm in c program.

Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 Step 12 Step 13

: Start the program : Define maximum array value : To give the total number of Jobs value : To give the job time one by one up to total no of jobs : To give the time slot value : To write waiting time of each job processing based on time slot order : To calculate service time : To calculate individual job waiting time : To calculate total waiting time : To calculate average waiting time : To write each job in waiting time : To write continue total service time, separate job waiting time, total waiting time and average waiting time : End

Program:
#include<stdio.h> struct job { int pt,st,wt; }j[10]; void main() { int i,svt=0,tst=0,n,t,tw=0; float aw; clrscr(); printf("CPU Scheduling - Round Robin"); printf("\n---------------------------"); printf("\n Enter No of JOBS:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter JOB%d processing time:",i); scanf("%d",&j[i].pt); } printf("\n Enter the Time Slot:"); scanf("%d",&t); for(i=1;i<=n;i++) tst=tst+j[i].pt;

printf("\n Waiting time of each processing:"); printf("\n ---------------------------------\n"); while(svt!=tst) { for(i=1;i<=n;i++) { if(j[i].pt>=t) { printf(" Job %d=%d\t",i,svt); j[i].pt=j[i].pt-t; j[i].wt=j[i].wt+(svt-j[i].st); svt=svt+t; j[i].st=svt; } else { if(j[i].pt!=0) { printf(" Job %d=%d\t",i,svt); j[i].wt=j[i].wt+(svt-j[i].st); svt=svt+j[i].pt; j[i].pt=j[i].pt-j[i].pt; } } } printf("\n"); } printf("\n Each JOB Waiting Time List:\n"); printf(" --------------------------"); for(i=1;i<=n;i++) { printf("\n Waiting time of job %d:%d",i,j[i].wt); tw=tw+j[i].wt; } printf("\n\n----------------------------"); printf("\nTotal Service Time\t:%d",svt); printf("\nTotal Waiting Time\t:%d",tw); aw=tw/n; printf("\nAverage Waiting Time\t:%.2f",aw); getch(); }

OUTPUT: CPU Scheduling - Round Robin --------------------------Enter No of JOBS: 3 Enter JOB1 processing time: 4 Enter JOB2 processing time: 2 Enter JOB3 processing time: 6 Enter the Time Slot: 2 Waiting time of each processing: --------------------------------Job 1=0 Job 2=2 Job 3=4 Job 1=6 Job 3=8 Job 3=10 Each JOB Waiting Time List: -------------------------Waiting time of job 1:4 Waiting time of job 2:2 Waiting time of job 3:6 ---------------------------Total Service Time : 12 Total Waiting Time : 12 Average Waiting Time : 4.00

Result: Thus the above CPU Scheduling using Round Robin algorithm Program to compile and getting the output was successfully completed.

Ex No: 5

PAGE REPLACEMENTS FIFO

Aim: To write a new program Page Replacement using First in First Out algorithm program in c program.

Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 Step 12 Step 13 Step 14

: Start the program : Define maximum array value : To give the Frame stack Size : To give the total no of inputs : To give the inputs one by one up to total no of inputs : If the input is already found in frame stack value then : To write No Need to Add: The given input is already exists... : Else to add input continue : If the input is getting time frame stack is full then : To write Fault: Adjusting the Queue... then : To remove first input value from the frame stack then add input : To write Front Input is Dequeued then write Dequeued value : To calculate the number of fault : To write total number of Fault : End

Program:
#include<stdio.h> #include<conio.h> void main() { int front=0,rear=-1,q[20],n,l,i=0,fault=0,a,k; clrscr(); printf("\nPage Replacement - FIFO"); printf("\n-----------------------"); printf("\nEnter the frame size:"); scanf("%d",&l); printf("Enter the Number of Inputs:"); scanf("%d",&n); while(i<n) { printf(" Enter the Input%d : ",i+1); scanf("%d",&a); if(rear<l-1) { for(k=front;k<=front+i;k++) if(q[k]==a) break; if(k<=front+i) {

printf("\nNo Need to Add: The given input is already exists...\n\n"); } else { rear=rear+1; q[rear]=a; } } else { for(k=front;k<front+l;k++) if(q[k]==a) break; if(k<front+l) { printf("\nNo Need to Add: The given input is already exists...\n\n"); } else { printf("\nFault: Adjusting the Queue..."); printf(" Front Input is Dequeued: %d\n\n",q[front]); rear=rear+1; q[rear]=a; fault++; front++; } } i++; } printf("\n----------------------------"); printf("\nTotal Number of Faults is : %d",fault); getch(); }

OUTPUT: Page Replacement - FIFO ----------------------Enter the frame size: 3 Enter the Number of Inputs: 8 Enter the Input1: 1 Enter the Input2: 2 Enter the Input3: 3 Enter the Input4: 1 No Need to Add: The given input is already exists... Enter the Input5: 4 Fault: Adjusting the Queue... Front Input is Dequeued: 1 Enter the Input6: 5 Fault: Adjusting the Queue... Front Input is Dequeued: 2 Enter the Input7: 3 No Need to Add: The given input is already exists... Enter the Input8: 4 No Need to Add: The given input is already exists...

---------------------------Total Number of Faults is: 2

Result: Thus the above Page Replacement using First in First out algorithm Program to compile and run the output was successfully completed.

Ex. No: 6

PAGE REPLACEMENTS - LRU

Aim: To write a new program Page Replacement using Least Recently Used Algorithm in c program.

Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 Step 12 Step 13 : Start the program : Define maximum array value : To give the total no of inputs : To give the inputs one by one up to total no of inputs : To give the Frame Queue size : If queue is not full add input to queue else : check if the input is already found in frame queue value then skip : else check least recently used the value to be replaced then : fault counter to incremented by 1 : To write Queue... then : To calculate the number of fault : To write total number of Fault : End

Program:
#include<stdio.h> struct queue { int s,qu; }q[20]; void main() { int a[20],l,n,i,j,k,x=0,front=0,rear=-1,y=1,qr,fault=0; clrscr(); printf("\nPage Replacement - LRU"); printf("\n----------------------"); printf("\nEnter the number of Inputs:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the Input%d:",i+1); scanf("%d",&a[i]); } printf("\nEnter the frame size:"); scanf("%d",&l); while(x<n) { if(rear>=l-1) {

for(j=front;j<=rear;j++) if(q[j].qu==a[x]) break; if(j>rear) { for(i=front;i<=rear;i++) q[i].s=0; while(y<l) { for(k=front;k<=rear;k++) { if((q[k].qu==a[x-y])&&(q[k].s==0)) { q[k].s=1; continue; } if(q[k].s==0) qr=k; } y++; } y=1; q[qr].qu=a[x]; fault++; } } else { for(j=front;j<=rear;j++) if(q[j].qu==a[x]) break; if(j>rear) q[++rear].qu=a[x]; } printf("\nQueue:\t"); for(j=front;j<=rear;j++) printf("%d\t",q[j].qu); if(x<n-1) { printf("Next Input: %d",a[x+1]); printf("\tMost Used:"); for(i=x-l+2;i<=x;i++) { if(x>=l-1) printf("\t%d",a[i]); } } x++; } Ptintf(\n---------------------); printf("\nThe No of Faults : %d\n",fault); getch(); }

OUTPUT: Page Replacement - LRU ---------------------Enter the number of Inputs: 12 Enter the Input1:2 Enter the Input2:3 Enter the Input3:2 Enter the Input4:1 Enter the Input5:5 Enter the Input6:2 Enter the Input7:4 Enter the Input8:5 Enter the Input9:3 Enter the Input10:2 Enter the Input11:5 Enter the Input12:2 Enter the frame size: 3 Queue: 2 NextInput: 3 MostUsed: Queue: 2 3 NextInput: 2 MostUsed: Queue: 2 3 NextInput: 1 MostUsed: 3 Queue: 2 3 1 NextInput: 5 MostUsed: Queue: 2 5 1 NextInput: 2 MostUsed: Queue: 2 5 1 NextInput: 4 MostUsed: Queue: 2 5 4 NextInput: 5 MostUsed: Queue: 2 5 4 NextInput: 3 MostUsed: Queue: 3 5 4 NextInput: 2 MostUsed: Queue: 3 5 2 NextInput: 5 MostUsed: Queue: 3 5 2 NextInput: 2 MostUsed: Queue: 3 5 2 --------------------The No of Faults: 4

2 2 1 5 2 4 5 3 2

1 5 2 4 5 3 2 5

Result: Thus the above Page Replacement using Least Recently Used Algorithm Program to compile and getting the output was successfully completed.

Ex. No: 7

PAGE REPLACEMENTS - OPTIMAL

Aim: To write a new program Page Replacement using Optimal Algorithm.

Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 Step 12 Step 13 : Start the program : Define maximum array value : To give the total no of inputs : To give the inputs one by one up to total no of inputs : To give the Frame Queue size : If queue is not full add input to queue else : check if the input is already found in frame queue value then skip : else check last inserted value in the queue to be replaced then : fault counter to incremented by 1 : To write Queue... then : To calculate the number of fault : To write total number of Fault : End

Program:
#include<stdio.h> #include<conio.h> struct queue { int qu,s; }q[20]; void main() { int front=0,rear=-1,n,l,a[50],fault=0,i,j,k,qr,y=1,x=0; clrscr(); printf("\nPaging OPTIMAL"); printf("\n--------------"); printf("\nEnter the no of inputs:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter the input %d:",i+1); scanf("%d",&a[i]); } printf("\nEnter the frame size:"); scanf("%d",&l); while(x<n) { if(rear>=l-1)

{ if(x<=n-l) { for(j=front;j<=rear;j++) if(q[j].qu==a[x]) break; if(j>rear) { for(i=front;i<=rear;i++) q[i].s=0; while(y<l) { for(k=front;k<=rear;k++) { if((q[k].qu==a[x+y])&&(q[k].s==0)) { q[k].s=1; continue; } if(q[k].s==0) qr=k; } y++; } y=1; q[qr].qu=a[x]; fault++; } } else { for(j=front;j<=rear;j++) if(q[j].qu==a[x]) break; if(j>rear) { q[rear].qu=a[x]; fault++; } } } else { for(j=front;j<=rear;j++) if(q[j].qu==a[x]) break; if(j>rear) { rear++; q[rear].qu=a[x]; } } printf("\n\n Queue:\t"); for(j=front;j<=rear;j++) printf("%d\t",q[j].qu); printf("Next consecutive terms:\t"); for(i=x+1;i<=x+l;i++) { if(i<n) printf("%d\t",a[i]); } x++; }

printf("\n-----------------------"); printf("\nThe No of Faults is :%d",fault); getch(); } OUTPUT: Paging OPTIMAL -------------Enter the no of inputs: 12 Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter the the the the the the the the the the the the input input input input input input input input input input input input 1:2 2:3 3:2 4:1 5:5 6:2 7:4 8:5 9:3 10:2 11:5 12:2

Enter the frame size: 3 Queue: 2 Next consecutive terms: 3 Queue: 2 3 Next consecutive terms: Queue: 2 3 Next consecutive terms: Queue: 2 3 1 Next consecutive Queue: 2 3 5 Next consecutive Queue: 2 3 5 Next consecutive Queue: 4 3 5 Next consecutive Queue: 4 3 5 Next consecutive Queue: 4 3 5 Next consecutive Queue: 4 2 5 Next consecutive Queue: 4 2 5 Next consecutive Queue: 4 2 5 Next consecutive ----------------------The No of Faults is: 3 2 2 1 terms: terms: terms: terms: terms: terms: terms: terms: terms: 1 1 5 5 2 4 5 3 2 5 2

5 2 2 4 5 3 2 5 2

4 5 3 2 5 2

Result: Thus the above Page Replacement using Optimal Algorithm Program to compile and getting the output was successfully completed.

Ex. No: 8

DEAD LOCK DETECTION

Aim: To write a new program Dead Lock detection using c program. Algorithm: Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 Step 10 Step 11 Step 12 Step 13 Step 14 Step 14 Program:
#include<stdio.h>; #include<conio.h>; void main() { int found,flag,l,p[4][5],tp,c[4][5],i,j, k=1,m[5],r[5],a[5],temp[5],sum=0; clrscr(); printf("DEAD LOCK - DETECTION"); printf("\n---------------------"); printf("\nEnter total no of processes : "); scanf("%d",&tp); printf("Enter Claim Matrix\n"); for(i=1;i<=4;i++) { for(j=1;j<=5;j++) { scanf("%d",&c[i][j]); } } printf("\nEnter Allocation Matrix\n"); for(i=1;i<=4;i++) { for(j=1;j<=5;j++) { scanf("%d",&p[i][j]); }

: Start the program : Define maximum array value : To set flag=null : To give the total no of process : To give the claim matrix : To give the allocation matrix : To give the resource vector : To give the available vector : Increment the sum value based on allocation matrix : If sum=null add m[i] then increment k by 1 : If i!=m[l] then set flag=1to check c[i][j]>temp[j] then flag=null : Else flag=1 then m[k]=I then k is incremented by 1 : To do temp[j]+=p[i][j] steps then count no of causing process : To write total number of causing process : End

} printf("\nEnter Resource Vector : "); for(i=1;i<=5;i++) { scanf("%d",&r[i]); } printf("Enter Available Vector : "); for(i=1;i<=5;i++) { scanf("%d",&a[i]); temp[i]=a[i]; } for(i=1;i<=4;i++) { sum=0; for(j=1;j<=5;j++) { sum+=p[i][j]; } if(sum==0) { m[k]=i; k++; } } for(i=1;i<=4;i++) { for(l=1;l<k;l++) if(i!=m[l]) { flag=1; for(j=1;j<=5;j++) if(c[i][j]>temp[j]) { flag=0; break; } } if(flag==1) { m[k]=i; k++; for(j=1;j<=5;j++) temp[j]+=p[i][j]; } } printf("\n----------------------------------"); printf("\nDeadlock Causing Processes are : "); for(j=1;j<=tp;j++) { found=0; for(i=1;i<k;i++) { if(j==m[i]) found=1; } if(found==0) printf("%d\t",j); } getch(); }

OUTPUT: DEAD LOCK - DETECTION --------------------Enter total no of processes : 4 Enter Claim Matrix 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 Enter 1 0 1 1 1 0 0 0 0 0 0 0 Allocation Matrix 1 0 0 0 1 0 0 0

Enter Resource Vector : 2 1 1 2 1 Enter Available Vector : 0 0 0 0 1 ---------------------------------Deadlock Causing Processes are : 1

Result: Thus the above Page Replacement using Dead Lock detection Program to compile and getting the output was successfully completed.

Вам также может понравиться