os lab file c programs

28
Program 1 Write a C program to implement First-Come-First-Serve Algorithm. #include<stdio.h> #include<conio.h> void main() { clrscr(); int pr,i,t=0,avg=0; printf("Enter The Number Of Process\n"); scanf("%d",&pr); char p[10]; int brst[10],Wt[10]; brst[0]=0;Wt[0]=0; for(i=0;i<pr;i++) { printf("Enter The Name Of The Process\n"); scanf("%s",&p[i]); printf("Enter The Burst Time Of The Process\n"); scanf("%d",&brst[i]); } t=Wt[0]; for(i=1;i<pr;i++) { Wt[i]=Wt[i-1]+brst[i-1]; t=t+Wt[i]; } printf("\nProcess\tBurst Time\tWaiting Time\n"); for(i=0;i<pr;i++) { printf("p%d\t%d\t\t%d\t\n",i+1,brst[i],Wt[i]); } avg=t/pr; printf("Total Waiting Time Is=%d\n",t); printf("Average Waiting Time Is=%d\n",avg); getch(); }

Upload: kandarp-tiwari

Post on 15-Jul-2015

318 views

Category:

Science


5 download

TRANSCRIPT

Page 1: Os lab file c programs

Program 1

Write a C program to implement First-Come-First-Serve Algorithm.

#include<stdio.h>

#include<conio.h> void main()

{ clrscr(); int pr,i,t=0,avg=0;

printf("Enter The Number Of Process\n"); scanf("%d",&pr);

char p[10]; int brst[10],Wt[10]; brst[0]=0;Wt[0]=0;

for(i=0;i<pr;i++) {

printf("Enter The Name Of The Process\n"); scanf("%s",&p[i]); printf("Enter The Burst Time Of The Process\n");

scanf("%d",&brst[i]); } t=Wt[0];

for(i=1;i<pr;i++) {

Wt[i]=Wt[i-1]+brst[i-1]; t=t+Wt[i]; }

printf("\nProcess\tBurst Time\tWaiting Time\n"); for(i=0;i<pr;i++)

{ printf("p%d\t%d\t\t%d\t\n",i+1,brst[i],Wt[i]); }

avg=t/pr; printf("Total Waiting Time Is=%d\n",t);

printf("Average Waiting Time Is=%d\n",avg); getch(); }

Page 2: Os lab file c programs
Page 3: Os lab file c programs

Program 2

Write a C program to implement non pre-emptive SJF scheduling algorithm.

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

{ clrscr();

int n,i,j,temp,temp1,temp2,pr[10],bt[10],at[10],wt[10],tat[10]; float atat,awt,totaltat=0,totalwt=0; for(i=0;i<10;i++)

{ bt[i]=0;

wt[i]=0; } printf("enter the number of process");

scanf("%d",&n); for(i=0;i<n;i++) {

pr[i]=i; printf("\nenter the burst time of process p%d\t",i);

scanf("%d",&bt[i]); printf("\nenter the arrival time of process p%d\t",i); scanf("%d",&at[i]);

} for(i=0;i<n;i++)

{ for(j=0;j<n;j++) {

if(at[j]>at[i]&&bt[j]<at[i]) {

temp=bt[i]; bt[i]=bt[j]; bt[j]=temp;

temp1=pr[i]; pr[i]=pr[j];

pr[j]=temp1; temp2=at[i]; at[i]=at[j];

at[j]=temp2; }

} } wt[0]=0;

for(i=0;i<n;i++)

Page 4: Os lab file c programs

{ wt[i+1]=wt[i]+bt[i]-at[i+1];;

tat[i]=wt[i]+bt[i]; }

for(i=0;i<n;i++) { totalwt=totalwt+wt[i];

totaltat=totaltat+tat[i]; }

awt=totalwt/n; atat=totaltat/n; printf("\n\t process \tburt time\t arival time\twaiting time\n");

for(i=0;i<n;i++) {

printf("\t p%d\t\t%d\t\t%d\t\t%d\n",pr[i],bt[i],at[i],wt[i]); } printf("the average waitingtimeis %f\n",awt);

printf("the average turn around time is %f\n",atat); getch();

}

Page 5: Os lab file c programs
Page 6: Os lab file c programs

Program 3

Write a C program to implement non pre-emptive SJF scheduling algorithm.

#include<stdio.h>

#include<conio.h> #include<string.h>

