You are on page 1of 5

3.1 Using the program shown in Figure 3.30, explain what the output will be at LineA.

Ans: The result is still 5 as the child updates its copy of value. When control returns to the
parent, its value remains at 5.


3.8 Describe the differences among short-term, medium-term, and long-term scheduling.
Ans: The short-term scheduler selects from the ready processes that the next process to run and
gives it to CPU. The long-term scheduler selects from the pool of processes that are
waiting on disk and loads the selected process(es) into memory. These processes have
not yet begun their execution. The medium-term scheduler takes processes that are
currently in memory and selects those to be swapped out to disk. These processes will be
swapped back in at a later point. This is done to improve process mix or because of
memory requirements .

A primary difference is in the frequency of their execution. The short-term scheduler
must select a new process quite often. Long-term is used much less often since it handles
placing jobs in the system and may wait a while for a job to finish before it admits
another one.


3.9 Describe the actions taken by a kernel to context-switch between processes?
Ans: In general, the operating system must save the state of the currently running
process and restore the state of the process scheduled to be run next. Saving the
state of a process typically includes the values of all the CPU registers in
addition to memory allocation. Context switches must also perform many
architecture-specific operations, including flushing data and instruction caches.



3.12 Including the initial parent process,how many processes are created by the
program showing in the below fig?

#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
for(i=0;i<4;i++)
fork();
return 0;
}
Ans: 16 process are created.


3.14. Using the program in fig3.34 identify the values of pid at lines A,B,C, and
D(Assume that the actual pids of the parent and child are 2600 and 2603,
respectively)
Ans: A=0
B-2603
C=2603
D=2603



4.4 .What resources are used when a thread is created? How do they differ from those
used when a process is created?
Ans: A context must be created, including a register set storage location for storage
during context switching, and a local stack to record the procedure call arguments,
return values, and return addresses, and thread-local storage. A process creation
results in mem-ory being allocated for program instructions and data, as well as
thread-like storage. Code may also be loaded into the allocated memory.



4.11: ``Is it possible to have concurrency but not parallelism? Explain.
Ans: Concurrency is a condition that exists when at least two threads are making
progress. A more generalized form of parallelism that can include time-slicing as
a form of virtual parallelism.''
In contrast, the parallelism is a condition that arises when at least two threads are
executing simultaneously''. It is possible for two threads to make progress, though not at
the same instant.
for example, consider a single-core processor running two threads. An operating system
will normally switch back and forth between the two threads very quickly, giving the
appearance of parallelism. the two threads are each taking turns executing their respective
instructions on the same cpu core. Though it is not possible to have parallelism without
concurrency, it is possible to have
concurrency but not parallelism.



4.17: The program shown in fig 4.16 uses the Pthreads API.What would be the output
from program at LINE C and LINE P?

Child-5
Parent-0



5.3: What is the meaning of the term busy waiting? What other kinds of waiting are
there in an operating system? Can busy waiting be avoided altogether? Explain
your answer.
Ans: Busy waiting means that a process is waiting for a condition to be satisfied in a
tight loop without relinquishing the processor. Alternatively, process could wait by
relinquishing the processor, and block on a condition and wait to be awakened at
some appropriate time in the future. Busy waiting can be avoided but incurs the
overhead associated with putting a process to sleep and having to wake it up
when the appropriate program state is reached.



5.4: Explain why spinlocks are not appropriate for single-processor systems yet are
often used in multiprocessor systems.
Ans: Spinlocks are not appropriate for single-processor systems because the condition
that would break a process out of the spinlock can be obtained only by executing a
different process. If the process is not relinquishing the processor, other processes
do not get the opportunity to set the program condition required for the first
process to make progress. In a multiprocessor system, other processes execute on
other processors and thereby modify the program state in order to release the first
process from the spinlock.



5.6: Illustrate how a binary semaphore can be used to implement mutual exclusion
among n processes.
Ans: The n processes share a semaphore, mutex, initialized to 1. Each process
Pi is organized as follows:
do {
wait(mutex);
/* critical section */
signal(mutex);
/* remainder section */
} while (true);



5.7: Race conditions are possible in many computer systems.Consider a abanking
system that maintains an account balance with two functions:deposit(amount) and
withdraw(amount).These two functions are passed the amount that is to be
deposited or withdrawn from the bank account balance.Assume that a husband and
wife share a bank account.Concurrently,the husband calls the withdrawn() function
and the wife calls deposit().Describe how a race condition is possible and what
might be done to prevent the race condition from occuring.
Ans: Assume the balance inthe account is 250.00 and the husband calls withdraw(50)
and the wife calls deposit(100).Obviously the correct value shuold be 300.00.Since
these two transactions will be serialized,the local value of balance for the husband
becomes 200.00,but before he can commit the transaction,the deposit of 100
operation takes place and updates the shared value of balance to 300.00.We then
switch back to the husband and the value of the shared balances is set to 200.00,it
is obviously an incorrect value.



5.17: Assume that a system has a multiple operating cores.For each of the following
scenarios,describe which is a better locking mechanism-a spinlock or a mutex lock
where waiting processes sleep while waiting for the lock to become available:
->The lock is to be held for a short duration.
Spinlock

->The lock is to be held for a long duration.
Mutex lock

->A thread may be put to sleep while holding the lock
Mutex lock



6.2: Explain the difference between preemptive and nonpreemptive scheduling.
Ans: -> Preemptive scheduling allows a process to be interrupted in the midst of its
execution, taking the CPU away and allocating it to another process.
-> Nonpreemptive scheduling ensures that a process relinquishes control of the
CPU only when it finishes with its current CPU burst.



6.10: Why is it important for the scheduler to distinguish I/O-bound programs from
CPU-bound programs?
Ans: I/O-bound programs have the property of performing only a small amount of
computation before performing I/O. Such programs typically do not use up their
entire CPU quantum. CPU-bound programs, on the other hand, use their entire
quantum without performing any blocking I/O operations. Consequently, one could make
better use of the computer's resouces by giving higher priority to I/O-bound programs and
allow them to execute ahead of the CPU-bound programs.



6.14: Consider the exponential average formula used to predict the length of the next CPU
burst. What are the implications of assigning the following values to the parameters used
by the algorithm?
a. = 0 and 0 = 100milliseconds
b. = 0.99 and 0 = 10milliseconds
Ans: (a) the formula always makes a prediction of 100 milliseconds
for the next CPU burst.
(b) The most recent behavior of the process is given much higher weight than the past
estimated value. Consequently, the scheduling algorithm is almost memory-less, and
simply predicts the length of the previous burst for the next quantum of CPU execution.


6.19: Which of the following scheduling algorithms could result in starvation?
a. First-come, first-served
b. Shortest job first
c. Round robin
d. Priority
Ans: Shortest job first and priority-based scheduling algorithms
could result in starvation.



6.31:Consider two processes P1 and P2 where p1 = 50, t1 = 25 and p2 = 75, t2 = 30.
a. Can these two processes be scheduled using rate monotonic scheduling? Illustrate your
answer using a Gantt chart such as the ones in fig6.16 - fig 6.19.
b. Illustrate the scheduling of these two processes using earliest deadline first (EDF)
scheduling.
Ans: Consider when P1 is assigned a higher priority than P2 with the rate monotonic
scheduler. P1 is scheduled at t = 0, P2 is scheduled at t = 25, P1 is scheduled at t = 50,
and P2 is scheduled at t = 75. P2 is not scheduled early enough to meet its deadline.
When P1 is assigned a lower priority than P2, then P1 does not meet its deadline since it
will not be scheduled in time.

You might also like