Unit 5 - Practice Quiz

CSE325

1 Which header file must be included to use the POSIX thread library in a C program?

A. <thread.h>
B. <pthread.h>
C. <process.h>
D. <unistd.h>

2 When compiling a C program that uses POSIX threads with gcc, which flag is typically required to link the library?

A. -lthread
B. -lpthread or -pthread
C. -posix
D. -mkthread

3 What is the return type of the function passed to pthread_create as the start routine?

A. void
B. int
C. void *
D. pthread_t

4 In the function pthread_create, what is the purpose of the fourth argument?

A. It specifies the thread attributes.
B. It is the pointer to the function to be executed.
C. It stores the thread ID.
D. It is the argument passed to the start routine.

5 Which function is used to wait for the termination of a specific thread?

A. pthread_wait()
B. pthread_join()
C. pthread_exit()
D. pthread_stop()

6 What happens if the main() function returns before the threads it created have finished execution (and pthread_exit() is not called in main)?

A. The created threads continue running in the background.
B. The created threads become zombies.
C. The entire process terminates, killing all threads.
D. The main function waits automatically.

7 Which of the following resources is shared among all threads in a process?

A. Program Counter (PC)
B. Stack pointer
C. Registers
D. Global variables and Heap memory

8 What is the function pthread_self() used for?

A. To terminate the calling thread.
B. To return the ID of the calling thread.
C. To create a copy of the calling thread.
D. To detach the calling thread.

9 Which function allows a thread to terminate itself without terminating the entire process?

A. exit()
B. return (from main)
C. pthread_exit()
D. pthread_kill()

10 A Race Condition occurs when:

A. Two threads execute different functions simultaneously.
B. Multiple threads access shared data concurrently and at least one is writing.
C. Threads communicate using message passing.
D. A thread waits indefinitely for a resource.

11 The segment of code where shared resources are accessed and modified is called:

A. Race Section
B. Critical Section
C. Entry Section
D. Remainder Section

12 Which of the following is NOT a requirement for a solution to the Critical Section problem?

A. Mutual Exclusion
B. Progress
C. Bounded Waiting
D. First-In-First-Out (FIFO) execution

13 Mutual Exclusion implies that:

A. Multiple threads can be in their critical sections simultaneously if they are reading.
B. If a thread is executing in its critical section, no other thread can be executing in their corresponding critical section.
C. Threads must take turns strictly.
D. All threads must share the same priority.

14 What is a Mutex?

A. A mechanism to enforce First-Come-First-Serve scheduling.
B. A synchronization primitive used to enforce mutual exclusion.
C. A type of shared memory segment.
D. A function to create threads.

15 What function initializes a POSIX mutex?

A. pthread_mutex_create()
B. pthread_mutex_init()
C. pthread_mutex_start()
D. pthread_mutex_alloc()

16 If a thread attempts to lock a mutex that is already locked by another thread using pthread_mutex_lock, what happens?

A. The calling thread returns an error immediately.
B. The calling thread terminates.
C. The calling thread blocks (waits) until the mutex becomes available.
D. The mutex is forcibly unlocked.

17 Which function allows a thread to attempt to lock a mutex without blocking if it is already locked?

A. pthread_mutex_lock()
B. pthread_mutex_trylock()
C. pthread_mutex_test()
D. pthread_mutex_check()

18 What is the correct sequence of operations to protect a critical section using a mutex?

A. Unlock -> Critical Section -> Lock
B. Lock -> Critical Section -> Unlock
C. Critical Section -> Lock -> Unlock
D. Lock -> Unlock -> Critical Section

19 What happens if a thread forgets to call pthread_mutex_unlock after finishing the critical section?

A. The OS automatically unlocks it after a timeout.
B. Other threads waiting for that mutex will remain blocked indefinitely (Deadlock/Starvation).
C. The mutex destroys itself.
D. The program crashes immediately.

20 In the context of semaphores, what does the variable represent?

A. A string identifier.
B. A pointer to the thread.
C. An integer value representing available resources.
D. A floating-point priority value.

21 Which header file is required for POSIX semaphores?

A. <semaphore.h>
B. <pthread.h>
C. <sem.h>
D. <sys/sem.h>

22 What are the two atomic operations defined on a semaphore?

A. Read and Write
B. Wait (P) and Post (V)
C. Start and Stop
D. Lock and Unlock

23 If a semaphore has a value of 0, what happens when sem_wait(&S) is called?

A. becomes -1.
B. remains 0 and the calling thread proceeds.
C. The calling thread blocks until .
D. The program terminates with an error.

24 What does the function sem_post(&S) do?

A. Decrements by 1.
B. Increments by 1 and wakes up a waiting thread if any.
C. Sets to 0.
D. Destroys the semaphore.

25 What is the primary difference between a Mutex and a Binary Semaphore?

A. There is no difference.
B. A mutex can be unlocked by any thread, while a semaphore has ownership.
C. A mutex has ownership (only the locker can unlock), while a semaphore does not.
D. Semaphores are faster than mutexes.

26 In sem_init(sem_t *sem, int pshared, unsigned int value), what does pshared = 0 indicate?

