Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.0 kB
9
Indexable
Never
#include<stdio.h>

struct Process
{
    int turntime,arrtime,waittime,bursttime,pid,priority;
    
};

void prioritysorting(struct Process p[],int n)
{
    int i,j;
    struct Process temp;
    int counter = p[0].bursttime;
    
    for(i=1;i<n;i++){
    for(j=i;j<n;j++)
    {
        if(counter>=p[j].arrtime && p[j].priority>p[j+1].priority) 
        {
            temp = p[j];
            p[j]= p[j+1];
            p[j+1]=temp;
        }
        }}
}
void WTtime(struct Process p[],int n)
{
    int i;
    int comptime = p[0].bursttime;
    p[0].turntime = p[0].bursttime;
    
    for(i=1;i<n;i++)
    {
        p[i].waittime = comptime-p[i].arrtime;
        comptime+=p[i].bursttime;
        p[i].turntime = comptime;
    } 
}

void sorting(struct Process p[], int n) 
{
    int i,j;
    struct Process temp;
    for(i=0;i<n;i++){
    for(j=0;j<n-1;j++){
        if(p[j].arrtime>p[j+1].arrtime)
          {
            temp=p[j];
            p[j]= p[j+1];
            p[j+1]=temp;
          }
       }
       
     }  	
}

void main()
{
    struct Process p[5];
    int i,n;
    float avgwait = 0,avgturn=0;
    
    printf("Enter the number of process\n");
    scanf("%d",&n);
    printf("Enter the Process Id, Arrival Time, Burst Time,Priority\n");
    for(i=0;i<n;i++)
    {
    	scanf("%d%d%d%d",&p[i].pid,&p[i].arrtime,&p[i].bursttime,&p[i].priority);
    }
    sorting(p,n);	
    
    prioritysorting(p,n);
    WTtime(p,n);
   
    printf("The Result is\n");
    printf("Process id\tArrivalTime\tBurstTime\tPriority\tWaitingTime\tTurnaroundTime\n");
      for(i=0;i<n;i++)
    {
         printf("p[%d]\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",p[i].pid,p[i].arrtime,p[i].bursttime,p[i].priority,p[i].waittime,p[i].turntime );
    }
    
    for(i=0;i<n;i++)
    {
         avgwait += p[i].waittime;
         avgturn+= p[i].turntime ;
    }
    avgwait = avgwait/n;
    avgturn = avgturn/n;
    printf("The average Waiting Time = %f\n The average Turnaround Time = %f\n",avgwait,avgturn);
    
}
Leave a Comment