Synchronization and Semaphores Lecture Notes

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/27

flashcard set

Earn XP

Description and Tags

Comprehensive practice questions covering thread synchronization, race conditions, atomic operations, and semaphore implementations based on the week eight lecture.

28 Terms

1
New cards

What is the primary objective of synchronization in multi-threaded programming?

To manage communication between multiple threads and coordinate their activities so they do not interfere with each other.

2
New cards

According to the lecture, what are the key topics for assignment two and assignment three?

Assignment two focuses on processes, fork, and exec, while assignment three requires knowledge of networking, threading, and semaphores.

3
New cards

When is assignment two due?

3 PM, Thursday, May 7.

4
New cards

Why did the program race1.crace1.c fail to reach the expected total of 1,200,0001,200,000 increments?

The increment operation was not atomic, meaning multiple threads performed a read-modify-write cycle simultaneously, overwriting each other's work.

5
New cards

What does it mean for an operation to be "atomic"?

The operation is indivisible and cannot be broken down into smaller pieces or interrupted halfway through.

6
New cards

Approximately how many x86 assembly instructions are generated for a pointer dereference increment operation like ∗p++*p++?

Five assembly language instructions.

7
New cards

Why did the custom lock implementation in race2.crace2.c fail to prevent race conditions?

The process of taking the lock was itself non-atomic; multiple threads could simultaneously see the lock was zero and attempt to grab it at the same time.

8
New cards

What are the two main issues identified with the race2.crace2.c program?

It failed to ensure mutual exclusion and it utilized inefficient busy waiting.

9
New cards

What is "busy waiting"?

A technique where a thread uses CPU time to repeatedly check a condition in a loop without adding value to the program.

10
New cards

Name a software-only algorithm mentioned for achieving mutual exclusion.

Peterson's algorithm.

11
New cards

What is a semaphore?

An opaque type, essentially an integer, that supports atomic weight and post operations for synchronization.

12
New cards

Describe the atomic behavior of the semWaitsemWait operation.

If the semaphore value is greater than zero, it is decremented by one; if the value is zero, the thread is suspended until the value becomes greater than zero.

13
New cards

What happens when semPostsemPost is called while multiple threads are waiting on a semaphore with a value of zero?

The semaphore value is incremented by one, and only one of the waiting threads is unblocked.

14
New cards

What is "starvation" in the context of semaphores?

A scenario where a thread never gets a turn to access a resource because other threads are always prioritized by the operating system.

15
New cards

In the function sem_init(sem_t∗sem,intpshared,unsignedintvalue)sem\_init(sem\_t *sem, int pshared, unsigned int value), what does the psharedpshared argument indicate?

If set to zero, the semaphore is shared between threads of a single process; if nonzero, it is shared between processes and must reside in shared memory.

16
New cards

Why might a program using semaphores like race3.crace3.c run slower than a version without synchronization?

Making system calls like sem_waitsem\_wait and sem_postsem\_post is expensive; the lecture noted over two seconds of CPU time were spent on 1,200,0001,200,000 such operations.

17
New cards

What is the restriction regarding passing semaphores between functions?

Semaphores are tied to their memory address and cannot be copied; you must pass the address of the semaphore instead.

18
New cards

Explain the Producer-Consumer pattern using semaphores.

The consumer starts with a semaphore at value zero and blocks on semWaitsemWait, while the producer performs its task and then calls semPostsemPost to unblock the consumer.

19
New cards

How can semaphores be used to limit the maximum number of active threads?

Initialize the semaphore to the maximum allowed number; each spawning thread calls semWaitsemWait before creation, and each exiting thread calls semPostsemPost.

20
New cards

What was the most efficient solution presented in race3a.crace3a.c to avoid race conditions and synchronization overhead?

Designing tasks to be independent by giving every thread its own counter and summing them only at the very end.

21
New cards

Describe the "Dining Philosophers" problem.

A classic deadlock scenario where philosophers sitting at a round table each need two forks to eat, but each only has access to one fork to their left and one to their right.

22
New cards

What is the difference between deadlock and livelock?

Deadlock is when threads are blocked waiting for something that will never happen; livelock is when threads are busy trying to recover from a situation but are not accomplishing any useful work.

23
New cards

What is a simple way to avoid deadlock when multiple resources are required?

Ensure that every thread requests the necessary resources in the same predefined order.

24
New cards

How does sem_trywaitsem\_trywait differ from sem_waitsem\_wait?

sem_trywaitsem\_trywait does not block the thread; if the resource is unavailable, it returns an error code immediately.

25
New cards

What is unique about the timeout provided to sem_timedwaitsem\_timedwait?

It must be an absolute time (a specific timestamp) rather than a relative duration of time.

26
New cards

What is a named semaphore?

A semaphore that can be used between processes by utilizing the file system via sem_opensem\_open, rather than shared memory.

27
New cards

What is the purpose of a pthread_rwlock_tpthread\_rwlock\_t (Read-Write Lock)?

It allows multiple threads to read shared memory simultaneously while ensuring only one writer can access the memory at a time.

28
New cards

What is a condition variable (pthread_cond_tpthread\_cond\_t) used for?

It is used for waiting for a specific condition to occur, such as a variable changing value, without busy waiting.