0% found this document useful (0 votes)
77 views21 pages

Geethanjali College of Engineering and Technology

The document contains C program code implementations of four CPU scheduling algorithms: first come first served, shortest job first, priority scheduling, and round robin. For each algorithm, the code is provided to calculate arrival times, service times, start times, finish times, waiting times, turnaround times, and average waiting time. Sample inputs and outputs are also displayed for each algorithm.

Uploaded by

kavithachenna
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views21 pages

Geethanjali College of Engineering and Technology

The document contains C program code implementations of four CPU scheduling algorithms: first come first served, shortest job first, priority scheduling, and round robin. For each algorithm, the code is provided to calculate arrival times, service times, start times, finish times, waiting times, turnaround times, and average waiting time. Sample inputs and outputs are also displayed for each algorithm.

Uploaded by

kavithachenna
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

Geethanjali College of Engineering and Technology

Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

1) Write a C Program to Implement First Come First Served Scheduling Algorithm

#include<stdio.h> #include<conio.h> #define max 20 void main() { int arrival_time[max]; int service_time[max]; int start_time[max]; int finish_time[max]; int turnaround_time[max]; float normalized_turnaround_time[max]; int num_of_jobs,j; float avg_turn=0.0; printf(Enter no. of jobs\n); scanf(%d, &Num_of_jobs); for9j=0;j<num_of_jobs;j++) { printf(Enter arrival_time of J[%d]:, j+1); scanf(%d,&arrival_time[j]);
1

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

printf(Enter service_time of j[%d]:, j+1); scanf(%d,&service_time[j]); } for(j=0; j<num_of_jobs; j++) { If(0>j-i) start_time[j]=0; else start_time[j]=finish_time[j-1]; finish_time[j]=start_time[j]+service_time[j]; turn_around_time[j]= finish_time[j]-arrival_time[j]; normalized_turnaround_time[j]=(float)turnaround_time[j]/service_ time[j]; avg_turn+=turnaround_time[j]; } clrscr(); for(j=0; j<num_of_jobs; j++) { if(j{%d}\t, j+1) printf(%d\t, arrival_time[j]); printf(%d\t, service_time[j]); printf(%d\t, start_time[j]);
2

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

printf(%d\t, finish_time[j]); printf(%d\t, turnaround_time[j]); printf(%d\t, normalized_turnaround_time[j]); } }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

Output: Enter number of jobs : 2 Enter arrival_time of j[1] : 1 Enter service_time of j[1] : 1 Enter arrival_time of j[2] : 1 Enter service_time of j[2] : 4

1 2 0 2 1 0.500000 3 4 2 6 3 0.750000

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

2) Write a C Program To Implement the Shortest Job First Scheduling Algorithm.

#include<stdio.h> #include<conio.h> #define max 20 void main() { int arrival_time[max]; int arrival_time1[max]; int service_time[max]; int start_time[max]; int finish_time[max]; int turnaround_time[max]; float normalized_turnaround_time[max]; int num_of_jobs,num_of_job,j; float avg_turn=0.0; int process_time=0; clrscr();
5

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

printf("enter the no of jobs"); scanf("%d",&num_of_jobs); num_of_job=num_of_jobs; for(j=0;j<num_of_jobs;j++) { printf("enter the arrival time of j[%d]:",j+1); scanf("%d",&arrival_time[j]); arrival_time[j]=arrival_time[j]; printf("enter the service time of j[%d]",j+1); scanf("%d",&service_time[j]); } while(num_of_job>1) { for(j=0;j<num_of_jobs;j++) { if(arrival_time[j]>=process_time&&arrival_time[j]!=-1) { if(0>j-1) start_time[j]=0; else start_time[j]=process_time;
6

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

process_time+=service_time[j]; finish_time[j]=process_time; turnaround_time[j]=finish_time[j]-arrival_time[j]; normalized_turnaround_time[j]=(float)turnaround_time[j]/service_ time[j]; avg_turn+=turnaround_time[j]; arrival_time[j]=-1; num_of_job--; }} if(num_of_job==0) break; } printf("name arrival service start finish turnaround n-t \n"); for(j=0;j<num_of_jobs;j++) { printf("j[%d]:\t",j+1); printf("%d \t",arrival_time[j]); printf("%d \t",service_time[j]); printf("%d \t",start_time[j]); printf("%d \t",finish_time[j]); printf("%d \t",turnaround_time[j]); printf("%d \t",normalized_turnaround_time[j]);
7

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

getch(); } }

