0% found this document useful (0 votes)
347 views12 pages

Write A Program For SJF When Arrival Time Is Given

The first document provides code to implement Shortest Job First (SJF) scheduling without preemption. It takes process burst times and IDs as input, sorts by burst time, calculates waiting times and turnaround times, and outputs scheduling order, average waiting time, and average turnaround time. The second document provides code to implement priority scheduling with preemption. It takes process IDs, burst times and priorities as input, sorts by priority, calculates waiting times and turnaround times, and outputs scheduling details. The third document provides code for a round robin scheduler. It takes processes and burst times as input, uses a time quantum to schedule in a round robin fashion, and outputs waiting times, turn

Uploaded by

Anshul Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
347 views12 pages

Write A Program For SJF When Arrival Time Is Given

The first document provides code to implement Shortest Job First (SJF) scheduling without preemption. It takes process burst times and IDs as input, sorts by burst time, calculates waiting times and turnaround times, and outputs scheduling order, average waiting time, and average turnaround time. The second document provides code to implement priority scheduling with preemption. It takes process IDs, burst times and priorities as input, sorts by priority, calculates waiting times and turnaround times, and outputs scheduling details. The third document provides code for a round robin scheduler. It takes processes and burst times as input, uses a time quantum to schedule in a round robin fashion, and outputs waiting times, turn

Uploaded by

Anshul Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 12

WRITE A PROGRAM FOR SJF WHEN ARRIVAL

TIME IS GIVEN.

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,temp,n,j,t,np[10],cbt[10],wt[10],tat[10];
int ttat=0,twt=0;
float avgwt,avgtat;
clrscr();
printf("Enter the number of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name of process and its cpu burst time ");
scanf("%d\t%d",&np[i],&cbt[i]);
}
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1);j++)
{
if(cbt[j]>cbt[j+1])
{
temp=cbt[j];
cbt[j]=cbt[j+1];
cbt[j+1]=temp;
t=np[j];
np[j]=np[j+1];
np[j+1]=t;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+cbt[i-1];
twt=twt+wt[i];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+cbt[i];
ttat=ttat+tat[i];
}
avgwt=(twt/(float)n);
avgtat=(ttat/(float)n);
printf("name of process waiting time turn around time");
for(i=0;i<n;i++)
{
printf("\np%d %d %d",np[i],wt[i],tat[i]);
}
printf("\nscheduling of processes is as follows ");
for(i=0;i<n;i++)
{
printf("p%d ",np[i]);
}
printf("\nAverage waiting time is=%f",avgwt);
printf("\nAverage turn around time is=%f",avgtat);
getch();
}
/***********OUTPUT**********
Enter the number of processes 5
Enter name of process and its cpu burst time 1 10
Enter name of process and its cpu burst time 2 1
Enter name of process and its cpu burst time 3 2
Enter name of process and its cpu burst time 4 1
Enter name of process and its cpu burst time 5 5
name of process waiting time turn around time
p2 0 1
p4 1 2
p3 2 4
p5 4 9
p1 9 19
scheduling of processes is as follows p2 p4 p3 p5 p1
Average waiting time is=3.200000 Average turn around time
is=7.000000
****/

CALCULATE WAITING TIME AND TURN AROUD


TIME USING PRIORITY ALGORITHM(PRE-
EMPTIVE).

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[100],p[100],wt[100],pr[100],temp,t,sum1=0,sum=0,tat[100];
float awt,n,atat;
clrscr();
printf("\nenter no. of processes:");
scanf("%f",&n);
for(i=0;i<n;i++)
{
printf("\nenter process no.");
scanf("%d",&a[i]);
printf("\nenter c.p.u burst time:");
scanf("%d",&p[i]);
printf("\nenter priority:");
scanf("%d",&pr[i]);
}
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1);j++)
{
if(pr[j]>pr[j+1])
{
temp=pr[j];
pr[j]=pr[j+1];
pr[j+1]=temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
printf("\nprocess no.\tc.p.u burst time\tpriority");
for(i=0;i<n;i++)
printf("\n%d\t%d\t%d",a[i],p[i],pr[i]);
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+p[i-1];
sum=sum+wt[i];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+p[i];
sum1=sum1+tat[i];
}
atat=(sum1/(float)n);
awt=(sum/(float)n);
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1);j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
t=wt[j];
wt[j]=wt[j+1];
wt[j+1]=t;
}
}
}
printf("\nprocess waiting time");
for(i=0;i<n;i++)
{
printf("\n %d %d",a[i],wt[i]);
}
printf("\naverage waiting time is %f",awt);
printf("\naverage turn around time is %f",atat);
getch();
}

