【操作系统】Operating System Concepts上机作业6

4.15 Consider the following code segment:

pid t pid; 
pid = fork();
if (pid == 0) { /* child process */ 
fork(); 
thread create( . . .); 
} 
fork(); 

How many unique processes are created?

6 processes. If the parent process is not considered, the unique processes is 5.

How many unique threads are created?

Thread creation is done in the if. Execute the only child process P1 in the if block. Therefore, process P1 will be created as a thread. Process P2 is created using fork(). Therefore, process P2 will also create a thread. Therefore, two unique threads will be created.

5.1 In Section 5.4, we mentioned that disabling interrupts frequently can affect the system’s clock. Explain why this can occur and how such effects can be minimized.

  • Disabling interrupts on a multiprocessor can be time consuming, since the message is passed to all the processors. This message passing delays entry into each critical section, and system efficiency decreases. Also consider the effect on a system’s clock if the clock is kept updated by interrupts.
  • In semaphore implementations, interrupts can also be enabled and disabled rather than waiting.
  • We can use these special instructions to solve the critical-section problem in a relatively simple manner such as test() and set() instructions.

5.2 Explain why Windows, Linux, and Solaris implement multiple locking mechanisms. Describe the circumstances under which they use spin- locks, mutex locks, semaphores, adaptive mutex locks, and condition variables. In each case, explain why the mechanism is needed.

  • In a variety of operating systems (Windows, Linux, or solaris), there is multiprogramming and multiprocessing. In this case, there are various threads working at the same time to complete the task, and there is a multithread working on Shared memory. So it’s important to maintain the atomicity of the data, which means that if someone is working on some memory address, no one else can access it. Locks are used frequently and typically are used for crucial kernel functions, tuning their implementation and use can produce great performance gains.
  • Spin locks are suitable for multi-processor systems. A spin lock does not cause the caller to fall asleep. If the spin lock is already held by another execution unit, the caller loops around to see if the spin lock holder has released the lock.
  • Mutex locks are useful for locking resources, can be used in interactive contexts but not in interrupt contexts, and only one task can hold a mutex at a time, and only that task can unlock the mutex.
    Semaphores is used for communication between threads.
  • Adaptive mutex locks is used by most operating systems. The idea is to use a spin lock when trying to access a resource that is locked by the currently running thread, but to sleep if the thread is not currently running.
  • Condition variables are a thread queue associated with the monitor on which a thread can wait for certain conditions to become true. Multiple condition variables can be associated with the same mutex.
  • Semaphores and condition variables are more appropriate synchronization tools when resources must be held for a long time because long rotation is not appropriate.

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.

  • Busy waiting means that a process is waiting for a condition to be satisfied in a tight loop without relinquishing the processor.
  • Busy waiting is an inherent problem in the semaphore. When a process fails to enter the critical region, it is still in the ready queue, and it may also occupy the CPU, wasting CPU time. Shaping semaphores, also known as spin locks, is sometimes useful in multiprocessor systems.
  • Other kinds of waiting in the operating system include processes waiting in the wait queue for I/O to complete, and blocking waits for the recording semaphore.
  • With the adoption of the recording semaphore, busy waiting is eliminated.

5.4 Explain why spinlocks are not appropriate for single-processor systems

  • yet are often used in multiprocessor systems.
  • They are often employed on multiprocessor systems where one thread can “spin” on one processor while another thread performs its critical section on another processor.
  • If a high-priority thread declares a spinlock that a low-priority thread already has, it will loop forever: the low-priority thread will never again have a chance to run and release.
  • If an interrupt handler tries to claim an owned spinlock thread, the interrupt handler will also loop forever. In a multi-processor setup, a spin lock is more appropriate because context change for multiple cpus in a multi-processor system is more expensive and may exceed the cost of a spin lock. With a spin lock, other processes execute on other processors, modifying the state of the program to release the first process from the spin lock.

5.5 Show that, if the wait() and signal() semaphore operations are not executed atomically, then mutual exclusion may be violated.

All modifications to the integer value of the semaphore in the wait() and signal() operations must be executed indivisibly. That is, when one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value.
In addition, in the case of wait(S), the testing of the integer value of S (S ≤ 0), as well as its possible modification (S–), must be executed without interruption.

wait(S) {
while (S <= 0) ; // busy wait
           S--;
} 
signal(S) { 
	S++; 
} 

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

do{
	wait(mutex);
	/*critical section */
	signal(mutex);
	/*remainder section*/
}while(true);

5.7 List three examples of deadlocks that are not related to a computer- system environment.

  • Two cars crossing a single-lane road from opposite directions.
  • A person go upstairs and a person go downstairs on a one-person stair.
  • Two persons need to use the same pencil at the same time.

5.8 Is it possible to have a deadlock involving only a single process? Explain your answer.

No. This follows directly from the hold-and-wait condition.

5.10 The first known correct software solution to the critical-section problem for two processes was developed by Dekker. The two processes, P0 and P1, share the following variables: The structure of process Pi (i == 0 or 1) is shown in Figure 5.25. The other process is Pj (j == 1 or 0).

Prove that the algorithm satisfies all three requirements for the critical-section problem.

boolean flag[2]; /* initially false */ 
int turn; 
do {
flag[i] = true; 
while (flag[j]) { if (turn == j) { 
                       flag[i] = false;
                       while (turn == j) ; /* do nothing */
                       flag[i] = true;
} } 
                    /* critical section */
                  turn = j;
                  flag[i] = false;
/* remainder section */ } while (true); 

They are mutex, which means there can only be one in the critical section.
If Pi is in the critical area while Pj wants to go in. Look at the flag。
Before the process enters the critical section, Pi Pj is flagged as true. See turn. Only after the process exits the critical section can another process enter the process
Currently no process is in the critical section, only one process is attempting to enter, see flag
Both try to go in, look at the turn, into the process in limited time to reset the flag
PI is refused to enter the critical area, pj has been in the critical area or allowed to enter, when Pj exit the critical area, set turn as I, reset flag, PI can enter

We now prove that this solution is correct. We need to show that:
Mutual exclusion is preserved.
The progress requirement is satisfied.
The bounded-waiting requirement is met.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值