Unit 5: Multithreading & Synchronization - Practice Quiz

CSE325 — Operating Systems Laboratory 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which header file must be included to use POSIX threads in a C program?

A. <sys/thread.h>
B. <thread.h>
C. <pthread.h>
D. <process.h>

2 Which compiler flag is typically required to compile a multithreaded program using gcc on Linux?

A. -lpthread
B. -kthread
C. -pthread
D. -thread

3 What is the return type of the function pthread_create?

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

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

A. It is the function pointer to the thread routine.
B. It stores the thread ID.
C. It sets the thread attributes.
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_exit()
C. pthread_stop()
D. pthread_join()

6 What happens if pthread_exit() is called in the main thread of a process, while other threads are still running?

A. The entire process terminates immediately.
B. The function returns an error.
C. The main thread waits for all other threads to finish.
D. The main thread terminates, but other threads continue execution.

7 Which function returns the thread ID of the calling thread?

A. pthread_id()
B. pthread_self()
C. getpid()
D. pthread_getid()

8 Which data type is used to identify a thread in POSIX?

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

9 Why is pthread_equal(id1, id2) used instead of the == operator?

A. Because thread IDs are always strings.
B. Because pthread_t is a float.
C. Because pthread_t is an opaque data type and may be a structure.
D. Because == is slower.

10 What is a Race Condition?

A. A situation where multiple threads access shared data concurrently and the outcome depends on execution order.
B. A situation where a thread waits indefinitely for a resource.
C. A mechanism to speed up thread execution.
D. A condition where threads race to finish execution first.

11 Which of the following is NOT a requirement for a solution to the Critical Section Problem?

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

12 A segment of code where shared resources are accessed is called a:

A. Critical section
B. Race condition
C. Mutex
D. Atomic region

13 If a variable is incremented () by two threads concurrently without synchronization, what is the problem?

A. Deadlock
B. Starvation
C. Lost Update
D. Segmentation Fault

14 Which POSIX function initializes a mutex?

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

15 What is the correct macro to statically initialize a mutex?

A. MUTEX_INIT
B. PTHREAD_MUTEX_INITIALIZER
C. STATIC_MUTEX
D. PTHREAD_MUTEX_INIT

16 What happens if a thread calls pthread_mutex_lock() on a mutex that is already locked by another thread?

A. The calling thread returns an error immediately.
B. The calling thread is terminated.
C. The calling thread blocks (sleeps) until the mutex is unlocked.
D. The calling thread steals the lock.

17 Which function attempts to lock a mutex but returns immediately if it is already locked?

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

18 When a thread is finished with a critical section protected by a mutex, it must call:

A. pthread_mutex_destroy()
B. pthread_mutex_stop()
C. pthread_mutex_unlock()
D. pthread_mutex_release()

19 What is a binary semaphore effectively similar to?

A. A condition variable
B. A spinlock
C. A mutex
D. A monitor

20 Which header file is required for using POSIX semaphores?

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

21 What is the data type for a POSIX semaphore?

A. pthread_mutex_t
B. sem_t
C. semaphore
D. mutex_t

22 Which function is used to initialize an unnamed semaphore?

A. sem_open()
B. sem_init()
C. sem_start()
D. sem_create()

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

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

24 The sem_wait() function corresponds to which theoretical semaphore operation?

A. V operation (Signal)
B. P operation (Proberen/Wait)
C. Destroy operation
D. Init operation

25 The sem_post() function corresponds to which theoretical semaphore operation?

A. Test operation
B. V operation (Verhogen/Signal)
C. P operation (Wait)
D. Lock operation

26 If a semaphore value is 0, what does sem_wait() do?

A. It destroys the semaphore.
B. It blocks (waits) until the value becomes greater than 0.
C. It increments the value to 1.
D. It returns an error.

27 What is the primary difference between pthread_mutex_lock and sem_wait on a binary semaphore?

A. Mutexes can only be unlocked by the thread that locked them; Semaphores can be signaled by any thread.
B. Semaphores are faster.
C. Mutexes can be used between processes, semaphores cannot.
D. There is no difference.

28 Consider a Counting Semaphore initialized to . If 5 threads call sem_wait() almost simultaneously, how many will immediately proceed?

A. 0
B. 1
C. 3
D. 5

29 Which function is used to free the resources associated with a mutex?

A. pthread_mutex_destroy()
B. pthread_mutex_delete()
C. pthread_mutex_kill()
D. free()

