Sie sind auf Seite 1von 4

Chapter 3:

3.1)
PARENT: value = 5
3.8)
Short-term (CPU scheduler)-selects which job is to be executed next and also
allocates the CPU to them.
It is invoked very frequently probably in milliseconds.
Medium-term- used especially with time-sharing systems as an intermediate
scheduling level. A swapping scheme is implemented if the degree of multi
programming is to decreased by moving the process to memory store it to disk and
reinstate them later to continue where they left off.
Long-term (job scheduler)-verifies which jobs are brought into ready queue for
processing. Long term scheduler controls degree of multiprogramming and is
invoked very frequently probably can be seconds or minutes.

3.9) 1. In response to a clock interrupt, the CPU stores the Program counter
information and user stack pointer of the currently executing process and transfers
control to the kernel clock interrupt handler.
2. The clock interrupt handler stores the rest of the registers and other machine
states in the Process Control Block.
3. The CPU invokes the scheduler to determine the next process to execute.
4. The CPU then retrieves the value of process state in the process control block
(PCB) and restores the registers. This restoration makes the processor start its
operation from it left interrupted executing the user code under user mode
privileges.

3.12)Total of 16 processes are created including the initial process.


3.14)child: pid = 0 /*Assume pid of parent and child are 1052 and 1163)
child: pid1 = 1163
parent : pid = 1163
parent : pid1 = 1052
Chapter-4:
4.4 )

Creating either a user or kernel thread involves allocating a small data structure
which can hold a register, stack, and priority levels. A thread is smaller compared to
a process and a thread creation typically uses fewer resources than creating a
process. Creating a process involves allocating a process control block (PCB) which
is a rather large data structure. The PCB includes CPU scheduling information,
memory management information, accounting information, and I/O status
information. Allocate and maintain the memory map is typically consumes most of
the time which is not required for a thread.

4.12 ).
speedup 1 /(S+ ((1S) /N))
(a) Two processing cores Speed up=1.428
(b) Four processing cores Speed up=1.82

4.17 )
Line C:
CHILD: value = 5
Line P:
PARENT: value = 0

5.3) Busy waiting means that a process is waiting for a condition to be satisfies
without ceasing the hold of the processor. But in alternate a process could also wait
by leaving the hold of the processor and can be blocked by condition and wait until
some appropriate time in the future. Busy waiting can be avoided which can incur
overhead, involves putting a process to sleep and having it to wake up when it
reaches the appropriate state.
5.4) Spinlocks are not appropriate for single-processor systems because the
condition to free a process out of the spinlock can be obtained only by executing
another process. If the process is not ceasing the control over the processor, other
processes wont get the opportunity to set the condition required for the first
process to make any progress. In a multiprocessor system, other processes execute
on other processors and thereby modifying the state of the program can release the
first process from the spinlock.

5.7) When several processes access and manipulate the same data concurrently
and the outcome depends on the order of execution in which the access took place
is called a race condition. Here in this case, for suppose the Bank Account consists
of (300) when husband and wife wanted to access the same bank account

concurrently and when husbands withdraw(50) is in process and before commit the
process to 250, wife tries to deposit(100) there may be chance of race condition
which changes the order of the execution making the value to 250. To guard against
the race condition above, we need to ensure that only one process at a time can
have access to variable counter. To make such a guarantee, we require that the
processes be synchronized in some way.

5.15) Test and Set:


typedef struct
{
int available;
}lock;
void init(lock *mutex)
{
// available==0 -> lock is available, available==1 -> lock unavailable
mutex->available=0;
}
int test_And_Set(int *target)
{
int rv = *target;
*target = true;
return rv
}
void acquire(lock *mutex)
{
while(test_and_set(&mutex->available,1)==1)
;
}
void release(lock *mutex)
{

mutex->available=0;

}
Compare and Swap:

int compare_and_Swap(int *ptr,int expected,int new)


{
int actual = *ptr;
if(actual == expected)
*ptr = new;
return actual;
}
void acquire(lock *mutex)
{
while(compare_and_swap(&mutex->available,0,1)==1)
;
}
void release(lock *mutex)
{

mutex->available=0;

}
5.17)
The lock is to be held for a short duration A spinlock is a better option compared to
mutex lock which requires putting process to sleep and awakening it when it
reaches appropriate state.
The lock is to be held for a long duration - a mutex lock is preferable to spinlock as
this allows a processing core to schedule another process while the locked process
waits for its turn.
A thread may be put to sleep while holding the lock - a mutex lock is definitely
preferable as it can put the process to sleep while allowing the other process to
wake up and upon reaching the appropriate state the sleeping process can awaken
for execution.