*Various CPU scheduling algorithms implementation using C

language…
*Semaphores are used in some of the programs
1.First Come,First Serve scheduling algorithm with arrival time
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int pid;
int bt;
int at;
int intime;
int fintime;
int wt;
int tt;
};
int main()
{
struct node *ptr;
int temp,n,i,j,timspd=0;
float awt=0,att=0;
printf("Enter the no. of process");
scanf("%d",&n);
ptr=malloc(n*sizeof(struct node));
for(i=0;i<n;i++)
{
printf("\nEnter the process id -- ");
scanf("%d",&ptr[i].pid);
printf("\nEnter the burst time -- ");
scanf("%d",&ptr[i].bt);
printf("\nEnter the arrival time--");
scanf("%d",&ptr[i].at);
}
printf("\nPID\tBurst Time\tArrval Time");

for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",ptr[i].pid,ptr[i].bt,ptr[i].at);
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(ptr[j].at>ptr[j+1].at)
{
temp=ptr[j].at;
ptr[j].at=ptr[j+1].at;
ptr[j+1].at=temp;
temp=ptr[j].bt;
ptr[j].bt=ptr[j+1].bt;
ptr[j+1].bt=temp;
temp=ptr[j].pid;
ptr[j].pid=ptr[j+1].pid;
ptr[j+1].pid=temp;
}
}
}
printf("\n");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",ptr[i].pid,ptr[i].bt,ptr[i].at);

ptr[0].wt=0;
ptr[0].tt=ptr[0].bt;
awt=ptr[0].wt;
att=ptr[0].tt;
ptr[0].intime=ptr[0].at;
ptr[0].fintime=ptr[0].bt;
printf("\n%d %d %d\n",ptr[0].pid,ptr[0].intime,ptr[0].fintime);
for(i=1;i<n;i++)
{
if(ptr[i-1].fintime>=ptr[i].at)

ptr[i].intime=ptr[i-1].fintime;
else
ptr[i].intime=ptr[i].at;
ptr[i].fintime=ptr[i].intime+ptr[i].bt;
ptr[i].wt=ptr[i].intime-ptr[i].at;
ptr[i].tt=ptr[i].fintime-ptr[i].at;
printf("%d %d %d\n",ptr[i].pid,ptr[i].intime,ptr[i].fintime);
awt+=ptr[i].wt;
att+=ptr[i].tt;
}
awt/=n;
att/=n;
printf("\nPID\tWaitingTime\tTurnAround");
//for(i=0;i<n;i++)
//printf("\n%d\t\t%d\t\t%d",ptr[i].pid,ptr[i].wt,ptr[i].tt);
printf("\nAvgwt=%f Avgtt=%f\n",awt,att);

getch();
return 0;
}

2.Shortest Job First with arrival time

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{

int pid;
int bt;
int at;
int intime;
int fintime;
int wt;
int tt;
};
struct node *ptr;
float awt=0,att=0;
int n;
int t;
void sort_arrv()
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(ptr[j].at>ptr[j+1].at)
{
temp=ptr[j].at;
ptr[j].at=ptr[j+1].at;
ptr[j+1].at=temp;
temp=ptr[j].bt;
ptr[j].bt=ptr[j+1].bt;
ptr[j+1].bt=temp;
temp=ptr[j].pid;
ptr[j].pid=ptr[j+1].pid;
ptr[j+1].pid=temp;
}
}
}
}

void sort_burst(int i,int k)
{
int j,t=i,temp;
for(i=0;i<k;i++)
{
for(j=t;j<k-i-1;j++)
{
if(ptr[j].bt>ptr[j+1].bt)
{
temp=ptr[j].at;
ptr[j].at=ptr[j+1].at;
ptr[j+1].at=temp;
temp=ptr[j].bt;
ptr[j].bt=ptr[j+1].bt;
ptr[j+1].bt=temp;
temp=ptr[j].pid;
ptr[j].pid=ptr[j+1].pid;
ptr[j+1].pid=temp;
}
}
}
}
void sjf()
{
int i=0,j,k=1;
for(j=1;k<=n&&ptr[0].at==ptr[j].at;j++,k++);
sort_burst(0,j);
ptr[0].wt=0;
ptr[0].tt=ptr[0].bt;
awt=ptr[0].wt;
att=ptr[0].tt;
ptr[0].intime=ptr[0].at;
ptr[0].fintime=ptr[0].bt;
printf("\n%d %d %d\n",ptr[0].pid,ptr[0].intime,ptr[0].fintime);

for(i=1;i<n;i++)
{
if(k<=n)
{
for(j=i;k<=n&&ptr[i-1].fintime>=ptr[j].at;j++,k++);
sort_burst(i,j);
}

if(ptr[i-1].fintime>=ptr[i].at)
ptr[i].intime=ptr[i-1].fintime;
else
ptr[i].intime=ptr[i].at;
ptr[i].fintime=ptr[i].intime+ptr[i].bt;
ptr[i].wt=ptr[i].intime-ptr[i].at;
ptr[i].tt=ptr[i].fintime-ptr[i].at;
printf("%d %d %d\n",ptr[i].pid,ptr[i].intime,ptr[i].fintime);
awt+=ptr[i].wt;
att+=ptr[i].tt;
}
}

