Sei sulla pagina 1di 9

Air University

Department of Computer Sciences

OPERATING SYSTEMS

Assignment No. : 2

ID: 150816, 160661, 150804, 160597

Class: BSCS-4-B

March 22, 2018

Q 4.2: What are two differences between user-level threads and kernel-level
Threads………?
Answer:
Difference Between User Level Thread & Kernel Level Thread
User Level Thread Kernel Level Thread

 When threads are managed in user  No run-time system is needed in each.


space, each process needs its own Also, there is no thread table in each
private thread table to keep track of the process. Instead, the kernel has a thread
threads in that process table that keeps track of all the threads
in the system.
 User-level threads are generally fast to
create and manage  The kernel-level threads are slow and
inefficient. 

User level thread id better than kernel level thread because kernel level thread is
restricted to one OS and is specific to one OS whereas user level thread is generic and
can run on any operating system.

Q 4.3 : Describe the actions taken by a kernel to context switch between kernel level
threads…????

ANSWER:

Context switching between kernel threads typically requires saving the value of the CPU
registers from the thread being switched out and restoring the CPU registers of the new
thread being scheduled.

QUESTION 4.11: Is it possible to have concurrency but not parallelism? Explain.

ANSWER:
It is not possible to have parallelism without concurrency though but it is possible to
have concurrency but not parallelism. As concurrency means that thread and more than
one process is processing at same time, but it does not mean that processes are running
simultaneously. So we can say that parallelism is not necessary for concurrency.
QUESTION 4.15: Consider the following code segment:
pid t pid;
pid = fork();
if (pid == 0) { /* child process */
fork();
thread create( . . .);
}
fork();
a. How many unique processes are created?
b. How many unique threads are created?

ANSWER:
a) 6 processes will be created.
b) 2 threads will be created.

Q 5.2: Explain why Windows, Linux, and Solaris implement multiple locking
mechanisms……………?
ANSWER:
In these operating systems, spinlocks are used for multiprocessing where a thread runs
in a busy loop rather than going through the sleep queue. Mutexes are used for locking
resources and the Solaris specifically implements it with a spinlock.
Whereas synchronization is achieved through semaphores and condition variables
where a resource must be held for a longer duration rather then spinning which is
inefficient for a long duration.

Q 5.3: What is the meaning of the term busy waiting? What other kinds of
waiting are there in an operating system?
ANSWER:

Busy wait is a state/condition in which a process is waiting in a loop for a condition to be


satisfied in order to move back in the ready queue. A process can also be put to sleep
and have it wake up when the program condition is satisfied for it to perform again.

Q 5.4: Explain why spinlocks are not appropriate for single-processor systems
yet are often used in multiprocessor systems?
Answer:
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 letting go of 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.

Q 5.6: Illustrate how a binary semaphore can be used to implement mutual exclusion
among n processes?

Answer:
The n processes share a semaphore, mutex , initialized to 1.
do {
wait(mutex);
signal(mutex);
}
while (true);

Q : 5.7
Race conditions are possible in many computer systems. Consider a
banking 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 withdraw() function and the wife
calls deposit(). Describe how a race condition is possible and what
might be done to prevent the race condition from occurring.

Ans:
A situation like this, where several processes access and manipulate the same data concurrently
and the outcome of the execution depends on the particular order in which the access takes
place, is called a race condition. Here in this case when husband and wife wanted to access the
same bank account concurrently and when withdraw is in process, wife is depositing there may
be chance of race condition which changes the order of the execution. To guard against the
race condition above, we need to ensure that only one process at a time can be manipulating
the variable counter. To make such a guarantee, we require that the processes be synchronized
in some way.

Assume the balance in the account is 250.00 and the husband


calls withdraw(50) and the wife calls deposit(100). Obviously the correct
value should 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(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 balance is set to 200.00 - obviously
an incorrect value.

Q : 5.11
Explain why interrupts are not appropriate for implementing synchronization
primitives in multiprocessor systems.

Interrupts are not sufficient in multiprocessor systems since


disabling interrupts only prevents other processes from executing on the
processor in which interrupts were disabled; there are no limitations on what
processes could be executing on other processors and therefore the process
disabling interrupts cannot guarantee mutually exclusive access to program
state.
Q : 5.15
Consider how to implement a mutex lock using an atomic hardware
instruction. Assume that the following structure defining the mutex
lock is available:
typedef struct {
int available;
} lock;
(available == 0) indicates that the lock is available, and a value of 1
indicates that the lock is unavailable. Using this struct, illustrate how
the following functions can be implemented using the test and set()
and compare and swap() instructions:
• void acquire(lock *mutex)
• void release(lock *mutex)

Ans:
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;
}

Question No # 2 :
Consider a coke machine that has 14 slots. The Producer is the delivery person and the
consumers is the student using the machine?
a) What would be the initial value of semaphores?
 Mutex =1
 Full machine = 0 (initially)
 Empty machine = 14 (no of slots available initially)

b) Code is written under


Mutex = 1;
Delivery_person()
{

Wait(empty machine); // to check slot


Wait (mutex); // to ensure that no one is using
machine by reducing value
put 1 coke in machine;
signal (mutex) ; // student can access it easily
signal (full machine) // one slot for coke is filled
}

Student ()
{
Wait (full machine) // check whether a coke is present
Wait (mutex) // no one else is using
Take 1 coke from machine;
Signal (mutex) // increase value back to 1
Signal(empty machine) //signal to delivery person

Task Division:-
Bassam = Question No # 1 Chapter 4: Practice Exercises 4.2, 4.3,4.11, 4.15
Talha = Question No # 1 Chapter 5: Practice Exercises5.2,5.3, 5.4, 5.6
Usman = Question No # 1 Chapter 5: Exercises 5.7, 5.11,5.15
Khurram = Question No # 2

Potrebbero piacerti anche