A. The semaphore is shared between processes.
B. The semaphore is shared only between threads of the same process.
C. The semaphore value is initialized to 0.
D. The semaphore is read-only.

27 Consider the Producer-Consumer problem. Which synchronization primitive is best suited to track the number of full slots in the buffer?

A. Mutex
B. Counting Semaphore
C. Spinlock
D. Binary Semaphore

28 What is a Deadlock in the context of multithreading?

A. When a thread finishes execution.
B. When two or more threads are waiting indefinitely for an event that can be caused only by one of the waiting threads.
C. When a thread uses too much CPU.
D. When the operating system stops a process.

29 The operation on a semaphore corresponds to which POSIX function?

A. sem_wait()
B. sem_trywait()
C. sem_post()
D. sem_getvalue()

30 Which of the following creates a thread that is 'detached' (resources released immediately upon termination)?

A. Calling pthread_detach() on the thread.
B. Calling pthread_join().
C. Creating the thread with default attributes.
D. Using sem_post().

31 What is the result of pthread_mutex_destroy(&mutex) if the mutex is currently locked?

A. The mutex is destroyed and the lock is released.
B. The behavior is undefined (often returns an error like EBUSY).
C. The calling thread waits until it is unlocked.
D. The mutex is reset to initial state.

32 In the Dining Philosophers problem, using a single mutex to protect the entire table (all forks) results in:

A. Maximum concurrency.
B. Deadlock.
C. Serialization of eating (only one philosopher eats at a time).
D. Starvation of the middle philosopher.

33 What is the initial value of a semaphore used to implement a Mutex (binary semaphore)?

A.
B. 1
C. 2
D. -1

34 Which variable type is used for a POSIX thread identifier?

A. int
B. pid_t
C. pthread_t
D. thread_id

35 What is the output of the following pseudocode: . Thread A: . Thread B: . (Assume load/store architecture and no synchronization).

A. Always 2
B. Always 1
C. Either 1 or 2
D. Always 0

36 Which synchronization tool essentially generalizes the mutex lock to allow concurrent accesses?

A. Condition Variable
B. Monitor
C. Counting Semaphore
D. Spinlock

37 Why might pthread_mutex_trylock be preferred over pthread_mutex_lock?

A. To ensure the thread never fails to get the lock.
B. To avoid blocking the thread if the lock is unavailable, allowing it to do other work.
C. It is faster to execute.
D. It automatically unlocks the mutex.

38 Which of the following is true about User-Level Threads (ULT) vs Kernel-Level Threads (KLT)?

A. ULTs are managed by the kernel.
B. Blocking system calls in a ULT block the entire process (if the kernel is unaware of threads).
C. KLTs are faster to create than ULTs.
D. POSIX threads are always User-Level Threads.

39 When passing arguments to a thread function, why is passing the address of a loop variable (e.g., &i) dangerous?

A. The compiler cannot handle pointers in threads.
B. The loop variable i may change value before the thread starts execution.
C. It consumes too much stack memory.
D. The thread ID will be invalid.

40 Which Pthread function is used to initialize a condition variable?

A. pthread_cond_init
B. pthread_cond_start
C. pthread_cv_init
D. pthread_init_cond

41 What determines if an operation is atomic?

A. It takes less than 1ms to execute.
B. It executes as a single, indivisible unit without interruption.
C. It uses a mutex.
D. It is written in C.

42 A spinlock is a type of lock where:

A. The thread sleeps while waiting.
B. The thread repeatedly checks (busy waits) if the lock is available.
C. The thread yields the CPU immediately.
D. The lock rotates between threads.

43 Which Pthread function yields the processor to another thread?

A. pthread_yield() (or sched_yield())
B. pthread_pause()
C. pthread_wait()
D. pthread_stop()

44 What is the main disadvantage of using sem_unlink()?

A. It stops the semaphore immediately.
B. It removes the name of a named semaphore, but existing references remain valid.
C. It crashes the system.
D. It resets the semaphore count.

45 In the Reader-Writer problem, a Writer requires:

A. Shared access.
B. Exclusive access to the database.
C. No access.
D. Read-only access.

46 What happens if you call pthread_join on a thread that has already been detached?

A. It joins successfully.
B. It creates a new thread.
C. It results in undefined behavior (usually an error).
D. It forces the thread to re-attach.

47 The pthread_attr_init function is used to:

A. Initialize a thread directly.
B. Initialize a thread attribute object with default values.
C. Start a thread.
D. Set the thread priority.

48 Which of the following is a classic example of a synchronization problem?

A. The Hello World Problem.
B. The Traveling Salesman Problem.
C. The Bounded-Buffer (Producer-Consumer) Problem.
D. The Knapsack Problem.

49 If a mutex is 'recursive', it means:

A. It calls itself.
B. The same thread can lock it multiple times without deadlocking.
C. It can be locked by multiple threads at once.
D. It is used for recursive functions only.

50 In pthread_create, if the attribute argument is NULL, what happens?

A. The thread is not created.
B. The thread is created with default attributes.
C. The thread is created as a zombie.
D. The compiler throws an error.