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. <pthread.h>
B. <process.h>
C. <sys/thread.h>
D. <thread.h>

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

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

3 What is the return type of the function pthread_create?

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

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

A. The function returns an error.
B. The main thread waits for all other threads to finish.
C. The entire process terminates immediately.
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_getid()
C. pthread_self()
D. getpid()

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

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

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

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

10 What is a Race Condition?

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

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

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

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

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

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

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

14 Which POSIX function initializes a mutex?

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

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

A. STATIC_MUTEX
B. PTHREAD_MUTEX_INITIALIZER
C. PTHREAD_MUTEX_INIT
D. 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 blocks (sleeps) until the mutex is unlocked.
B. The calling thread steals the lock.
C. The calling thread is terminated.
D. The calling thread returns an error immediately.

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

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

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

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

19 What is a binary semaphore effectively similar to?

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

20 Which header file is required for using POSIX semaphores?

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

21 What is the data type for a POSIX semaphore?

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

22 Which function is used to initialize an unnamed semaphore?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

32 Which of the following creates a "Deadlock"?

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

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

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

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

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

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

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

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. User-level threads are managed by the OS kernel.
C. Blocking one user-level thread never blocks the entire process.
D. POSIX threads on Linux are purely user-level threads.

37 What is a Spinlock?

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

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. Condition Variables (pthread_cond_t)
C. Spinlock
D. Another Mutex

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

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

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

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

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 is passed to the next thread.
D. The mutex remains locked by the thread.

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

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

43 Can pthread_join be called on a detached thread?

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

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

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

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

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

46 Which of the following is thread-safe?

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

47 What is the concept of Priority Inversion?

A. A low-priority thread runs faster than a high-priority one.
B. A high-priority thread waits for a lower-priority thread holding a lock.
C. All threads have the same priority.
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_destroy()
B. sem_close()
C. sem_exit()
D. sem_end()

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

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