When is SJF worse than FCFS? - operating-system

In operating systems of supercomputers, which handles a big quantity of tasks at the same time, is there any situation when SJF policy is taking longer than FCFS policy, speaking of waiting time metric?
It can be assumed that more than one core are present in the system.

First I thought that it is not possible, then I took some time and finally arrived at this result:
Yes it can be.
Suppose that ready queue is filled with processes with equal burst times(all = x):
Process Burst time
P1 x
P2 x
P3 x
P4 x
. .
. .
. .
Pn x
Now in this case what FCFS would do, the process that would come first will be allocated the CPU and then the next process which comes first will be allocated the CPU and so on without wasting any time.
But what SJF will do is :it will first find the job with the shortest burst time from the available jobs in the ready queue which in this case is wastage of time as all have equal burst times and SJF would end up traversing the ready queue without any fruitful result.

Related

In Short Job First (SJF) scheduling algorithm does IO bound jobs get priority over CPU bound jobs?

Recently I came across the statement that
In SJF IO bound jobs get priority over CPU bound jobs.
I found this statement in page 4 of this slide and also in page 3 of this slide. I decide to attach the corresponding pictures below, if in case the link breaks in the future.
But I am having difficulty to understand the above and it seems rather counter intuitive to me. My argument is as follows:
I assume a CPU bound process is one which uses has higher CPU burst:(CPU Burst+IO Burst) and I assume a process as IO bound which has higher IO Burst:(CPU Burst+IO Burst). I assumed it from the knowledge I have received after reading the textbook "Operating Concepts" by Galvin et. al and the excerpt is below:
An I/O-bound process is one that spends more of its time doing I/O than it spends doing computations. A CPU-bound process, in contrast, generates I/O requests infrequently, using more of its time doing computations.
Which I guess agrees with what the professor says here.
Based on this I came up with the following examples:
Suppose I have two jobs
JOB 1: CPU BURST = 10 units; IO BURST =100 units
JOB 2: CPU BURST= 100 units; IO BURST=10 units...
SJF shall schedule JOB1 first which is IO Bound...
———————————————————————————
suppose I have two other jobs
JOB 3: CPU BURST = 10 units; IO BURST =1 units
JOB 4: CPU BURST= 100 units; IO BURST=200 units...
SJF shall schedule JOB3 first which is CPU bound...
From the above example I do not find any such correlation that SJF gives priority to IO bound jobs.

SJF Scheduling: Selecting Process based on predicted CPU Burst Time

In SJF Algorithm, we predict the next CPU Burst time using the formula:
τ(n+1) = α*t(n) + (1-α)*τ(n). And then we select the process with the shortest predicted burst time.
Now my question is: do we already have an idea about the CPU burst times of the processes arriving?
If yes, then why predict the CPU burst time? We could rather just use the shortest time process for scheduling.
If no i.e., we do not have any idea about the burst times of the processes, how is the predicted burst time τ(n+1) helping us to pick a process?
Hope I am able to explain my confusion.
Thanks.
The answer is in the question itself. The later condition is true we don't have an idea about burst time of incoming processes this is the reason that we are predicting their burst time τ(n+1). Our prediction may not be 100% right all the time but it'll server the purpose of SJF to a very great extent !
I hope you would've coded this and saw the results if not then i recommend to do so, it'll help a lot understanding this.
This is the application I developed, for my teacher, on some scheduling techniques.enter image description here

Is it possible that you have the same arrival time in CPU Scheduling?

I have searched the internet for examples for the algorithms in cpu scheduling and I have never seen any examples with the same arrival time.
Is it possible to make the processes have the same arrival time?
For example:
Algorithm: Round Robin
Process ---- Arrival Time ----- Burst Time
P1 ----------------- 3 ------------------ 4 -----
P2 ----------------- 1 ------------------ 5 -----
P3 ----------------- 1 ------------------ 3 -----
Quantum = 1
What would be the gantt chart look like?
Is it possible to make the processes have the same arrival time?
Yes, for normal definitions of "same time" (e.g. excluding "in the same Planck time quantum") it's possible for processes to have the same arrival time.
For an example, imagine if 100 tasks sleep until midnight. When midnight occurs a timer IRQ handler processes a list of tasks waiting to wake up and wakes up 100 tasks at the "same" time.
Now, for this example you could say that "same time" is stricter; and that the timer IRQ handler processes the list of tasks sequentially and adds them to scheduler's queues sequentially, and that it's only "almost at the same time". In this case it's still possible to have N CPUs running in parallel (with a different timer for each CPU) that happen to wake (up to) N tasks at the same time.
Of Course, multiple processes can have the same arrival time i.e the time they came for looking the CPU to execute them. And its the responsibility of the Processor to handle and schedule them accordingly as per the appropriate Scheduling Algorithms.
When the Arrival time for two or more processes are same, then the RR-Scheduling follows FCFS {First come first serve} approach. Here in Round robin scheduling with quantum = 1, we have
Gantt Chart
At time 0, no process
At time 1 , we have P2 and P3 with P2 first then after a quantum RR executes P3,
At time 3 we have all three processes with order P2,P3,P1 hence the RR-Algorithm will keep switching between them until they complete their execution (burst) time.
And we will get all executed at time 13.

What has higher priority on MLFQ?

Hello I'm undergraduate student of computer programming
I take operating system class and I have question about MLFQ scheduling
Suppose that MLFQ has two ready queues, and they use both round-robin scheduling method which of time quantum is 3 seconds and 5 seconds each
Of course priority of Q1 is higher.
Then suppose that there are no ready process in Q1, so process in Q2 can be allocated on CPU.
However, a process that finished IO bound burst returns to Q1, and process in CPU which belonged to Q2 is not done(just 3 seconds gone)
In this situation, What happen?
Is the process in Q1 preempt the CPU right now?
Or process in CPU now(which is come from Q2) is allocated on CPU until time quantum of it(still 2 seconds needed) is reached?
Thank you for reading.
The process that arrives after completing I/O burst in Q1, will preempte the process in Q2. Quoting from the Operating Systems principles by Galvin,Gagne and Silberchatz:
A process that arrives in Q1, will preempt a process in Q2. A process
in Q1 will in turn be preempted by a process arriving in Q0(if Q0
exists).
SO,Any process that arrives in a higher priority Q, will preempt the process of a lower priority Q, even if the time quantum of lower process is not reached. A process gets to have the CPU as long as the ready Q of higher priority is empty.

Four processes of 1gb,1.2gb,2gb,2gb are there and RAM available is 2gb. We have a time shared system.

What of the following is the most appropriate scheduling algorithm
Options being-
a. all processes are loaded sequentially 1 by 1
b. load one process at a time and execute processes in RR fashion
c. load 1gb, 1,2gb first then processes 3 and 4 follow
d. All processes can be loaded together and CPU time shared among them
I came across this question somewhere and I was confused, as the answer could b (D) if we consider virtual memory and otherwise (B). Am I missing something here?
In my opinion, virtual memory should be taken into account here. Its clearly logical. Let me give you the answer by negation.
A.) Clearly not the case as CPU cycles will be wasted.
B.) If we are loading one process at a time, then it doesn't matter what algorithm we are applying afterwards. Its same as #A.
C.) Taking virtual memory into account, if we can load P1 and P2, then for some smaller page size, we can load P3 and P4 too at same time.
D.) As I stated in #C, for an arbitrary smaller page size value, we can load all of them simultaneously and schedule them using Round Robin Scheduling Algorithm.