Review qeustions 4.pdf
Document Details
Uploaded by RaptQuasimodo
Tags
Full Transcript
Keep the ques*ons and answers exactly as they are. Which of the following problems is a classic example of a producer-consumer scenario that requires synchroniza*on? A) Thread execu*on B) Race condi*on C) Semaphore implementa*on D) Buffer manipula*on Correct Answer: D) Buffer manipula*on Explana*on: T...
Keep the ques*ons and answers exactly as they are. Which of the following problems is a classic example of a producer-consumer scenario that requires synchroniza*on? A) Thread execu*on B) Race condi*on C) Semaphore implementa*on D) Buffer manipula*on Correct Answer: D) Buffer manipula*on Explana*on: The producer-consumer problem typically involves managing a buffer where producers add items and consumers remove them. Synchroniza*on is needed to ensure that the producer doesn't add items to a full buffer and the consumer doesn't remove items from an empty buffer. What is the primary purpose of using mutex locks in thread synchroniza*on? A) To allow mul*ple threads to run in parallel B) To ensure mutual exclusion when threads access shared resources C) To op*mize the CPU usage by threads D) To detect deadlocks between threads Correct Answer: B) To ensure mutual exclusion when threads access shared resources Explana*on: Mutex locks are used to ensure that when one thread is accessing a shared resource, no other thread can access the same resource un*l the mutex is unlocked. Which of the following is a requirement for solving the cri*cal-sec*on problem? A) Unbounded Wai*ng B) Mutual Progress C) Bounded Wai*ng D) Sequen*al Execu*on Correct Answer: C) Bounded Wai*ng Explana*on: Bounded wai*ng is a requirement that ensures a limit on the number of *mes other processes are allowed to enter their cri*cal sec*ons aQer a process has made a request to enter its cri*cal sec*on and before that request is granted. In the context of semaphores, what does the 'wait()' opera*on do? A) Increments the semaphore value B) Decrements the semaphore value C) Checks the semaphore value without changing it D) Resets the semaphore value to its ini*al value Correct Answer: B) Decrements the semaphore value Explana*on: The 'wait()' opera*on, also known as 'P()' or 'down()', decrements the semaphore value. If the value becomes nega*ve, the process performing the wait is blocked un*l the semaphore value is greater than or equal to zero. What is a binary semaphore? A) A semaphore that only allows one thread to execute at a *me B) A semaphore that can take on any integer value C) A semaphore that is used exclusively for input/output opera*ons D) A semaphore that can only have values 0 or 1 Correct Answer: D) A semaphore that can only have values 0 or 1 Explana*on: A binary semaphore is a type of semaphore that can only have two values, 0 or 1, similar to a mutex lock. It is used to control access to a single shared resource. Why are spinlocks useful in mul*processor systems? A) Because they prevent race condi*ons in single-threaded applica*ons. B) Because they allow a thread to run in a busy loop for a short *me, avoiding the overhead of a sleep queue. C) Because they use less memory than other synchroniza*on tools. D) Because they provide automa*c priority inheritance. Correct Answer: B) Because they allow a thread to run in a busy loop for a short *me, avoiding the overhead of a sleep queue. Explana*on: Spinlocks are efficient for mul*processor systems as they allow threads to wait in a busy loop for a very short dura*on instead of being put to sleep, which can be costly due to context switching overhead. What is the main disadvantage of disabling interrupts frequently in terms of system *me? A) It can result in the CPU being underu*lized. B) It can cause the system clock to lose the correct *me. C) It can lead to a higher number of context switches. D) It increases the complexity of the system's code. Correct Answer: B) It can cause the system clock to lose the correct *me. Explana*on: The system clock is updated on every clock interrupt. Disabling interrupts, especially for a long dura*on, can prevent the clock from upda*ng correctly and result in losing the correct *me. What happens if semaphore opera*ons 'wait()' and 'signal()' are not executed atomically? A) The semaphore value is reset to zero. B) The semaphore value may incorrectly increment. C) Mutual exclusion may be violated. D) The semaphore becomes a binary semaphore. Correct Answer: C) Mutual exclusion may be violated. Explana*on: If semaphore opera*ons 'wait()' and 'signal()' are not atomic, two 'wait()' opera*ons could decrement the semaphore value at the same *me, poten*ally allowing mul*ple processes to enter their cri*cal sec*on simultaneously, thus viola*ng mutual exclusion. In the context of thread synchroniza*on, what is the primary use of 'pthread_mutex_lock()' and 'pthread_mutex_unlock()'? A) To create and delete threads. B) To send signals between threads. C) To ensure mutual exclusion during cri*cal sec*on access. D) To allocate and deallocate memory for thread opera*ons. Correct Answer: C) To ensure mutual exclusion during cri*cal sec*on access. Explana*on: The 'pthread_mutex_lock()' func*on is used to acquire a lock on a mutex before accessing shared data, and 'pthread_mutex_unlock()' is used to release the lock aQer the access is completed. This mechanism ensures that only one thread can access the shared data at a *me, maintaining mutual exclusion. Which synchroniza*on tool is preferred for situa*ons where fairness or priority is important but quick acquisi*on and release of the lock are equally significant? A) Spinlocks B) Condi*on variables C) Reader-writer locks D) Slim reader-writer locks Correct Answer: D) Slim reader-writer locks Explana*on: Slim reader-writer locks are lightweight and do not favor readers over writers or vice versa, nor do they order wai*ng threads in a FIFO queue. They are appropriate for scenarios where fairness is key but so is the speed of lock acquisi*on and release.