Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shortest Job Next Example
Author Message
LouArnold Offline
Junior Member
**

Posts: 3
Joined: Jul 2013
Reputation: 0
Post: #1
Shortest Job Next Example
0
0
Shortest Job Next, also known as Shortest Job First or Shortest Process Next is a scheduling algorithm that selects the waiting process with the smallest execution time to execute next. Shortest Job Next is a non-preemptive algorithm. Shortest remaining time is a preemptive variant of Shortest Job Next.
Code:
#include<stdio.h>

struct proc
{
    int id;
    int arrival;
    int burst;
    int start;
    int rem;
    int wait;
    int finish;
    int turnaround;
    float ratio;
}process[10];

int no;

main()
{
    int i, j, k, time = 0, n;
    struct proc temp;
    int chkprocess(int);
    int nextprocess();


    printf("---SHORTEST JOB NEXT---");
    printf("\n Enter the number of processes: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++)
    {
        process[i].id = i;

        printf("\n\nEnter the arrival time for process %d: ", i);
        scanf("%d", &(process[i].arrival));

        printf("Enter the burst time for process %d: ", i);
        scanf("%d", &(process[i].burst));

        process[i].rem = process[i].burst;

    }    

    for(i = 1; i <= n; i++)
    {
        for(j = i + 1; j <= n; j++)
        {
            if(process[i].arrival > process[j].arrival)
            {
                temp = process[i];
                process[i] = process[j];
                process[j] = temp;
            }
        }
    }

    no = 0;
    j = 1;
    process[1].start = process[1].arrival;

    while(chkprocess(n) == 1)
    {    
        if(process[no + 1].arrival == time)
            no++;

        if(process[j].rem != 0)
        {
            process[j].rem--;    
            for(i = 1; i <= no; i++)
            {
                if(i != j && process[i].rem != 0)
                    process[i].wait++;
            }
        }
        else
        {

            process[j].finish = time;
            j = nextprocess();
            time--;                
            process[j].start = time + 1;
        }        

        time++;
    }
    process[j].finish = time;

    printf("\n\n\t\t\t---SHORTEST JOB FIRST---");
    printf("\n\n Process  Arrival  Burst  Starting  Waiting  Finishing turnaround  Tr/Tb \n");
    printf("%5s %9s %7s %7s %10s %8s %9s\n\n", "id", "time", "time", "time", "time", "time", "time");


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

        process[i].turnaround = process[i].wait + process[i].burst;
        process[i].ratio = (float)process[i].turnaround / (float)process[i].burst;

        printf("%5d %8d %7d %7d %8d %10d %9d %10.1f ", process[i].id, process[i].arrival,
                          process[i].burst, process[i].start,
                          process[i].wait, process[i].finish,
                          process[i].turnaround, process[i].ratio);

        printf("\n\n");
    }

}


int chkprocess(int s)
{
    int i;
    for(i = 1; i <= s; i++)
    {
        if(process[i].rem != 0)
            return 1;
    }
    return 0;
}

int nextprocess()
{
    int min, l, i;
    min = 32000;
    for(i = 1; i <= no; i++)
    {
        if(process[i].rem != 0 && process[i].burst < min)
        {
            min = process[i].burst;
            l = i;
        }
    }
    return l;
}
07-14-2013 03:04 PM
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)