SJF simulation

Hi,... I need to simulate the SJF and get the average waiting time for preemptive and non-preemptive SJF. And then i have to modify the program to get the random arrival of the jobs and then find their average waiting time for 100 unit time.

my code is as below:
/* Scheduling Simulation*/

#include
#include
#include
#include

/* Process Data Structure */
struct process {
int pid; /* Process ID */
int burst; /* CPU Burst Time */
int wait; /* Wait Time*/
struct process *next;
};

/* Function Prototype Declarations */
struct process *init_process (int pid, int burst, int wait);
void listprocs (struct process *proc);
void sjf (struct process *proc);

/* Main Program Segment */
int main (void) {
/* Initialize process list */
struct process *plist, *ptmp;
plist = init_process(1, 6, 3);
plist->next = init_process(2, 8, 16); ptmp = plist->next;
ptmp->next = init_process(3, 7, 9); ptmp = ptmp->next;
ptmp->next = init_process(4, 3, 0);

/* Perform simulations */
listprocs(plist);
sjf(plist);

/* Terminate cleanly */
while (plist != NULL) {
ptmp = plist;
plist = plist->next;
free(ptmp);
};
return(0);
};


/* Process list entry initialization routine */
struct process *init_process (int pid, int burst, int wait) {
struct process *proc;
proc = malloc(sizeof(struct process));
if (proc == NULL) {
printf("Fatal error: memory allocation failure.
Terminating.
");
exit(1);
};

proc->pid = pid;
proc->burst = burst;
proc->wait = wait;
proc->next = NULL;
return(proc);
};

/* Process listing */
void listprocs (struct process *proc) {
struct process *tmp = proc;

printf(" Process Listing");
printf("
~~~~~~~~~~~~~~~
");
//printf(" -----------------------------------
");
// printf(" | |");
printf("
PID Burst Wait
");
printf(" --- ----- -----
");

while (tmp != NULL) {
printf(" %d %d %d
", tmp->pid, tmp->burst, tmp->wait);
tmp = tmp->next;
};

//printf(" -----------------------------------
");
// printf("END: Process Listing

");
};

/* Shortest Job First scheduling simulation */
void sjf (struct process *proc) {
int time, start, end, shortest;
int total_wait, tot;
float avg_wait;
struct process *copy, *tmpsrc, *tmp, *beforeshortest;

printf("

Shortest Job First scheduling simulation

");

/* Duplicate process list */
tmpsrc = proc;
copy = tmp = NULL;
while (tmpsrc != NULL) {
if (copy == NULL) {
copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->wait);
tmp = copy;
} else {
tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->wait);
tmp = tmp->next;
};
tmpsrc = tmpsrc->next;
};

/* Main routine */
time = 0;
while (copy != NULL) {
/* Find the next job */
beforeshortest = NULL;
shortest = copy->burst;
tmp = copy->next;
tmpsrc = copy;
while (tmp != NULL) {
if (tmp->burst < shortest) {
shortest = tmp->burst;
beforeshortest = tmpsrc;
};
tmpsrc = tmp;
tmp = tmp->next;
};

/* Process job and remove from copy of process list */
if (beforeshortest == NULL) {
/* Handle first job is shortest case */
start = time;
time += copy->burst;
end = time;
total_wait = tot;
tot += copy->wait;
avg_wait = (float) total_wait/4;

printf("Process: %d End Time: %d Waiting: %d Turnaround: %d
", copy->pid, time, start, end);

tmpsrc = copy;
copy = copy->next;
free(tmpsrc);

} else {
/* Handle first job is not shortest case */
tmp = beforeshortest->next;
start = time;
time += tmp->burst;
end = time;
total_wait = tot;
tot += tmp->wait;
avg_wait = (float) total_wait/4;

printf("Process: %d End Time: %d Waiting: %d Turnaround: %d
", tmp->pid, time, start, end);

beforeshortest->next = tmp->next;
free(tmp);

};

};
printf("
Total wait : %d

", total_wait);

printf("
Average wait : %.2f

", avg_wait);



//printf(" Shortest Job First scheduling simulation

");
};

the above codes is for the non-preemptive SJF. the problem is that i can't seem to get the correct Total and Average waiting time.
The second problem is that i do not know how to code for the preemptive part and then lastly to include random arrival of the jobs..
I have to create a menu and the users can choose either:
1. Non preemptive SJF
2. Preemptive SJF
3. Random arrival of jobs

I have to use linked list..
Hope someone can shed some light...!!
Thanks

Comments

  • hi there, do you have a simulation in SJF using JAVA Programming?
  • : Hi,... I need to simulate the SJF and get the average waiting time for preemptive and non-preemptive SJF. And then i have to modify the program to get the random arrival of the jobs and then find their average waiting time for 100 unit time.
    :
    : my code is as below:
    : /* Scheduling Simulation*/
    :
    : #include
    : #include
    : #include
    : #include
    :
    : /* Process Data Structure */
    : struct process {
    : int pid; /* Process ID */
    : int burst; /* CPU Burst Time */
    : int wait; /* Wait Time*/
    : struct process *next;
    : };
    :
    : /* Function Prototype Declarations */
    : struct process *init_process (int pid, int burst, int wait);
    : void listprocs (struct process *proc);
    : void sjf (struct process *proc);
    :
    : /* Main Program Segment */
    : int main (void) {
    : /* Initialize process list */
    : struct process *plist, *ptmp;
    : plist = init_process(1, 6, 3);
    : plist->next = init_process(2, 8, 16); ptmp = plist->next;
    : ptmp->next = init_process(3, 7, 9); ptmp = ptmp->next;
    : ptmp->next = init_process(4, 3, 0);
    :
    : /* Perform simulations */
    : listprocs(plist);
    : sjf(plist);
    :
    : /* Terminate cleanly */
    : while (plist != NULL) {
    : ptmp = plist;
    : plist = plist->next;
    : free(ptmp);
    : };
    : return(0);
    : };
    :
    :
    : /* Process list entry initialization routine */
    : struct process *init_process (int pid, int burst, int wait) {
    : struct process *proc;
    : proc = malloc(sizeof(struct process));
    : if (proc == NULL) {
    : printf("Fatal error: memory allocation failure.
    Terminating.
    ");
    : exit(1);
    : };
    :
    : proc->pid = pid;
    : proc->burst = burst;
    : proc->wait = wait;
    : proc->next = NULL;
    : return(proc);
    : };
    :
    : /* Process listing */
    : void listprocs (struct process *proc) {
    : struct process *tmp = proc;
    :
    : printf(" Process Listing");
    : printf("
    ~~~~~~~~~~~~~~~
    ");
    : //printf(" -----------------------------------
    ");
    : // printf(" | |");
    : printf("
    PID Burst Wait
    ");
    : printf(" --- ----- -----
    ");
    :
    : while (tmp != NULL) {
    : printf(" %d %d %d
    ", tmp->pid, tmp->burst, tmp->wait);
    : tmp = tmp->next;
    : };
    :
    : //printf(" -----------------------------------
    ");
    : // printf("END: Process Listing

    ");
    : };
    :
    : /* Shortest Job First scheduling simulation */
    : void sjf (struct process *proc) {
    : int time, start, end, shortest;
    : int total_wait, tot;
    : float avg_wait;
    : struct process *copy, *tmpsrc, *tmp, *beforeshortest;
    :
    : printf("

    Shortest Job First scheduling simulation

    ");
    :
    : /* Duplicate process list */
    : tmpsrc = proc;
    : copy = tmp = NULL;
    : while (tmpsrc != NULL) {
    : if (copy == NULL) {
    : copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->wait);
    : tmp = copy;
    : } else {
    : tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->wait);
    : tmp = tmp->next;
    : };
    : tmpsrc = tmpsrc->next;
    : };
    :
    : /* Main routine */
    : time = 0;
    : while (copy != NULL) {
    : /* Find the next job */
    : beforeshortest = NULL;
    : shortest = copy->burst;
    : tmp = copy->next;
    : tmpsrc = copy;
    : while (tmp != NULL) {
    : if (tmp->burst < shortest) {
    : shortest = tmp->burst;
    : beforeshortest = tmpsrc;
    : };
    : tmpsrc = tmp;
    : tmp = tmp->next;
    : };
    :
    : /* Process job and remove from copy of process list */
    : if (beforeshortest == NULL) {
    : /* Handle first job is shortest case */
    : start = time;
    : time += copy->burst;
    : end = time;
    : total_wait = tot;
    : tot += copy->wait;
    : avg_wait = (float) total_wait/4;
    :
    : printf("Process: %d End Time: %d Waiting: %d Turnaround: %d
    ", copy->pid, time, start, end);
    :
    : tmpsrc = copy;
    : copy = copy->next;
    : free(tmpsrc);
    :
    : } else {
    : /* Handle first job is not shortest case */
    : tmp = beforeshortest->next;
    : start = time;
    : time += tmp->burst;
    : end = time;
    : total_wait = tot;
    : tot += tmp->wait;
    : avg_wait = (float) total_wait/4;
    :
    : printf("Process: %d End Time: %d Waiting: %d Turnaround: %d
    ", tmp->pid, time, start, end);
    :
    : beforeshortest->next = tmp->next;
    : free(tmp);
    :
    : };
    :
    : };
    : printf("
    Total wait : %d

    ", total_wait);
    :
    : printf("
    Average wait : %.2f

    ", avg_wait);
    :
    :
    :
    : //printf(" Shortest Job First scheduling simulation

    ");
    : };
    :
    : the above codes is for the non-preemptive SJF. the problem is that i can't seem to get the correct Total and Average waiting time.
    : The second problem is that i do not know how to code for the preemptive part and then lastly to include random arrival of the jobs..
    : I have to create a menu and the users can choose either:
    : 1. Non preemptive SJF
    : 2. Preemptive SJF
    : 3. Random arrival of jobs
    :
    : I have to use linked list..
    : Hope someone can shed some light...!!
    : Thanks
    :



    this program cannot convert void * to process * in line
    : proc = malloc(sizeof(struct process)); this is the problem


  • : this program cannot convert void * to process * in line
    : : proc = malloc(sizeof(struct process)); this is the problem
    :

    only if you compile it as a c++ program (with .cpp or .cc file extension). C compilers do not require typecast.

    Any reason you dug up this old thread???
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories