answersLogoWhite

0


Best Answer

#include #include #include 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+1); scanf("%s",&p[i]); printf("enter process time"); scanf("%d",&pt[i]); } for(i=0;ipt[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\t P_time\t w_time\n"); for(i=0;i\t%d\t%d\n",p[i],et[i],wt[i]); printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

This is perfectly executing code.

#include<stdio.h>

int n, start;

struct process

{

int id;

int at,st,et,bt,tat,wt,flag,rt,priority;

}p[10],temp,q[10];

void display();

void fcfs();

void sjf();

void sjfpre();

void priority();

void prioritypre();

void main()

{

int i,j,min,u,ch;

printf("\nEnter the total no. of processes:");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\n enter the process id:");

scanf("%d",&q[i].id);

printf("\n enter process arrival time:");

scanf("%d",&q[i].at);

printf("\n enter process burst time:");

scanf("%d",&q[i].bt);

printf("\n enter the priority of the process:");

scanf("%d",&q[i].priority);

q[i].flag=0;

q[i].rt=q[i].bt;

}

do

{

for(i=0;i<n;i++)

{

p[i]=q[i];

}

min=999;

for(i=0;i<n;i++)

{

if(p[i].at<min)

{

min=p[i].at;

u=i;

}

// p[i].rt=p[u].bt;

}

start=p[u].at;

printf("\n1. fcfs");

printf("\n2.sjf");

printf("\n3.sjfpre");

printf("\n4.priority");

printf("\n5.prioritypre");

printf("\n6 exit");

printf("\n enter your choice:");

scanf("%d",&ch);

switch(ch)

{

case 1: fcfs();

break;

case 2: sjf();

break;

case 3: sjfpre();

break;

case 4: priority();

break;

case 5: prioritypre();

break;

// case 6: exit 0;

// break;

}

if(ch!=6)

display();

}while(ch!=6);

}

void display()

{

float turn=0, wait=0;

int i;

printf("\nprocess\tarrivaltime\tbursttime\tstarttime\tendtime\tpriority\twaitingtime\tturnaroundtime");

for(i=0;i<n;i++)

{

p[i].tat=p[i].et-p[i].at;

p[i].wt=p[i].tat-p[i].bt;

wait=wait + p[i].wt;

turn=turn + p[i].tat;

printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",p[i].id,p[i].at,p[i].bt,p[i].st,p[i].et,p[i].priority,p[i].wt,p[i].tat);

}

wait=wait/n;

turn=turn/n;

printf("\nAverage waiting time is: %f",wait);

printf("\nAverage turnaround time is: %f",turn);

}

void fcfs()

{

int i,j;

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

{

if(p[i].at>p[j].at)

{

temp=p[i];

p[i]=p[j];

p[j]=temp;

}

}

start=p[0].at;

for(i=0;i<n;i++)

{

if(start<p[i].at)

start=p[i].at;

p[i].st=start;

p[i].et=start + p[i].bt;

start=p[i].et;

}

}

void sjf()

{

int i,j,min,u;

for(j=0;j<n;j++)

{

min=999;

for(i=0;i<n;i++)

if(p[i].at<=start && p[i].bt < min && p[i].flag==0)

{

min=p[i].bt;

u=i;

}

if(min==999)

{

for(i=0;i<n;i++)

if(p[i].at<min && p[i].flag==0)

{

min=p[i].at;

u=i;

}

start=p[u].at;

}

p[u].st=start;

p[u].et=start + p[u].bt;

p[u].flag=1;

start=p[u].et;

}

}

void sjfpre()

{

int i,j=0,min,u;

while(j<n)

{

min=999;

for(i=0;i<n;i++)

if(p[i].at<=start && p[i].rt<min && p[i].flag==0)

{

min=p[i].rt;

u=i;

}

if(min==999)

{

for(i=0;i<n;i++)

if(p[i].at<min && p[i].flag==0)

{

min=p[i].at;

u=i;

}

start=p[u].at;

}

if(p[u].bt==p[i].rt)

p[u].st=start;

p[u].rt=p[u].rt-1;

start=start+1;

if(p[u].rt==0)

{

p[u].et=start;

p[u].flag=1;

j++;

}

}

}

void priority()

{

int i,j,min,u;

for(j=0;j<n;j++)

{

min=999;

for(i=0;i<n;i++)

if(p[i].at<=start && p[i].priority < min && p[i].flag==0)

{

min=p[i].priority;

u=i;

}

if(min==999)

{

for(i=0;i<n;i++)

if(p[i].at<min && p[i].flag==0)

{

min=p[i].at;

u=i;

}

start=p[u].at;

}

p[u].st=start;

p[u].et=start + p[u].bt;

p[u].flag=1;

start=p[u].et;

}

}

void prioritypre()

{

int i,j=0,min,u;

while(j<n)

{

min=999;

for(i=0;i<n;i++)

if(p[i].at<=start && p[i].priority<min && p[i].flag==0)

{

min=p[i].priority;

u=i;

}

if(min==999)

{

for(i=0;i<n;i++)

if(p[i].at<min && p[i].flag==0)

{

min=p[i].at;

u=i;

}

start=p[u].at;

}

if(p[u].bt==p[i].rt)

p[u].st=start;

p[u].rt=p[u].rt-1;

start=start+1;

if(p[u].rt==0)

{

p[u].et=start;

p[u].flag=1;

j++;

}

}

}

code written by : Fabianski Benjamin

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program on shortest-job-first scheduling algorithm?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Can you give a C program about SJF algorithm?

no.


Program in c to develop an algorithm that prints the n value of algorithm?

reymond rillera reymond rillera


How does algorithm and c plus plus program get along?

They are bosom-friends.


C program algorithm for simple interest using while loop?

Not used


Can someone provide the C program for Prim's algorithm?

The C code for Prim's algorithm can be found in the following link. https://sites.google.com/site/itstudentjunction/lab-programming-solutions/data-structures-programs/program-to-find-minimal-spanning-tree-using--prims-algorithm


What is solution in c program?

The program itself is the solution. All programs are a solution to a given problem; that's the entire point of writing a program, to solve a problem. The program's algorithm specifies how the problem is solved and it's the programmer's job to convert that algorithm into working code.


How do you RSA algorithm c?

Perform encryption on the following PT using RSA and find the CT p = 3; q = 11; M = 5


Write a c program to find smallest among three using function with pointers?

Please visit http://talentsealed.blogspot.com/2009/10/to-find-smallest-among-three-using.htmlfor answer.


Does every algorithm have a c program?

Yes. More generally, every algorithm (defined as a sequence of finite steps to solve a problem that can be easily understood by a human) can be converted into machine code such that the algorithm can be understood by a machine. The C programming language is just one such method of converting algorithms into working machine code.


Algorithms to the given c plus plus programs?

The question is impossible to answer. You cannot determine an algorithm from a given program if the program is not actually given in the question. Please place the program in the discussion section.


What is disk scheduling in operating system?

why we use disk scheduling in c ?