int main()
{
int temp,i,j,timspd=0;

printf("Enter the no. of process");
scanf("%d",&n);
ptr=malloc(n*sizeof(struct node));
for(i=0;i<n;i++)
{

printf("\nEnter the process id -- ");
scanf("%d",&ptr[i].pid);
printf("\nEnter the burst time -- ");
scanf("%d",&ptr[i].bt);
printf("\nEnter the arrival time--");
scanf("%d",&ptr[i].at);
}
printf("\nPID\tBurst Time\tArrval Time");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",ptr[i].pid,ptr[i].bt,ptr[i].at);
sort_arrv();
sjf();
//printf("\n");
// for(i=0;i<n;i++)
// printf("\n%d\t\t%d\t\t%d",ptr[i].pid,ptr[i].bt,ptr[i].at);
//
awt/=n;
att/=n;
printf("\nPID\tWaitingTime\tTurnAround");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",ptr[i].pid,ptr[i].wt,ptr[i].tt);
printf("\nAvgwt=%f Avgtt=%f\n",awt,att);

getch();
return 0;
}

3.Round Robin Algorithm with arrival time

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int pid;
int bt;
int at;
int rtime;
int fintime;
int wt;
int tt;
int intim;
int count;
};
int main()
{
struct node *ptr;
int n,i,j=0,spendtim=0,quantam,seq[100],k=0;
float awt=0,att=0;
printf("Enter the no. of process");
scanf("%d",&n);
ptr=(struct node *)malloc(n*sizeof(struct node));

for(i=0;i<n;i++)
{
printf("\nEnter the process id -- ");
scanf("%d",&ptr[i].pid);
printf("\nEnter the burst time -- ");
scanf("%d",&ptr[i].bt);
printf("\nEnter the arrival time--");
scanf("%d",&ptr[i].at);
}
printf("\nEnter the time quantam");
scanf("%d",&quantam);

printf("\n");
for(i=0;i<n;i++)
{
ptr[i].rtime=ptr[i].bt;
ptr[i].count=-1;
}
printf("\nPID\tBurst Time\tArrval Time");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",ptr[i].pid,ptr[i].bt,ptr[i].at);
printf("\n\n");
for(i=0;j<n;i=(i+1)%n)
{
if(ptr[i].at<=spendtim&&ptr[i].rtime!=0)
{
ptr[i].intim=spendtim;
//printf("\n spend value = %d \n",spendtim);
//printf("\ni value = %d %d\n",i,ptr[i].pid);
seq[k]=ptr[i].pid;
k++;
ptr[i].count+=1;
if(ptr[i].rtime<=quantam)
{
spendtim+=ptr[i].rtime;
ptr[i].rtime=0;
j++;
}
else
{
ptr[i].rtime-=quantam;
spendtim+=quantam;
}
ptr[i].tt=spendtim-ptr[i].at;
}
else if(ptr[i].rtime!=0&&ptr[(i-1)%n].rtime==0)
{

ptr[i].count+=1;
ptr[i].intim=ptr[i].at;
//printf("\n spend value = %d \n",spendtim);
seq[k]=ptr[i].pid;
k++;
if(ptr[i].rtime<=quantam)
{
spendtim=ptr[i].rtime+ptr[i].at;
ptr[i].rtime=0;
j++;
}
else
{
ptr[i].rtime-=quantam;
spendtim=quantam+ptr[i].at;
}
ptr[i].tt=spendtim-ptr[i].at;
}
}
printf("\n\n");
//awt/=n;
//att/=n;
//printf("\nPID\tWaitingTime\tTurnAround");

for(i=0;i<k;i++)
printf("%d ",seq[i]);
printf("\n\n");
for(i=0;i<n;i++)
ptr[i].wt=ptr[i].intim-(ptr[i].count*quantam)-ptr[i].at;
printf("\nPID\tturntim\twttim");
for(i=0;i<n;i++)
printf("\n%d\t%d\t%d",ptr[i].pid,ptr[i].tt,ptr[i].wt);
getch();
return 0;
}

4.Banker's Algorithm

#include<stdio.h>
#include<conio.h>
int n,m,*Avail,**Max,**Allocation,**Need,*Work;

