Question
Using this code: typedef enum Taskstate{ e_task_ready = 0, e_task_running = 1, e_task_waiting = 2, e_task_done = 3, } etaskstate; static char statechars[] = {'r','R','w',0};
Using this code:
typedef enum Taskstate{
e_task_ready = 0,
e_task_running = 1,
e_task_waiting = 2,
e_task_done = 3,
} etaskstate;
static char statechars[] = {'r','R','w',0};
/* 1) handle state changes:
running process completes cpu burst
running process has quantum expire
io complete
2) do context switch if necessary
both ready
one ready and cpu free
3) append appropriate characters to character arrays
avoid putting in multiple string terminators
*/
/* assume s1 and s2 point to buffers with enough space to hold the result */
/* assume that the int parameters are strictly greater than 0 */
void fcfs(char *s1, char *s2, int quantum, int x1, int y1, int z1, int x2, int y2, int z2) {
int i; /* next string position (time) */
/* start with both ready */
etaskstate state1 = e_task_ready;
etaskstate state2 = e_task_ready;
int cpuleft1 = x1; /* p1 next cpu burst remaining */
int cpuleft2 = x2; /* p2 next cpu burst remaining */
int ioleft1 = y1; /* p1 next io burst remaining, 0 if no more io */
int ioleft2 = y2; /* p2 next io burst remaining, 0 if no more io */
int qleft; /* quantum remaining */
for (i=0; (state1 != e_task_done) || (state2 != e_task_done); i++) {
/* running process completes its cpu burst */
if((state1 == e_task_running) && (cpuleft1== 0)) {
// if process finishes all its cpu-related workload.
// let's check the io of process1
if (ioleft1 == 0) {
state1 = e_task_done;
s1[i] = statechars[state1]; /* terminate the string */
}
else
state1 = e_task_waiting;
}
else if ((state2 == e_task_running) && (cpuleft2 == 0) ) {
if (ioleft2 == 0) {
state2 = e_task_done;
s2[i] = statechars[state2]; /* terminate the string */
}
else
state2 = e_task_waiting;
}
/* running process has quantum expire */
/*
if ((state1 == e_task_running) && (qleft == 0) ) {
...
}
if ((state2 == e_task_running) && (qleft == 0) ) {
...
}*/
/* handle io complete */
if ((state1 == e_task_waiting) && (ioleft1 == 0)) {
state1 = e_task_ready;
cpuleft1 = z1;
}
if ((state2 == e_task_waiting) && (ioleft2 == 0)) {
state2 = e_task_ready;
cpuleft2 = z2;
}
/* if both ready, depends on algorithm */
if ( (state1 == e_task_ready) && (state2 == e_task_ready)) {
state1 = e_task_running;
qleft = quantum;
}
/* handle one ready and cpu available */
else if ( (state1 == e_task_ready) && (state2 != e_task_running)) {
state1 = e_task_running;
qleft = quantum;
}
else if ( (state2 == e_task_ready) && (state1 != e_task_running)) {
state2 = e_task_running;
qleft = quantum;
}
/* insert chars in string, but avoid putting in extra string terminators */
if (state1 != e_task_done)
s1[i] = statechars[state1];
if (state2 != e_task_done)
s2[i] = statechars[state2];
/* decrement counts */
qleft--; /* ok to decrement even if nothing running */
if (state1 == e_task_running)
cpuleft1--;
if (state1 == e_task_waiting)
ioleft1--;
if (state2 == e_task_running)
cpuleft2--;
if (state2 == e_task_waiting)
ioleft2--;
} /* end of main for loop */
}
Implement:
Part 1 Implement the SJF algorithm (non-preemptive). Again, ignore the first input parameter. Do this by putting the function: sjf(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2); in pslibrary.c. Add two lines to assign1.c to call this and display. Use the heading: "SJF ". Implement this by modifying your fcfs code or the prototype. Part 2 Implement the PSJF algorithm. That is, write the function: psjf(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2); and add two additional lines to assign1.c. Part 3 Implement the Round Robin algorithm. That is, write the function: rr(char *s1, char *s2, int q, int x1, int y1, int z1, int x2, int y2, int z2); In this case the first input parameter is the quantum and the others will be as before.
Problem description in terms of process scheduling Consider two processes, each with two CPU bursts with one I/O burst in between. Process 1 has a CPU burst of x1 units followed by an I/O burst of y1 units followed by a second CPU burst of z1 units. Process 2 has a CPU burst of x2 units followed by an I/O burst of y2 units followed by a second CPU burst of z2units. Suppose that Process 1 arrives in the ready queue just before Process 2 and just after Process 2 arrives the process that was in the CPU terminates. No other processes are in the system. For each of the scheduling algorithms draw a Gantt charts showing the state of each of the two processes. Use the letter R to represent the running state, the letter w for the waiting state and the letter r for the ready state. Calculate the two waiting times, the average waiting time and the CPU utilization. (Break any ties by having process 1 run first.) Part 1 of assignment 1 describes an algorithm that is similar, but possibly not identical to the FCFS algorithm, where the last 6 input parameters are x1, y1, z1, x2, y2, and z2. The numbers displayed are the waiting times for process 1 and process 2, the average waiting time, and the cpu utilization. This version of the algorithm always chooses process 1 if both processes become ready at the same time.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started