void main() { int i,j,k,n,Bt[10],wt[10],B[10],A[10],Tt=0;

char s[20],start[20]; int max=0,min,Time=0,z[50];

float Twt=0.0,Awt; int w=0,flag=0,t=0; clrscr();

printf("\n Enter no. of processes ::"); scanf("%d",&n);

for(i=1;i<=n;i++) { printf("\n Enter the Burst time for process P%d::",i);

scanf("%d",&Bt[i]); printf("\n Enter the Arrival time for process P%d::",i); scanf("%d",&A[i]);

B[i]=Bt[i]; if(B[i]>max)

max=B[i]; if(A[i]>Time) Time=A[i];

wt[i]=0; s[i]='T';

start[i]='F'; Tt+=B[i]; }

i=1;k=0; z[k]=0;

k++; while(t<Time) {

if(A[i]<=t && B[i]!=0) {

if(flag==0) wt[i]=wt[i]+w; B[i]=B[i]-1;

if(B[i]==0) s[i]='F';

start[i]='T'; t++; w=w+1;

if(s[i]!='F')

Page 7: Os lab file c programs

{ j=1;

flag=1; while(j<=n&&flag!=0)

{ if(s[j]!='F'&&B[i]>B[j]&&A[i]<=t&&i!=j) {

flag=0; z[k]=w;

wt[i]=wt[i]-w; i=j; k++;

} else

flag=1; j++; }

} else

{ i++; j=1;

while(A[j]<=t && j<=n) {

if(B[i]>B[j] && s[j]!='F') { flag=0;

i=j; }

j++; } }

} else

{ if(flag==0) i++;

} }

while(w<Tt) { min=max+1;

i=1; while(i<=n)

{ if(min>B[i]&&s[i]=='T') {

min=B[i]; j=i;

}

Page 8: Os lab file c programs

i++; }

i=j; if(w==Time &&start[i]=='T')

{ w+=B[i]; z[k]=w;

k++; s[i]='F';

} else {

wt[i]+=w; w+=B[i];

z[k]=w; k++; s[i]='F';

} }

printf("\n..............................................\n"); printf("\nPno\tBt\tAt\tWt"); printf("\n..............................................\n");

for(i=1;i<=n;i++) {

wt[i]=wt[i]-A[i]; printf("\nP%d\t%d\t%d\t%d",i,Bt[i],A[i],wt[i]); }

printf("\n..............................................\n"); printf("\n\n Gannt Chart ::\n");

for(i=0;i<k;i++) printf("%4d",z[i]); for(i=1;i<=n;i++)

Twt+=wt[i]; printf("\n\n Total waiting Time ::%f",Twt);

Awt=Twt/n; printf("\n\n Average waiting Time ::%f",Awt); getch();

}

Page 9: Os lab file c programs
Page 10: Os lab file c programs

Program 4

Write a C program to implement priority scheduling algorithm.

#include<stdio.h>

#include<conio.h> void main()

{ int i,j,n,time[10],bursttime[10],wt[10],pt[10],temp2,total=0,p[10],temp; float avgwt,avgtat;

int tat[10],totaltat=0; clrscr();

printf("\nenter the no of process:"); scanf("\t%d",&n); for(i=0;i<n;i++)

{ printf("\nenter the time of process P%d\t",i);

scanf("\n\t%d",&time[i]); printf("\nenter the priority of process p%d\t",i); scanf("\n\t%d",&pt[i]);

} for(i=0;i<n;i++) {

p[i]=i; for(j=0;j<n;j++)

{ if(pt[i]<pt[j]) {

temp=pt[i]; pt[i]=pt[j];

pt[j]=temp; temp=time[i]; time[i]=time[j];

time[j]=temp; temp2=p[i];

p[i]=p[j]; p[j]=temp2; }

} }

wt[0]=0; for(i=1;i<=n;i++) {

wt[i]=wt[i-1]+time[i-1]; printf("\nthe wating time of process%d",p[i-1]);

printf("\t%d",wt[i-1]); } for(i=0;i<n;i++)

{

Page 11: Os lab file c programs

tat[i]=wt[i]+time[i]; }

printf("\nps\tct\tpt\twt\ttat"); for(i=0;i<n;i++)

{ printf("\np%d\t%2d\t%2d\t%2d\t%2d",p[i],time[i],pt[i],wt[i],tat[i]); }

for(i=0;i<n;i++) {

total=total+wt[i]; totaltat=totaltat+tat[i]; }

avgwt=float(total)/n; avgtat=float(totaltat)/n;

printf("\navg waiting time is:\t%f",avgwt); printf("\nAverage turn around time is :%f",avgtat); getch();

}

