Labels

C program for non-preemptive SJF CPU scheduling algorithm

Introduction to non-preemptive SJF scheduling :

                     A different approach to CPU scheduling is the shortest-job-first(SJF) scheduling algorithm. This algorithm associates with each process the length of the process’s next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie. Note that a more appropriate term for this scheduling method would be the shortest-next CPU-burst algorithm, because scheduling depends on the length of the next CPU burst of a process,rather than its total length.Non-preemptive execution means the execution of a process will not be deferred at any time.



C program for non-preemptive SJF scheduling :


#include<stdio.h>
int main()
{
            int at[10],bt[10],pr[10];
            int n,i,j,temp,time=0,count,over=0,sum_wait=0,sum_turnaround=0,start;
            float avgwait,avgturn;
            printf("Enter the number of processes\n");
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                        printf("Enter the arrival time and execution time for process %d\n",i+1);
                        scanf("%d%d",&at[i],&bt[i]);
                        pr[i]=i+1;
            }
            for(i=0;i<n-1;i++)
            {
                        for(j=i+1;j<n;j++)
                        {
                                    if(at[i]>at[j])
                                    {
                                                temp=at[i];
                                                at[i]=at[j];
                                                at[j]=temp;
                                                temp=bt[i];
                                                bt[i]=bt[j];
                                                bt[j]=temp;
                                                temp=pr[i];
                                                pr[i]=pr[j];
                                                pr[j]=temp;     
                                    }
                        
                        }
            }
            printf("\n\nProcess\t|Arrival time\t|Execution time\t|Start time\t|End time\t|waiting                                     time\t|Turnaround time\n\n");
            while(over<n)
            {
                        count=0;
                        for(i=over;i<n;i++)
                        {
                                    if(at[i]<=time)
                                    count++;
                                    else
                                    break;
                        }
                        if(count>1)
                        {
                                    for(i=over;i<over+count-1;i++)
                                    {
                                                for(j=i+1;j<over+count;j++)
                                                {
                                                            if(bt[i]>bt[j])
                                                            {
                                                                        temp=at[i];
                                                                        at[i]=at[j];
                                                                        at[j]=temp;
                                                                        temp=bt[i];
                                                                        bt[i]=bt[j];
                                                                        bt[j]=temp;
                                                                        temp=pr[i];
                                                                        pr[i]=pr[j];
                                                                        pr[j]=temp;     
                                                            }
                                                }                     
                                    }
                        }
                        start=time;
                        time+=bt[over];
                         printf("p[%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\n",pr[over],
                                    at[over],bt[over],start,time,time-at[over]-bt[over],time-at[over]);
                        sum_wait+=time-at[over]-bt[over];


                        sum_turnaround+=time-at[over];
                        over++;
            }
            avgwait=(float)sum_wait/(float)n;
            avgturn=(float)sum_turnaround/(float)n;
            printf("Average waiting time is %f\n",avgwait);
            printf("Average turnaround time is %f\n",avgturn);
            return 0;
}   



OUTPUT :








1 comment: