Operating system: Full name: Trịnh Mạnh Hùng Student id: 1952740
Operating system: Full name: Trịnh Mạnh Hùng Student id: 1952740
Operating system: Full name: Trịnh Mạnh Hùng Student id: 1952740
Lab 4
Full name: Trịnh Mạnh Hùng
Student id: 1952740
3. Practice (7 PTS):
Code of sched.c:
#include "queue.h"
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
exec_time = timeout;
proc->burst_time -= timeout;
/* Emulate the execution of the process by using
* 'usleep()' function */
usleep(exec_time * TIME_UNIT);
int main() {
pthread_t cpu_id; // CPU ID
pthread_t loader_id; // LOADER ID
/* Initialize queues */
initialize_queue(&in_queue);
initialize_queue(&ready_queue);
pthread_exit(NULL);
Code of sched_FCFS.c:
#include "queue.h"
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
pthread_t cpu_id; // CPU ID
pthread_t loader_id; // LOADER ID
/* Initialize queues */
initialize_queue(&in_queue);
initialize_queue(&ready_queue);
pthread_exit(NULL);
Code of queue.c:
#include <stdlib.h>
#include "queue.h"
#include <pthread.h>
#!/bin/bash
echo "Input file:"
cat input.txt
echo "Results_Round Robin"
gcc sched.c queue.c -o sched -lpthread
cat input.txt | ./sched
echo "Results_FCFS"
gcc sched.c queue.c -o sched_FCFS.s -lpthread
cat input.txt | ./sched_FCFS
Suppose that the following processes arrive for execution at the times indicated. Each process
will run for the amount of time listed. In answering the questions, use non-preemptive
scheduling, and base all decisions on the information you have at the time the decision must be
made.
a) What is the average turnaround time for these processes with the FCFS scheduling
algorithm?
The average turnaround time for these processes with the FCFS scheduling algorithm is
(9 − 0) + (14 – 0.6) + (17 − 1)
= 12.8
3
b) What is the average turnaround time for these processes with the SJF scheduling
algorithm?
The average turnaround time for these processes with the SJF is
(9 − 0) + (12 − 1) + (17 − 0.6)
≈ 12.13
3
c) The SJF algorithm is supposed to improve performance, but notice that we chose to run
process P1 at time 0 because we did not know that two shorter processes would arrive
soon. Compute what the average turnaround time will be if the CPU is left idle for the
first 1 unit and then SJF scheduling is used. Remember that processes P1 and P2 are
waiting during this idle time, so their waiting time may increase. This algorithm could be
called future-knowledge scheduling.
After CPU is left idle for one unit then the processes are normally run using SJF
scheduling. It means that after one unit, P3 will run first then P2, P1 run respectively.
(1+3−1) + (1+3+5−0.6)+(1+3+5+9) 29.4
The average turnaround time is = = 9.8
3 3