Page 12: Os lab file c programs
Page 13: Os lab file c programs

Program 5

Write a C program to implement round robin.

#include<stdio.h> #include<conio.h>

void main() { int st[10],bt[10],wt[10],tat[10],n,tq;

int i,count=0,swt=0,stat=0,temp,sq=0; float awt=0.0,atat=0.0;

clrscr(); printf("Enter number of processes:"); scanf("%d",&n);

printf("Enter burst time for sequences:"); for(i=0;i<n;i++)

{ scanf("%d",&bt[i]); st[i]=bt[i];

} printf("Enter time quantum:");

scanf("%d",&tq); while(1) {

for(i=0,count=0;i<n;i++) {

temp=tq; if(st[i]==0) {

count++; continue;

} if(st[i]>tq) st[i]=st[i]-tq;

else if(st[i]>=0)

{ temp=st[i]; st[i]=0;

} sq=sq+temp;

tat[i]=sq; } if(n==count)

break; }

for(i=0;i<n;i++) { wt[i]=tat[i]-bt[i];

swt=swt+wt[i];

Page 14: Os lab file c programs

stat=stat+tat[i]; }

awt=(float)swt/n; atat=(float)stat/n;

printf("\nProcess_no\tBurst time\t Wait time\tTurn around time\n"); for(i=0;i<n;i++) {

printf("\n%d\t\t%d\t\t%d\t\t\t%d",i+1,bt[i],wt[i],tat[i]); }

printf("\nAvg wait time is= %f",awt); printf("\nAvg turn around time is= %f",atat); getch();

}

Page 15: Os lab file c programs
Page 16: Os lab file c programs

Program 6

Write a C program to implement banker’s algorithm. #include<stdio.h> #include<conio.h> void main() { int k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=0; clrscr(); printf("\n Enter the number of resources : "); scanf("%d", &rz); printf("\n enter the max instances of each resources\n"); for(i=0;i<rz;i++) { avail[i]=0; printf("%c= ",(i+97)); scanf("%d",&ins[i]); } printf("\n Enter the number of processes : "); scanf("%d", &pno); printf("\n Enter the allocation matrix \n "); for(i=0;i<rz;i++) printf(" %c",(i+97)); printf("\n"); for(i=0;i <pno;i++) { P[i]=i; printf("P[%d] ",P[i]); for(j=0;j<rz;j++) { scanf("%d",&allocated[i][j]); avail[j]+=allocated[i][j]; } } printf("\nEnter the MAX matrix \n "); for(i=0;i<rz;i++) { printf(" %c",(i+97)); avail[i]=ins[i]-avail[i]; } printf("\n"); for(i=0;i <pno;i++) { printf("P[%d] ",i); for(j=0;j<rz;j++) scanf("%d", &MAX[i][j]); } printf("\n"); A: d=-1; for(i=0;i <pno;i++) { count=0; t=P[i]; for(j=0;j<rz;j++) { need[t][j] = MAX[t][j]-allocated[t][j];

Page 17: Os lab file c programs

if(need[t][j]<=avail[j]) count++; } if(count==rz) { output[k++]=P[i]; for(j=0;j<rz;j++) avail[j]+=allocated[t][j]; } else P[++d]=P[i]; } if(d!=-1) { pno=d+1; goto A; } printf("\t <"); for(i=0;i<k;i++) printf(" P[%d] ",output[i]); printf(">"); getch(); }

Page 18: Os lab file c programs
Page 19: Os lab file c programs

Program 7

Write a C program to implement Producer-Consumer Problem.

#include<stdio.h>

#include<conio.h> #include<stdlib.h>

void display(int c,int stack[]) { int i;

printf("\n----------------------------------------------------------\n"); if(c==0)

printf("\tStack is EMPTY\n\t\t(Now It is sleeping)"); else for(i=1;i<=c;i++)

printf("\t%d",stack[i]); printf("\n----------------------------------------------------------\n");

} void main() {

int ch,n,c1=0,c2=0,produce[23],consume[23]; clrscr(); printf("\n\n\n\n\n\n\n\t\tEnter Stack Size : ",n);

scanf("%d",&n); while(1)

{ clrscr(); printf("\t\tProducer Stack (Stack Size : %d)\n\t\t~~~~~~~~~~~~~~~~~~~~~~",n);

display(c1,produce); printf("\n\n\t\tConsumer Stack (Stack Size : %d)\n\t\t~~~~~~~~~~~~~~~~~~~~~~~",n);

display(c2,consume); printf("\n\t\tCHOICES\n\t\t~~~~\n\t1.Producer\n\t2.Consumer\n\t3.Exit\nEnter your choice : ");

scanf("%d",&ch); switch(ch)

{ case 1: if(c1==n)

printf("Produer stack is FULL.So Producer goes to SLEEP\n"); else

{ c1++; printf("\t\tEnter PRODUCE item is :");

scanf("%d",&produce[c1]); }

break; case 2: if(c2==n)

Page 20: Os lab file c programs

printf("Consumer Stack is FULL.So it goes to SLEEP!.......\n\tReset the Cosumer Stack\n",c2=0);

else if(c1==0) printf("\tProducer stack is EMPTY\n");

else { c2++;

consume[c2]=produce[c1]; printf("\t\tCONSUME one item :");

c1--; } break;

case 3: exit(0);

default: printf("\tIt is Wrong choice,Please enter correct choice!............\n"); }

getch(); }

}

