DEV Community

loading...

Program for SJF without arrival time

bharathboga profile image Bharath Chandra Boga Updated on ・3 min read

(NEWBIE ALERT)

Shortest Job First(SJF) without arrival time Scheduling is a non-preemptive CPU scheduling algorithm.

Non preemptive - The process will not be unallocated or it will not leave CPU until it's been completely executed.

CPU Scheduling algorithm involves which process is to be given control of CPU for the execution of process.

Processes are rearranged according to their burst times in ascending order and then allocated to CPU.

Average waiting time is reduced when compared to FCFS.

Burst times of each process is to be known earlier.

First, I have defined a macro with name ARRIVAL_TIME and assigned it zero. Since all processes arrive at t=0ms.

#define ARRIVAL_TIME 0
Enter fullscreen mode Exit fullscreen mode

Then I have defined a structure with name task (synonym of process :p) with process id(pid) and burst time as variables of integer data type.

struct task
{
    int pid;
    int burst_time;
};
Enter fullscreen mode Exit fullscreen mode

limit is a variable of integer data type used to store the total number of processes that are waiting to be executed.

int limit;
Enter fullscreen mode Exit fullscreen mode

process is a pointer of type struct task to allocate memory dynamically based on the total number of processes.

struct task *process;
Enter fullscreen mode Exit fullscreen mode

sort() is a function essential for SJF. It rearranges the processes based on their burst times in ascending order. It uses insertion sort. Insertion sort is very useful when volume of data is small.

void sort()
{
    int i,j;
    struct task key;
    for(i=1;i<limit;i++)
    {
        key=process[i];
        j=i-1;
        while(j>=0 && process[j].burst_time > key.burst_time)
        {
            process[j+1]=process[j];
            j--;
        }
        process[j+1]=key;
    }
}
Enter fullscreen mode Exit fullscreen mode

pline (like print a line :p) function is just for table formation.

void pline()
{
    for(int i=0;i<81;i++)
    printf("-");
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

wt(int pin) function calculates the total waiting time of a process. Waiting time of the process is the amount of time that the process has spent in ready queue before its execution or it is also the sum of burst times of all previous processes minus the arrival time of the process(in sjf without arrival time, it is zero).

int wt(int pin)
{
    int waiting_time=0;
    for(int i=0;i<pin;i++)
    waiting_time+=process[i].burst_time;
    return waiting_time;   
}
Enter fullscreen mode Exit fullscreen mode

tat(int pin) function calculates the turnaround time of a process. Turnaround time of a process is the amount of time spent in ready queue(waiting time) and in CPU being executed(burst time).

int tat(int pin)
{
    return wt(pin)+process[pin].burst_time;
}
Enter fullscreen mode Exit fullscreen mode

Main function takes all the input and outputs the table accordingly.
It also calculates total waiting time and total turnaround time to find an average.

A good CPU scheduling algorithm should have
Minimum waiting time and Minimum turnaround time.

#include<stdio.h>
#include<stdlib.h>
#define ARRIVAL_TIME 0
struct task
{
    int pid;
    int burst_time;
};

int limit;
struct task *process;
void sort();
void pline();
int wt(int pin);
int tat(int pin);
void main()
{
    int total_wt=0;
    int total_tat=0;
    printf("Enter Number of Processes: ");
    scanf("%d",&limit);

    process=(struct task*)malloc(sizeof(struct task)*limit);
    printf("\nEnter Burst Times:\n");
    for(int i=0;i<limit;i++)
    {
        printf("For Process ID %d: ",i+1);
        scanf("%d",&process[i].burst_time);
        process[i].pid=i+1;
    }

    sort();

    pline();
    printf("|%-15s|%-15s|%-15s|%-15s|%-15s|\n","Process ID","Burst Time","Arrival Time","Waiting Time","Turnaround Time");
    pline();
    for(int i=0;i<limit;i++,pline())
    {
        printf("|%-15d|%-15d|%-15d|%-15d|%-15d|\n",process[i].pid,process[i].burst_time,ARRIVAL_TIME,wt(i),tat(i));
        total_wt+=wt(i);
        total_tat+=tat(i);
    }

    printf("Average Waiting Time: %.2f ms\n",total_wt/(float)limit);
    printf("Average Turnaround Time: %.2f ms\n",total_tat/(float)limit);
}
void sort()
{
    int i,j;
    struct task key;
    for(i=1;i<limit;i++)
    {
        key=process[i];
        j=i-1;
        while(j>=0 && process[j].burst_time > key.burst_time)
        {
            process[j+1]=process[j];
            j--;
        }
        process[j+1]=key;
    }
}
void pline()
{
    for(int i=0;i<81;i++)
    printf("-");
    printf("\n");
}
int wt(int pin)
{
    int waiting_time=0;
    for(int i=0;i<pin;i++)
    waiting_time+=process[i].burst_time;
    return waiting_time;   
}
int tat(int pin)
{
    return wt(pin)+process[pin].burst_time;
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app