int Check(int i)
{
//printf("\nin check %d\n",i);
int j,count=0;
for(j=0;j<m;j++)
{
if(Need[i][j]<=Work[j])
count++;
}
if(count==m)
return 1;
else
return 0;
}
SafeState()
{
int i,j,count=0,flag=1;
char *Finish;
Work=(int *)malloc(sizeof(int)*m);
Finish=(char *)malloc(sizeof(char)*n);
for(i=0;i<m;i++)
Work[i]=Avail[i];
for(i=0;i<n;i++)
Finish[i]='F';
printf("\nWork Matrix\n");

for(i=0;i<m;i++)
printf("%d ",Work[i]);
printf("\n");
for(i=0;i<n;i++)
printf("%c ",Finish[i]);
printf("\n");
while(flag==1)
{
flag=0;
for(i=0;i<n;i++)
{
if(Finish[i]=='F'&& Check(i))
{
flag=1;
Finish[i]='T';
for(j=0;j<m;j++)
Work[j]+=Allocation[i][j];
}
for(j=0;j<m;j++)
printf("%d ",Work[j]);
printf("\n");
}
printf("\n");
}
for(i=0;i<n;i++)
printf("%c ",Finish[i]);
for(j=0;j<n;j++)
{
if(Finish[j]=='T')
count++;
}
if(count==n)

printf("\n\nSystem in Safe state\n\n");
else
printf("\n\nSystem is not in safe state\n\n");
}

int main()
{
int i,j;
printf("Enter the No. of Process: ");
scanf("%d",&n);
printf("Enter the No. of Resources: ");
scanf("%d",&m);
Avail=(int *)malloc(sizeof(int)*m);
printf("Enter the Available Resources:\n");
for(i=0;i<m;i++)
scanf("%d",&Avail[i]);

Max=(int **)malloc(sizeof(int *)*n);
for(i=0;i<n;i++)
{
Max[i]=(int *)malloc(sizeof(int)*m);
}
for(i=0;i<n;i++)
{
printf("Enter the Max Resources need by process %d:\n",i);
for(j=0;j<m;j++)
scanf("%d",&Max[i][j]);
}
Allocation=(int **)malloc(sizeof(int *)*n);
for(i=0;i<n;i++)
{
Allocation[i]=(int *)malloc(sizeof(int)*m);

}
for(i=0;i<n;i++)
{
printf("Enter the Resource allocate to process %d:\n",i);
for(j=0;j<m;j++)
scanf("%d",&Allocation[i][j]);
}
Need=(int **)malloc(sizeof(int *)*n);
for(i=0;i<n;i++)
{
Need[i]=(int *)malloc(sizeof(int)*m);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
Need[i][j]=Max[i][j]-Allocation[i][j];
}
printf("\nMax Matrix \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",Max[i][j]);
printf("\n");
}
system("cls");
printf("Allocate Matrix \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",Allocation[i][j]);
printf("\n");
}
printf("\nNeed Matrix\n");
for(i=0;i<n;i++)
{

for(j=0;j<m;j++)
printf("%d ",Need[i][j]);
printf("\n");
}
printf("\nAvailable Matrix\n");
for(i=0;i<m;i++)
{
printf("%d ",Avail[i]);
}
SafeState();
getch();
return 0;
}

5. Reader's writer's problem

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
int readcount = 0;
sem_t wrt,mutex;
void * reader();
void * writer();
int main ()
{
pthread_t rthread1,rthread2,rthread3,wthread;
sem_init (&wrt,0,1);
sem_init (&mutex,0,1);
int i= 1;

pthread_create(&rthread1,NULL,reader,(void *) i);
i= 2;
pthread_create(&rthread2,NULL,reader,(void *) i);
i=3;
pthread_create(&rthread3,NULL,reader,(void *) i);
pthread_create(&wthread,NULL,writer,NULL);
pthread_exit(0);
return 0;
}
void * writer ()
{
while (1)
{
printf ("\nwriter wants to write and readcount is %d",readcount);
sem_wait(&wrt);
printf("\nWriter is writing and readcount is : %d ",readcount);
sleep(1);
sem_post(&wrt);
//sleep(1);
}
}
void * reader(void * i)
{
while (1)
{
sem_wait(&mutex);
readcount++;
if (readcount==1)
sem_wait(&wrt);
sem_post(&mutex);
printf("\nReader %d is reading and readcount is %d",i,readcount);
sleep (1);
sem_wait(&mutex);
readcount--;
if (readcount==0)

{ sem_post(&wrt);
printf ("\n now writers can write");
}
sem_post(&mutex);
sleep(1);
}
}

*the remaining algorithms are Producer-Consumer problem and page
fault methods..I'll upload them shortly

Sign up to vote on this title
UsefulNot useful