/*****************OUTPUT *******************/
enter no. of processes:4
enter process no.1
enter c.p.u burst time:5
enter priority:3
enter process no.2
enter c.p.u burst time:9
enter priority:1
enter process no.3
enter c.p.u burst time:5
enter priority:4
enter process no.4
enter c.p.u burst time:6
enter priority:3

process no. c.p.u burst time priority


2 9 1
1 5 3
4 6 3
3 5 4
process waiting time
1 9
2 0
3 20
4 14
average waiting time is 10.750000
average turn around time is 17.000000 //
ROUND ROBIN SCHEDULING

#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int
p[MAX],b[MAX],wait[MAX],count[MAX],flag[MAX],remain[MAX],tat[
MAX];
int i,j,n,t,finish=0,time=0;
float avgw=0,avgt=0;
clrscr();
printf("\n Enter the number of processes : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i]=i;
printf("\n Enter the Burst Time of Process- %d : ",i);
scanf("%d",&b[i]);
flag[i]=1;
wait[i]=0;
count[i]=0;
remain[i]=b[i];
time+=b[i];
}
printf("\n Enter the time quantum : ");
scanf("%d",&t);
while(finish!=time)
{
for(i=1;i<=n;i++)
{
if(flag[i])
{
if(remain[i]<=t)
{
wait[i]=finish-count[i]*t;
finish+=remain[i];
flag[i]=0;
remain[i]=0;
}
else
{
wait[i]=finish-count[i]*t;
finish+=t;
remain[i]-=t;
}
;
count[i]++;
}
}
}
for(i=1;i<=n;i++)
{
printf("\nwaiting time is: %d", wait[i]);
avgw+=wait[i];
tat[i]=b[i]+wait[i];
printf("\n turn around time is: %d" , tat[i]);
avgt+=tat[i];
}
avgw/=n;
printf("\n Average waiting time is : %f",avgw);
avgt/=n;
printf("\n average turn around time is : %f",avgt);
getch();
}
//*******************OUTPUT***********************
Enter the number of processes : 3
Enter the Burst Time of Process-1 : 24
Enter the Burst Time of Process-2 : 3
Enter the Burst Time of Process-3 : 3
Enter the time quantum : 4
waiting time is:6
turn around time is:30
waiting time is:4
turn around time is:7
waiting time is:7
turn around time is:10
Average waiting time is : 5.666667
average turn around time is : 15.666667 *//

PRODUCER CONSUMER PROBLEM

#include<stdio.h>
#include<conio.h>
#define BUFFER 10
void producer();
void consumer();
int count=0;
void main()
{
int f;
clrscr();
while(1)
{
printf("\n SELECT '1' FOR PRODUCE\n SELECT '2' FOR
CONSUMER\n SELECT ANY KEY FOR EXIT ");
scanf("%d",&f);
switch(f)
{
case 1: producer();
break;
case 2: consumer();
break;
default : exit();
getch();
}

}
}
void producer()
{
if(count<BUFFER)
{
count++;
printf(" PRODUCER HAS PRODUCED 1-UNIT.\n");
}
else
{
printf(" CAN NOT PRODUCE 'BUFFER' IS FULL.\n");
}
return;
}

void consumer()
{
if(count>0)
{
count--;
printf(" CONSUMER HAS CONSUMED 1-UNIT.\n");
}
else
{
printf(" IT IS NOTHING TO CONSUME.\n");
}
return;
}
//**********************OUTPUT************************
SELECT '1' FOR PRODUCE
SELECT '2' FOR CONSUMER
SELECT ANY KEY FOR EXIT 1
PRODUCER HAS PRODUCED 1-UNIT.

SELECT '1' FOR PRODUCE


SELECT '2' FOR CONSUMER
SELECT ANY KEY FOR EXIT 2
CONSUMER HAS CONSUMED 1-UNIT.

SELECT '1' FOR PRODUCE


SELECT '2' FOR CONSUMER
SELECT ANY KEY FOR EXIT 2
IT IS NOTHING TO CONSUME.

SELECT '1' FOR PRODUCE


SELECT '2' FOR CONSUMER
SELECT ANY KEY FOR EXIT 3 *//

You might also like