| Sr. No. |
NAME OF PROGRAM | DATE | REMARK | ||
| 1. | Write a program to Implement FCFS scheduling | | | ||
| 2. | Write a program to Implement Round Robin sheduling | | | ||
| 3. | Write a program to implement shortest job first sheduling | | | ||
| 4. | Write a program to implement bankers algorithm | | | ||
| 5. | Write a program to implement Reader/Writer problem | | | ||
| 6. | Write a program to implement page replacement using LRU | | | ||
| 7. | Write a program to implement page replacement using FIFO | | | ||
| 8. | Write a program to implement Priority sheduling | | |
Faculty’s Signature
1.Program to implement FCFS scheduling.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
char p[10][5];
int tot=0,wt[10],i,n;
float avg=0;
clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+et[i-1];
tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t w_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
}
2.Program to implement ROUND ROBIN scheduling.
#include<stdio.h>#include<conio.h>#include<process.h>#include<string.h>void main(){char p[10][5];int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m;float avgwt;clrscr();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;for(i=0;i<m;i++){printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);}printf("\ntotal waiting time %d\n",totwt);printf("total avgtime %f",avgwt);}3.Program to implement Shortest Job First scheduling.
#include<stdio.h>#include<conio.h>#include<process.h>void main(){char p[10][5],temp[5];int tot=0,wt[10],pt[10],i,j,n,temp1;float avg=0;clrscr();printf("enter no of processes:");scanf("%d",&n);for(i=0;i<n;i++){printf("enter process%d name:\n",i+1);scanf("%s",&p[i]);printf("enter process time");scanf("%d",&pt[i]);}for(i=0;i<n-1;i++){for(j=i+1;j<n;j++){if(pt[i]>pt[j]){temp1=pt[i];pt[i]=pt[j];pt[j]=temp1;strcpy(temp,p[i]);strcpy(p[i],p[j]);strcpy(p[j],temp);}}}wt[0]=0;for(i=1;i<n;i++){wt[i]=wt[i-1]+et[i-1];tot=tot+wt[i];}avg=(float)tot/n;printf("p_name\t P_time\t w_time\n");for(i=0;i<n;i++)printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);printf("total waiting time=%d\n avg waiting time=%f",tot,avg);getch();}3.Program to implement Priority scheduling.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}
4.Program to implement Banker’s algorithm.
#include< stdio.h >
#include< conio.h >
void main()
{
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
clrscr();
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf("Enter the no of processes:\n");
scanf("%d",&p);
printf("Enter the no of resources:\n");
scanf("%d",&r);
printf("Enter the claim for each process:");
for(i=1;i<=p;i++)
{
printf("\nFor process %d",i);
for(j=1;j<=r;j++)
#include< conio.h >
void main()
{
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
clrscr();
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf("Enter the no of processes:\n");
scanf("%d",&p);
printf("Enter the no of resources:\n");
scanf("%d",&r);
printf("Enter the claim for each process:");
for(i=1;i<=p;i++)
{
printf("\nFor process %d",i);
for(j=1;j<=r;j++)
{
scanf("%d",&clm[i][j]);
}
}
printf("Enter the allocation for each process:\n");
for(i=1;i<=p;i++)
{
printf("\nFor process ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter total no of each resource:");
for(j=1;j<=r;j++)
scanf("%d",&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
}
do
{
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
req[i][j]=clm[i][j]-alloc[i][j];}
}
printf("\n\nAvailable resorces is:");
for(j=1;j<=r;j++)
{ printf(" ",avail[j]); }
printf("\nClaim matrix:\t\tAllocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{
printf("%d",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j]
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\nProcess ",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
} }
while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
5.Program to implement Reader / Writer problem.
#include<stdio.h>#include<pthread.h> pthread_mutex_t m; void* writer(void*);void* reader(void*); FILE *fp; void main(){ pthread_t tid1,tid2,tid3; pthread_mutex_init(&m,NULL); pthread_create(&tid2,NULL,reader,NULL); pthread_create(&tid1,NULL,writer,NULL); pthread_create(&tid3,NULL,reader,NULL); pthread_join(tid2,NULL); pthread_join(tid1,NULL); pthread_join(tid3,NULL);}void* writer(void *str){ pthread_mutex_lock(&m); fp=fopen("file1.txt","w"); global += 35; fprintf(fp,"%d",100); fclose(fp); pthread_mutex_unlock(&m);}void* reader(void *param){ int abc; pthread_mutex_lock(&m); fp=fopen("file1.txt","r"); fscanf(fp,"%d",&abc); printf("%d",global); fclose(fp); pthread_mutex_unlock(&m);}6.Program to implement page replacement using LRU .
#include<stdio.h>#include<conio.h>int fr[3];void main(){ void display(); int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3]; int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3; clrscr(); for(i=0;i<3;i++) { fr[i]=-1; } for(j=0;j<12;j++) { flag1=0; flag2=0; for(i=0;i<3;i++) { if(fr[i]==p[j]) { flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<3;i++) { if(fr[i]==-1) { fr[i]=p[j]; flag2=1; break; } } } if(flag2==0) { for(i=0;i<3;i++) lg[i]=0; for(i=0;i<frsize;i++) { for(k=j+1;k<12;k++) { if(fr[i]==p[k]) { lg[i]=k-j; break; } } } found=0; for(i=0;i<frsize;i++) { if(lg[i]==0) { index=i; found=1; break; } } if(found==0) { max=lg[0]; index=0; for(i=1;i<frsize;i++) { if(max<lg[i]) { max=lg[i]; index=i; } } } fr[index]=p[j]; pf++; } display();} printf("\n no of page faults:%d",pf); getch();}void display(){int i;printf("\n");for(i=0;i<3;i++)printf("\t%d",fr[i]);}7.Program to implement page replacement using FIFO .
#include<conio.h>
#include<stdio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,ct=0,co=0,pf=0,list[30],arr[10],i,j,l;
clrscr();
printf("\n enter no fo processors:");
scanf("%d",&a);
printf("\n enter the series:");
for(i=0;i
scanf("%d",&list[i]);
printf("\n enter the no of frames:");
scanf("%d",&b);
for(i=0;i
arr[i]=0;
for(i=0;i
{
co=0;
if(ct==b)
ct=0;
for(int j=0;j
{
if(arr[j]==list[i])
{
int a,b,ct=0,co=0,pf=0,list[30],arr[10],i,j,l;
clrscr();
printf("\n enter no fo processors:");
scanf("%d",&a);
printf("\n enter the series:");
for(i=0;i
scanf("%d",&list[i]);
printf("\n enter the no of frames:");
scanf("%d",&b);
for(i=0;i
arr[i]=0;
for(i=0;i
{
co=0;
if(ct==b)
ct=0;
for(int j=0;j
{
if(arr[j]==list[i])
{
co=1;
break;
}
}
if(co==0)
{
arr[ct]=list[i];
pf++;
ct++;
printf("\nf");
}
for(int l=0;l
printf("%d",arr[l]);
printf("\n");
}
printf("\n no of page faults are : %d",pf);
getch();
}
co=1;
break;
}
}
if(co==0)
{
arr[ct]=list[i];
pf++;
ct++;
printf("\nf");
}
for(int l=0;l
printf("%d",arr[l]);
printf("\n");
}
printf("\n no of page faults are : %d",pf);
getch();
}
No comments:
Post a Comment