Output: Enter the number of jobs : 2 Enter the arrival time of j[1] : 2 Enter the service time of j[1] : 3 Enter the arrival time of j[2] : 5 Enter the service time of j[2] : 9

Name Arrivaltime Servicetime Starttime Finishtime Turnaroundtime n-t j[1] j[2] 5 2 0.3333 9 3 12 7 3 0 3 1

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

3) Write a C Program to Implement Priority Scheduling Algorithm

#include<stdio.h> #include<conio.h> struct job { char name; int a,b,s,w,f,v,t,p; }x[5]; void starttime(); void finishtime(); void waitingtime(); void turnaroundtime();
9

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

void sort(int); int q[5],k=0; void main() { int i; clrscr(); for(i=0;i<4;i++) { printf("enter process name,arrival time,burst time,priority for %d job",i+1); scanf("%s %d %d %d",&x[i].name,&x[i].a,&x[i].b,&x[i].p); } starttime(); finishtime(); waitingtime(); turnaroundtime(); printf("process time,arrival time,burst time,priority time,start time,finish time,waiting time,turn around time \n"); for(i=0;i<4;i++) printf("%c\t %d\t %d\t %d\t %d\t %d\t %d \t %d\t \n" ,x[i].name,x[i].a,x[i].b,x[i].p,x[i].s,x[i].f,x[i].w,x[i].t); getch(); }
10

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

void finishtime() { int i; for(i=0;i<4;i++) x[i].f=x[i].b+x[i].s; } void waitingtime() { int i; for(i=0;i<4;i++) { x[i].w=x[i].s-x[i].a; }} void turnaroundtime() { int i; for(i=0;i<4;i++) x[i].t=x[i].w+x[i].b; } void starttime() {
11

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

int i,j,m; q[k]=0; x[k].s=x[k].a; x[k].v=1; for(i=0;i<4;i++) { sort(i); j=q[i+1]; m=q[i]; }} void sort(int c) { int i,j,m,temp; m=x[q[c]].s+x[q[c]].b; for(i=c;i<4;i++) { if(x[i].a<m &&x[i].v==0) { q[temp+k]=1; x[i].v=1; }}
12

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

for(i=c+1;i<=k;i++) for(j=1;j<=k;j++) if(x[q[i]].p>x[q[i]].p) { temp=q[i]; q[i]=q[j]; q[j]=temp; } getch(); }

Output:

13

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

4) Write a C Program To Implement Round Robin Scheduling Algorithm.

#include<stdio.h> #include<conio.h> struct job { char name; int a,b,s,t,w,f,v,tl ,e,p;


14

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

}x[5]; int q[20],k=0,n,front,rear,time; void times(); void main() { int i,j; clrscr(); for(i=0;i<3;i++) { printf("Enter process name arrival time,burst time, for %d",i+1); scanf("%s%d%d",&x[i].name,&x[j].a,&x[i].b); x[i].tl=x[i].b; x[i].e=0; } printf("Enter the stomp"); scanf("%d",&n); times(); printf("process, arrival time, burst time, start time, finish time, waiting time, turn around time\n"); for(i=0;i<3;i++) printf("%c\t %d\t %d\t %d\t %d\t %d\n", x[i].name,x[i].a,x[i].b,x[i].s,x[i].f,x[i].w,x[i].t);
15

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

getch(); } void times() { int i,j; q[k]=0; x[0].e=1; while(front<=rear) { if(x[q[front]].v==0) { x[q[front]].s=time; x[q[front]].v=1; x[q[front]].w=x[q[front]].s-x[q[front]].a; } else { x[q[front]].w=x[q[front]].w+time-x[q[front]].p; } if(x[front].tl<n) {
16

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

time+=x[q[front]].tl; x[q[front]].tl=0; } else { x[q[front]].tl-=n; time+=n; } for(j=0;j<=3;j++) { if(x[j].a=time&&x[j].e==0) { q[++k]=j; rear++; x[j].e=1; } } if(x[q[front]].tl==0) x[q[front]].f=time; else {
17

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

q[++k]=q[front]; x[q[front]].p=time; rear++; } front++; } for(i=0;i<3;i++) x[i].t=x[i].w+x[i].b; }

Output: Enter process name, arrival time, burst time for job 1 : 166 Enter process name, arrival time, burst time for job 2 :
18

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

266 Enter process name, arrival time, burst time for job 3 : 366 Enter the stamp 2 Process arrivaltime bursttime starttime finishtime workingtime turnaroundtime 1 2 16 3 13 0 14 0 0 6 6 6 0 2 4 14 16 18 8 10 6

19

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

20

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District.

Roll No: 09R11F0018

21

You might also like