Page 21: Os lab file c programs
Page 22: Os lab file c programs

Program 8

Write a C program to implement Dekker’s Alogorithm.

#include<stdio.h> #include<conio.h> void main() { int choice; int c1=1,c2=1,turn=1; clrscr(); do { printf("\n1.Process 1 Enter"); printf("\n2.Process 2 Enter"); printf("\n3.Both Process Enter"); printf("\n4.Exit"); scanf("%d",&choice); if(choice==1) { printf("\nProcess P1 Enters the Critical section"); c1=1; turn=2; } else printf("\nIt is the turn process P2"); c1=0; while(c2==0) { if(turn==2) c1=1; } while(turn==2); c1=0; if(choice==2) { printf("\nProcess P2 enters in critical section"); c2=1; turn=1; } else printf("\nIt is turn of Process P1"); c2=0; while(c1==0) { if(turn==1) c2=1; } while(turn==1); c2=0; } while(choice!=4); getch();

Page 23: Os lab file c programs
Page 24: Os lab file c programs

Program 9

Write a C program to implement LRU page replacement.

#include<stdio.h>

#include<conio.h> int n,ref[100],fs,frame[100],count=0;

void input(); void show(); void cal();

void main() {

clrscr(); input(); cal();

show(); getch();

} void input() {

int i; printf("Enter no of pages in Refrence String\t"); scanf("%d",&n);

printf("Enter the reference string:"); for(i=0;i<n;i++)

scanf("%d",&ref[i]); printf("Enter the Frame Size\t"); scanf("%d",&fs);

} void cal()

{ int i,j,k=0,c1,c2[100],r,temp[100],t; frame[k]=ref[k];

count++; k++;

for(i=1;i<n;i++) { c1=0;

for(j=0;j<fs;j++) {

if(ref[i]!=frame[j]) c1++; }

if(c1==fs) {

count++; if(k<fs) {

frame[k]=ref[i];

Page 25: Os lab file c programs

k++; }

else {

for(r=0;r<fs;r++) { c2[r]=0;

for(j-1;j<n;j--) {

if(frame[r]!=ref[j]) c2[r]++; else

break; }

} for(r=0;r<fs;r++) temp[r]=c2[r];

for(r=0;r<fs;r++) {

for(j=r;j<fs;j++) { if(temp[r]<temp[j])

{ t=temp[r];

temp[r]=temp[j]; temp[j]=t; }

} }

for(r=0;r<fs;r++) { if(c2[r]==temp[0])

frame[r]=ref[i]; }

} } }

} void show()

{ printf("Page Faults = %d",count); }

Page 26: Os lab file c programs
Page 27: Os lab file c programs

Program 10

Write a C program to implement FIFO page replacement.

#include<stdio.h>

#include<conio.h> void main()

{ int ref[50],i,j,fault=0,count=0,frame[5],n; int temp;

clrscr(); printf("\n Enter the no. of Frames :;");

scanf("%d",&n); printf("\nEnter the reference string and end with -1 ::"); scanf("%d",&temp);

while(temp!=-1) {

ref[count++]=temp; scanf("%d",&temp); }

for(i=0;i<n;i++) frame[i]=-1; for(i=0;i<count;i++)

{ for(j=0;j<n;j++)

if(frame[j]==ref[i]) break; if(j==n)

{ frame[fault%n]=ref[i];

fault++; } printf("\n\n After inserting %2d the Frame status is ::",ref[i]);

for(j=0;j<n;j++) printf("%4d",frame[j]);

} printf("\n\n\t Total no. of page faults ::%d",fault); getch();

}

Page 28: Os lab file c programs