Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You can use any programming language whatever you like.. Please explain your sol

ID: 3850038 • Letter: Y

Question

You can use any programming language whatever you like.. Please explain your solution..

Write a program that simulates performance of different scheduling algorithms in a multi processor system. Inputs to the simulation program are the number of seconds to simulate, the number of processors in the system and the scheduling algorithm to use. Assume that the system is not preemptive, that is, when one a job is assigned to a processor it executes until it finishes and further assume that jobs are never blocked. Treat each second as a discrete event. A single new job arrives at each single second during the given simulation duration. When a job arrives, assign its required execution time as a random integer between 1 and 10 seconds. An arriving job is placed in a waiting queue. At every discrete event (that is every second), if there are processors available, remove jobs from the queue and assign them to processors. Use the following scheduling rules to decide which job to remove: First Come First Served, Shortest Job First and Longest Job First. If the queue is not empty or there are still jobs running at the end of the given simulation limit, your program should wait until all the jobs are executed. At the end of the simulation, output the average waiting time for the jobs for each scheduling algorithm. A high level simulation algorithm can be stated as: for each second from 0 to maximum simulation time generate a new job add the job to the queue while there are available processors remove a job from the queue according to the scheduling rule assign it to a processor end while increment waiting times of jobs in the queue decrement the remaining execution times of the jobs being executed end for

Explanation / Answer

Solution:-

(1) This is a FCFS scheduling algorithm simulation using C language.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>

#include<string.h>

#include<conio.h>

main()

{

    char pn[10][10],t[10];

    int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;

    int totwt=0,tottat=0;

//clrscr();

    printf("Enter the number of processes:");

    scanf("%d",&n);

    for(i=0; i<n; i++)

    {

        printf("Enter the ProcessName, Arrival Time& Burst Time:");

        scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);

    }

    for(i=0; i<n; i++)

    {

        for(j=0; j<n; j++)

        {

            if(arr[i]<arr[j])

            {

                temp=arr[i];

                arr[i]=arr[j];

                arr[j]=temp;

                temp=bur[i];

                bur[i]=bur[j];

                bur[j]=temp;

                strcpy(t,pn[i]);

                strcpy(pn[i],pn[j]);

                strcpy(pn[j],t);

            }

        }

    }

    for(i=0; i<n; i++)

    {

        if(i==0)

            star[i]=arr[i];

        else

            star[i]=finish[i-1];

        wt[i]=star[i]-arr[i];

        finish[i]=star[i]+bur[i];

        tat[i]=finish[i]-arr[i];

    }

    printf(" PName Arrtime Burtime WaitTime Start TAT Finish");

    for(i=0; i<n; i++)

    {

printf(" %s %3d %3d %3d %3d %6d %6d",pn[i],arr[i],bur [i],wt[i],star[i],tat[i],finish[i]);

        totwt+=wt[i];

        tottat+=tat[i];

    }

    printf(" Average Waiting time:%f",(float)totwt/n);

    printf(" Average Turn Around Time:%f",(float)tottat/n);

    getch();

    return 0;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(2) Now we simulate the shortest job first using C language.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>

void main()

{

    int bt[10],p[10],wt[10],tat[10],i,j,n,total=0,pos,temp;

    float avg_wt,avg_tat;

    printf("Enter number of process:");

    scanf("%d",&n);

    printf(" Enter Burst Time: ");

    for(i=0;i<n;i++)

    {

        printf("p%d:",i+1);

        scanf("%d",&bt[i]);

        p[i]=i+1;           //contains process number

    }

    //sorting burst time in ascending order using selection sort

    for(i=0;i<n;i++)

    {

        pos=i;

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

        {

            if(bt[j]<bt[pos])

                pos=j;

        }

        temp=bt[i];

        bt[i]=bt[pos];

        bt[pos]=temp;

        temp=p[i];

        p[i]=p[pos];

        p[pos]=temp;

    }

    wt[0]=0;            //waiting time for first process will be zero

    //calculate waiting time

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

    {

        wt[i]=0;

        for(j=0;j<i;j++)

            wt[i]+=bt[j];

        total+=wt[i];

    }

    avg_wt=(float)total/n;      //average waiting time

    total=0;

    printf(" Process     Burst Time     Waiting Time Turnaround Time");

    for(i=0;i<n;i++)

    {

        tat[i]=bt[i]+wt[i];     //calculate turnaround time

        total+=tat[i];

        printf(" p%d   %d     %d %d",p[i],bt[i],wt[i],tat[i]);

    }

    avg_tat=(float)total/n;     //average turnaround time

    printf(" Average Waiting Time=%f",avg_wt);

    printf(" Average Turnaround Time=%f ",avg_tat);

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(3) Now we simulate the longest Job First algorithm using C language. This is something different approach on C language to implement LJF.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
typedef unsigned int time;
typedef struct job job;
typedef struct job {
int id;
time arrival;
time process;
job * next;
} job;
job * prompt( const char *p, int id ) {
time arrival, process;
job * result;
for ( ; printf( "%s for J%d: ", p, id ); printf( "Re-try. " ) ) {
int c;
char ln[200];
if ( fgets( ln, sizeof(ln), stdin ) == 0 ) return 0;
if ( sscanf( ln, " %n", &c ) == 0 && c >= 0 && ln[c] == '' ) return 0;
if ( sscanf( ln, "%u %u %n", &arrival, &process, &c ) == 2 && c > 0 && ln[c] == '' ) break;
}
result= (job *) malloc( sizeof( job ) );
if ( result != 0 ) {
result->arrival= arrival;
result->process= process;
result->id= id;
result->next= 0;
}
return result;
}
job * sort( job * h, time clk ) {
time c;
job *p, *q, **r, **s;
for ( c= 0, p= h, r= &h; p != NULL; ) {
for ( s= &h; (q= *s) != p; s= &q->next )
if ( p->arrival <= clk ) { if ( p->process > q->process ) break; }
else if ( p->arrival < q->arrival ||
(p->arrival == q->arrival && p->process > q->process) ) break;
if ( q != p )
*s= p, *r= p->next, p->next= q, p= *r;
else
r= &p->next, p= p->next;
}
return h;
}
void print( const job *h ) {
if ( h != 0 ) {
printf( "J%d: Arrival = %u, Process = %u ", h->id, h->arrival, h->process );
print( h->next );
}
}
job * destroy( job *h ) {
if ( h != 0 ) {
destroy( h->next );
free( h );
}
return 0;
}
int main( void ) {
int i;
time c;
job *list= 0, *j, **x;
for( i= 1; (j= prompt( "Enter arrival and process times", i )) != 0; ++i )
j->next= list, list= j;
for ( c= 0, x= &list; *x != 0; x= &(*x)->next )
c += (*x= sort( *x, c ))->process;
print( list );
list= destroy( list );
return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote