OUTPUT:
enter no of processes: 5
enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1
p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11
total waiting time=26
avg waiting time=5.20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include<stdio.h> #include<conio.h> void main() { char p[10][5],temp[5]; int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n; float avgwt; clrscr(); printf("enter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:",i+1); scanf("%s",&p[i]); printf("enter process time:"); scanf("%d",&pt[i]); printf("enter priority:"); scanf("%d",&pr[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pr[i]>pr[j]) { temp1=pr[i]; pr[i]=pr[j]; pr[j]=temp1; 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]; totwt=totwt+wt[i]; } avgwt=(float)totwt/n; printf("p_name\t p_time\t priority\t w_time\n"); for(i=0;i<n;i++) { printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[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 priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1
p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11
total waiting time=26
avg waiting time=5.20
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
January 28th, 2010 at 11:03 pm
really thanks a lotttttttt …..
dey r quite helpful to me
July 22nd, 2010 at 8:25 pm
Very Very Thanks
August 11th, 2010 at 10:30 am
Thankyou
September 3rd, 2010 at 1:39 pm
thank u very very much for this solution
October 18th, 2010 at 7:55 am
But WHAT if processes having different arrival time????
March 17th, 2011 at 3:26 pm
its pt[i-1] n’t et[i-1]
March 26th, 2011 at 3:53 pm
very very thanks
May 13th, 2011 at 11:58 pm
//i wrote this better program it considers different arrival times and prints a verbal gannt daigram for priroty scheduling with preemtion without preemtion and with aging any one is welcome to use it just thank me if u like it
#include
#include
typedef struct{
int arrival;
int burst;
int priorty;
int index;
int status;
}process;
void swap(process *x,process *y)
{
process temp;
temp = *x;
*x = *y;
*y = temp;
}
void bublesort(process p[], int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j p[j+1].arrival||(p[j].arrival==p[j+1].arrival&&p[j].priorty>p[j+1].priorty))
swap(&p[j],&p[j+1]);
}
void sortpr(process p[],int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j p[j+1].priorty)
swap(&p[j],&p[j+1]);
}
void deletep(process p[],int size,int index)
{ int i=index;
while(i<size-1){
p[i]=p[i+1];
i++;}
return;
}
bool nonew(process p[],int size)
{ for(int i=0;i<size;i++){
if(p[i].status==0 )return false;
}
return true;
}
bool noready(process p[],int size)
{for(int i=0;i<size;i++)
if(p[i].status==1)
return false;
return true;
}
int ready(process p[],int size)
{ int min=0;
for(int i=0;i<size;i++)
if(p[i].status==1&&p[i].priorty<p[min].priorty)
min=i;
return min;
}
void NonPreemtive(process p[],int size)
{ int i,j,minpr,current=p[0].arrival;
for(i=0;icurrent){
current=p[i+1].arrival;}
else{ j=i+1;
minpr=p[j].priorty;
for(j=i+1;j<size;j++)
{ if(p[j].arrival<current){
if (p[j].priorty<minpr){
minpr=p[j].priorty;
swap(&p[i+1],&p[j]);
}}
}
}
}
}
printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
}
void NonPreemtiveAging(process p[],int size)
{int i,j,minpr,current=p[0].arrival;
for(i=0;icurrent){
current=p[i+1].arrival;}
else{ j=i+1;
minpr=p[j].priorty-(current-p[j].arrival)/5;
for(j=i+1;j<size;j++)
{ if(p[j].arrival<current){
if (p[j].priorty-(current-p[j].arrival)/5<minpr){
minpr=p[j].priorty-(current-p[j].arrival)/5;
swap(&p[i+1],&p[j]);
}
}
}
}
}
}
printf("p%d from %d to %d\n",p[i].index,current,current+p[i].burst);
}
void Preemtive(process p[],int size)
{
int i=0,current=p[0].arrival,j,test;
while(size!=0)
{
printf("p%d from %d to ",p[i].index,current);
if(nonew(p,size)){
printf("%d\n",current+p[i].burst);
current=current+p[i].burst;
deletep(p,size,i);
size–;
if(size==0) break;
i=ready(p,size);
continue;
}
else{test=0;
for(j=0;j<size;j++)
{
if(p[j].arrival<current+p[i].burst)
{ if(p[j].status==1)continue;
if(p[j].priorty<p[i].priorty)
{
printf(" %d\n",p[j].arrival);
p[i].burst=p[i].burst-(p[j].arrival-current);
p[i].status=1;
current=p[j].arrival;
test=1;
i=j;
break;
}else p[j].status=1;
}else if(p[j].arrival==current+p[i].burst)p[j].status=1;
}
if(test==1)continue;
printf("%d\n",current+p[i].burst);
current=current+p[i].burst;
deletep(p,size,i);
size–;
if (!noready(p,size)){
i=ready(p,size);}
else {
current=p[i].arrival;}
}
}
return;
}
void main()
{
process p[5];
int i,c;
printf("Welcome\n");
printf( "Choose the mode of scedueling:\npress 1 for non preemtive\npress 2 for non preemtive with aging\npress 3 for preemptive\n");
scanf("%d",&c);
for (i=0;i<5;i++)
{
printf("\nenter the arrival time of p %d:",i);
scanf("%d",&p[i].arrival);
printf("enter the burst time of p %d:",i);
scanf("%d",&p[i].burst);
printf("enter the priorty of p %d:",i);
scanf("%d",&p[i].priorty);
p[i].status=0;
p[i].index=i;
}
bublesort(p,5);
printf("\nsorted:\nprocess arrival time burst time priorty\n");
for (i=0;i<5;i++)
{
printf( " p%d %d %d %d\n",p[i].index,p[i].arrival,p[i].burst,p[i].priorty);
}
if(c==1)
NonPreemtive(p,5);
else{
if(c==2) NonPreemtiveAging(p,5);
else if (c==3)Preemtive(p,5);
}
getch();
}
August 8th, 2011 at 3:48 pm
thanks a lot….
August 13th, 2011 at 4:44 pm
really very helpful
September 20th, 2011 at 3:32 pm
thank’s ”””””””””’
September 26th, 2011 at 3:50 pm
hw headerfile will come in c program
October 7th, 2011 at 12:54 pm
wow,,,really excellent and easy to understand……tnx
November 4th, 2011 at 3:41 pm
thanks alot realy very helpfull it is
November 4th, 2011 at 3:43 pm
thank u..
November 12th, 2011 at 12:23 am
Thanks
December 1st, 2011 at 4:27 am
Great job… You prevented me from getting a zero on my end of year project. Got this to code but was not up to it. I actually coded the fcfs, sjf, rr and the none-pree-priority in least than a week but got exhausted and was about to call it quits. Then there you were, I owe you one bro!
Will post my code shortly along with the disk scheduling algorithms also……
THANX
December 1st, 2011 at 4:40 am
Check out this fcfs. I think there is a few bugs but nothing you guy can;t resolve….
Let me know the errors you encountered!
//First Come First Serve
#include
#include
#include
#include
#include
//Global Variables
int result;
int quan = 0;
float avg_tat;
float avg_wt;
float total_wt = 0;
float total_tat = 0;
time_t n, later;
//Global Data Structure
struct cProcess
{
int id;
int priority;
int start_time;
int end_time;
int arrive_time;
int burst_time;
int wait_time;
int turn_time;
struct cProcess *next;
};
//Menu and Sub-Menu Function Prototype
void Menu(); //Function that displays menu options
void cpuProcess(); //Function that controll cpu processes
//Function that displays menu options
//Process Function Prototype
void FCFS(); //Function that Perform FCFS Scheduling
/***********************SLEEP**************************/
//Function that pause system for a selected period
void sleep(int delay)
{
n=time(NULL);
later=n+delay;
while(nnul”);
return 0;
} //End Mian Function
/****************MENU TABLE******************************/
//Function Menu That Display GUI
void Menu()
{
char type;
Logo(); //Call logo function to display program logo
printf(“\n\t Select the type of process you require!\n”);
printf(“\t —————————————-\n\n”);
printf(“\t PRESS: [C] – CPU Processes\n”);
printf(“\t [D] – DISK Processes\n\n”);
printf(“\t [E] – Exit & Close Scheduler\n\n”);
printf(“\t SELECT:[ ]\b\b”);
scanf(“%c”,&type);
switch(type)
{
case ‘c’:
case ‘C’: Loading();
cpuProcess();
break;
case ‘d’:
case ‘D’: Loading();
diskProcess();
break;
case ‘e’:
case ‘E’: fflush(stdin);
Exit();
break;
case ‘\n’: printf(“\a\n\t Invalid entry. Press any key to try again…\n”);
getch();
fflush(stdin);
system(“cls”);
Menu();
default: printf(“\a\n\t Invalid entry. Press any key to try again…\n”);
getch();
fflush(stdin);
system(“cls”);
Menu();
}
}//End Meu Function
/******************FCFS Scheduling*************************/
//First come First Server Scheduler
void FCFS()
{
int i=1;
int type=0;
int num, result;
char* name;
retry:;
Logo();
struct cProcess *head;
struct cProcess *node,*ptr;
name = “FCFS”;
printf(“\n\t Schedule Type: %s”, name);
printf(“\n\t ——————-\n”);
printf(“\n\t Enter the number of processes: “);
scanf(“%d”,&num);
result = Error(num);
if(result == 0)
{
printf(“\n\a\t Invalid process entry! Press any key to try again…\n”);
getch();
goto retry;
}
head = (struct cProcess*)malloc(sizeof(struct cProcess));
head->id = i;
printf(“\n\t Enter P%d Arrival Time: “,i);
scanf(“%d”, &head->arrive_time); //Add process arrival time
printf(“\n\t Enter P%d Burst Time: “,i);
scanf(“%d”, &head->burst_time); //Add process bust time
printf(“\n\t Enter P%d Priority: “,i);
scanf(“%d”, &head->priority); //Add process priority number where 1 is the highest or significantly urgent
head->start_time = 0;
head->end_time = head->start_time + head->burst_time;
head->turn_time = head->end_time – head->arrive_time;
head->wait_time = head->start_time – head->arrive_time;
total_wt += head->wait_time;
total_tat += head->turn_time;
head->next = NULL;
ptr = head;
for(i=2;iid = i;
printf(“\n\t Enter P%d Arrival Time: “,i);
scanf(“%d”, &node->arrive_time); //Add process arrival time
printf(“\n\t Enter P%d Burst Time: “,i);
scanf(“%d”, &node->burst_time); //Add process bust time
printf(“\n\t Enter P%d Priority: “,i);
scanf(“%d”, &node->priority); //Add process priority number where 1 is the highest or significantly urgent
node->start_time = ptr->end_time;
node->end_time = node->start_time + node->burst_time;
node->turn_time = node->end_time – node->arrive_time;
node->wait_time = node->start_time – node->arrive_time;
total_wt += node->wait_time;
total_tat += node->turn_time;
node->next = NULL;
ptr->next = node;
ptr = node;
}
result = Check(head); //Call result funtion to compare user input to system requirements
if(result == 0)
{
printf(“\a\n\t Error – At least one process MUST arrive at zero time!”);
printf(“\n\t Press any key to re-enter…”);
getch();
free(head);
FCFS();
}
Sort(head); //Call arrange function to arrange processes in arrival time order
avg_wt = total_wt/num; //Calculate process average wait time
avg_tat = total_tat/num; //Calculate process average turn arounf time
cDisplay(head,name,type); //Call display functon to display process results
} //End FCFS() Function
/*************************DISPLAY***************************/
//Display Function
void cDisplay(struct cProcess *start,char* name,int type)
{
Logo(); //Call Program logo
struct cProcess *temp; //Create pointer of type object
printf(“\t Schedule Type: %s”, name);
if(quan > 0) //Do if condition is satisfied
printf(“\n\t Quantum Slice: %d”,quan); //Show quantum slice for Round Robin scheduler
printf(“\n\t Process Result Table\n”);
printf(“\t ——————–\n\n”);
printf(“\t| ID | Ar.Time | CPU.Time | Start | End | Priority | WT | TAT |”);
printf(“\n\t ————————————————————-\n”);
while(start != NULL)
{
printf(“\t P%d\t %d\t %d\t %d\t %d\t %d\t %d\ %d\n”, start->id,start->arrive_time,start->burst_time,start->start_time,start->end_time,start->priority,start->wait_time,start->turn_time);
temp = start->next; //Set temp pointer to next object inlist
free(start); //Free start memory to prevent memory leak
start = temp; //Set temp pinter to start
sleep(1); //Call sleep function and pause system for 1 second
}
printf(“\n\t Avg Waiting Time: %.1f”, avg_wt);
printf(“\n\t Avg Turn Around Time: %.1f”, avg_tat);
printf(“\n\n\t Table Completed!!!”);
printf(“\n\t Press any key to continue…”);
//Set global values to default
avg_tat = 0;
avg_wt = 0;
total_wt = 0;
total_tat = 0;
quan = 0;
free(start); //Free memory within the list!
free(temp); //Free memory within the list!
getch();
if(type == 0)
cpuProcess(); //Call cpuProcess Menu Function
else
diskProcess(); //Call diskProcess Menu Function
}//End Display() Function
/*********************LOADING*******************************/
//Loading Function
void Loading()
{
int x;
int random;
srand(time(NULL)); //Initialize srand
random = rand()%4 + 3; //Randomize number
printf(“\n\t Please Wait”);
sleep(1);
for(x=0;x=0){
printf(” “);
}
}
sleep(1); //Call sleep function to pause system for 1 second
}//End Loading() function
/*******************Error*****************************/
//Function Error
int Error(int a)
{
int j=1,x=2;
if(a < x || a == '\n'){
j = 0;
}
return j;
}//End Error() Function