30 Which function allows a thread to separate itself from the main thread, such that its resources are automatically released upon termination?

A. pthread_detach()
B. pthread_free()
C. pthread_split()
D. pthread_release()

31 In the Producer-Consumer problem, what is the purpose of the mutex?

A. To signal when the buffer is empty.
B. To count the number of items.
C. To signal when the buffer is full.
D. To ensure exclusive access to the buffer when adding or removing items.

32 Which of the following creates a "Deadlock"?

A. Two threads waiting for a resource held by the other.
B. A thread sleeping for 10 seconds.
C. High CPU usage by a thread.
D. A thread waiting for a semaphore initialized to 0.

33 What is the return value of sem_trywait() if the semaphore is currently 0?

A. -1 and sets errno to EAGAIN.
B. -1 and sets errno to EINTR.
C. It blocks until available.
D. 0

34 To pass multiple arguments to a thread function, one should:

A. Pass them as separate arguments to pthread_create.
B. Use global variables only.
C. Cast them to integers.
D. Define a structure containing the arguments and pass a pointer to it.

35 What is the danger of passing a pointer to a local variable (on the stack) to a thread created via pthread_create?

A. It causes a compile error.
B. The thread cannot access stack memory.
C. It is perfectly safe.
D. The creating function might return and deallocate the stack before the thread reads the variable.

36 Which of the following is correct regarding User-level threads vs Kernel-level threads?

A. Context switching is faster in User-level threads.
B. Blocking one user-level thread never blocks the entire process.
C. User-level threads are managed by the OS kernel.
D. POSIX threads on Linux are purely user-level threads.

37 What is a Spinlock?

A. A hard disk locking mechanism.
B. A lock that rotates between threads.
C. A lock where the waiting thread sleeps.
D. A lock where the waiting thread loops (spins) checking the lock condition.

38 If you want to synchronize threads based on a condition (e.g., 'Wait until Queue is not empty'), which mechanism is best suited alongside a mutex?

A. Global Boolean Flag
B. Another Mutex
C. Condition Variables (pthread_cond_t)
D. Spinlock

39 Which function signals a condition variable, waking up at least one waiting thread?

A. pthread_cond_signal()
B. pthread_cond_init()
C. pthread_cond_wait()
D. pthread_cond_broadcast()

40 Why must pthread_cond_wait() be called inside a loop checking the condition?

A. Because the syntax requires it.
B. It is not required, an if statement is sufficient.
C. To handle 'spurious wakeups'.
D. It creates a spinlock.

41 When pthread_cond_wait(&cond, &mutex) is called, what happens to the mutex?

A. The mutex is automatically released while the thread waits.
B. The mutex is destroyed.
C. The mutex remains locked by the thread.
D. The mutex is passed to the next thread.

42 What is the maximum value a binary semaphore can hold?

A. Infinity
B. Depending on OS
C. 1
D. 0

43 Can pthread_join be called on a detached thread?

A. Only if the thread has finished.
B. Yes, but it returns immediately.
C. No, it results in undefined behavior or error.
D. Yes, always.

44 In a multithreaded program, which of the following is shared among all threads?

A. Global Heap Memory
B. Register Set
C. Stack Pointer
D. Program Counter

45 If a thread attempts to unlock a mutex that it does not own, what is the result?

A. The mutex becomes unlocked regardless of ownership.
B. It works successfully.
C. The thread gets ownership.
D. Undefined behavior (usually an error).

46 Which of the following is thread-safe?

A. Using a global variable as a counter without locks.
B. Using strtok_r (reentrant version).
C. Modifying a linked list simultaneously without locks.
D. Using strtok (standard static version).

47 What is the concept of Priority Inversion?

A. A high-priority thread waits for a lower-priority thread holding a lock.
B. All threads have the same priority.
C. A low-priority thread runs faster than a high-priority one.
D. The scheduler assigns priorities randomly.

48 How do Named Semaphores (sem_open) differ from Unnamed Semaphores (sem_init)?

A. There is no difference.
B. Named semaphores are faster.
C. Unnamed semaphores use strings for identification.
D. Named semaphores are identified by a string name and can be used by unrelated processes.

49 Which function closes a named semaphore?

A. sem_end()
B. sem_close()
C. sem_exit()
D. sem_destroy()

50 What happens if you free() a structure that contains an active mutex?

A. Undefined behavior; the mutex should be destroyed first.
B. The program crashes immediately.
C. The mutex is automatically destroyed.
D. The OS cleans it up safely.