C program – SJF CPU Scheduling Algorithm

by Nideesh C on April 17, 2011 · 0 comments

in C Programming




#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();
}

OUTPUT:
 enter no of processes: 5

 enter process1 name: aaa
 enter process time: 4
 enter process2 name: bbb
 enter process time: 3
 enter process3 name: ccc
 enter process time: 2
 enter process4 name: ddd
 enter process time: 5
 enter process5 name: eee
 enter process time: 1

p_name  P_time  w_time

eee	1	0
 ccc	2	1
 bbb	3	3
 aaa	4	6
 ddd	5	10
 total waiting time=20
 avg waiting time=4.00



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program – Shortest Job Next
  2. Sample – Program to enter an integer and print its total value based on the formula ‘x – 1/3!x3 + 1/5!x5 – 1/7!x7 + 1/9!x9′.
  3. C program – Priority Scheduling
  4. C progrma – First Come First Serve Algorithm
  5. C program to read N names, store them in the form of an array and sort them in alphabetical order. Output the give names and the sorted names in two columns side by side with suitable heading

Leave a Comment

Previous post:

Next post: