1Which header file must be included to use POSIX threads in a C program?
A.<process.h>
B.<thread.h>
C.<sys/thread.h>
D.<pthread.h>
Correct Answer: <pthread.h>
Explanation:
The standard header file for the POSIX thread library is <pthread.h>.
Incorrect! Try again.
2Which compiler flag is typically required to compile a multithreaded program using gcc on Linux?
A.-lpthread
B.-pthread
C.-thread
D.-kthread
Correct Answer: -pthread
Explanation:
The -pthread flag tells the compiler to link the pthread library and configure compilation for threads. -lpthread links the library but doesn't necessarily set preprocessor macros correctly.
Incorrect! Try again.
3What is the return type of the function pthread_create?
A.void
B.pthread_t
C.int
D.void *
Correct Answer: int
Explanation:
pthread_create returns an integer: 0 indicates success, while a non-zero value indicates an error number.
Incorrect! Try again.
4In the function pthread_create, what is the purpose of the fourth argument?
A.It is the argument passed to the start_routine.
B.It is the function pointer to the thread routine.
C.It sets the thread attributes.
D.It stores the thread ID.
Correct Answer: It is the argument passed to the start_routine.
Explanation:
The signature is int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg). The fourth argument is passed to the start routine.
Incorrect! Try again.
5Which function is used to wait for the termination of a specific thread?
A.pthread_exit()
B.pthread_stop()
C.pthread_wait()
D.pthread_join()
Correct Answer: pthread_join()
Explanation:
pthread_join() blocks the calling thread until the specified thread terminates.
Incorrect! Try again.
6What happens if pthread_exit() is called in the main thread of a process, while other threads are still running?
A.The main thread terminates, but other threads continue execution.
B.The entire process terminates immediately.
C.The main thread waits for all other threads to finish.
D.The function returns an error.
Correct Answer: The main thread terminates, but other threads continue execution.
Explanation:
Calling pthread_exit() in main() allows other threads to continue running even after the main thread has finished executing.
Incorrect! Try again.
7Which function returns the thread ID of the calling thread?
A.pthread_getid()
B.pthread_self()
C.pthread_id()
D.getpid()
Correct Answer: pthread_self()
Explanation:
pthread_self() returns the ID of the calling thread.
Incorrect! Try again.
8Which data type is used to identify a thread in POSIX?
A.int
B.pthread_t
C.struct thread
D.void *
Correct Answer: pthread_t
Explanation:
pthread_t is the opaque data type used to identify a thread.
Incorrect! Try again.
9Why is pthread_equal(id1, id2) used instead of the == operator?
A.Because == is slower.
B.Because pthread_t is a float.
C.Because pthread_t is an opaque data type and may be a structure.
D.Because thread IDs are always strings.
Correct Answer: Because pthread_t is an opaque data type and may be a structure.
Explanation:
The implementation of pthread_t varies by system (it could be an integer or a struct), so pthread_equal ensures portability.
Incorrect! Try again.
10What 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.
Correct Answer: A situation where multiple threads access shared data concurrently and the outcome depends on execution order.
Explanation:
A race condition occurs when the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events.
Incorrect! Try again.
11Which of the following is NOT a requirement for a solution to the Critical Section Problem?
The three requirements are Mutual Exclusion, Progress, and Bounded Waiting. FIFO is a scheduling strategy, not a strict requirement for solving the CS problem (though bounded waiting implies no starvation).
Incorrect! Try again.
12A segment of code where shared resources are accessed is called a:
A.Mutex
B.Race condition
C.Atomic region
D.Critical section
Correct Answer: Critical section
Explanation:
The critical section is the part of the code where the process/thread accesses shared variables or resources.
Incorrect! Try again.
13If a variable is incremented () by two threads concurrently without synchronization, what is the problem?
A.Segmentation Fault
B.Starvation
C.Lost Update
D.Deadlock
Correct Answer: Lost Update
Explanation:
Since incrementing is not atomic (Read-Modify-Write), one thread's update might overwrite the other's, leading to a lost update (Race Condition).
Incorrect! Try again.
14Which POSIX function initializes a mutex?
A.pthread_mutex_init()
B.pthread_mutex_start()
C.pthread_init_mutex()
D.pthread_mutex_create()
Correct Answer: pthread_mutex_init()
Explanation:
pthread_mutex_init() is used to initialize a mutex dynamically.
Incorrect! Try again.
15What is the correct macro to statically initialize a mutex?
16What 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 returns an error immediately.
C.The calling thread steals the lock.
D.The calling thread is terminated.
Correct Answer: The calling thread blocks (sleeps) until the mutex is unlocked.
Explanation:
The standard behavior of a blocking lock call is to suspend execution until the resource becomes available.
Incorrect! Try again.
17Which function attempts to lock a mutex but returns immediately if it is already locked?
A.pthread_mutex_lock_nowait()
B.pthread_mutex_test()
C.pthread_mutex_trylock()
D.pthread_mutex_check()
Correct Answer: pthread_mutex_trylock()
Explanation:
pthread_mutex_trylock() is the non-blocking variant. It returns EBUSY if the mutex is locked.
Incorrect! Try again.
18When a thread is finished with a critical section protected by a mutex, it must call:
A.pthread_mutex_release()
B.pthread_mutex_destroy()
C.pthread_mutex_unlock()
D.pthread_mutex_stop()
Correct Answer: pthread_mutex_unlock()
Explanation:
The mutex must be unlocked to allow other threads to enter the critical section.
Incorrect! Try again.
19What is a binary semaphore effectively similar to?
A.A spinlock
B.A monitor
C.A condition variable
D.A mutex
Correct Answer: A mutex
Explanation:
A binary semaphore has values 0 and 1, providing mutual exclusion similar to a mutex, though ownership semantics differ.
Incorrect! Try again.
20Which header file is required for using POSIX semaphores?
A.<semaphore.h>
B.<pthread.h>
C.<sem.h>
D.<sys/sem.h>
Correct Answer: <semaphore.h>
Explanation:
<semaphore.h> defines the sem_t type and related functions.
Incorrect! Try again.
21What is the data type for a POSIX semaphore?
A.mutex_t
B.pthread_mutex_t
C.sem_t
D.semaphore
Correct Answer: sem_t
Explanation:
POSIX semaphores use the sem_t type.
Incorrect! Try again.
22Which function is used to initialize an unnamed semaphore?
A.sem_start()
B.sem_open()
C.sem_create()
D.sem_init()
Correct Answer: sem_init()
Explanation:
sem_init initializes an unnamed semaphore at a given address.
Incorrect! Try again.
23In 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 between processes.
C.The semaphore is shared only between threads of the same process.
D.The semaphore is disabled.
Correct Answer: The semaphore is shared only between threads of the same process.
Explanation:
If pshared is 0, the semaphore is shared between threads of the process. If non-zero, it is shared between processes.
Incorrect! Try again.
24The sem_wait() function corresponds to which theoretical semaphore operation?
A.V operation (Signal)
B.P operation (Proberen/Wait)
C.Init operation
D.Destroy operation
Correct Answer: P operation (Proberen/Wait)
Explanation:
sem_wait decrements the semaphore. This is historically known as the P operation (from Dutch proberen, to test).
Incorrect! Try again.
25The sem_post() function corresponds to which theoretical semaphore operation?
A.V operation (Verhogen/Signal)
B.Test operation
C.Lock operation
D.P operation (Wait)
Correct Answer: V operation (Verhogen/Signal)
Explanation:
sem_post increments the semaphore. This is known as the V operation (from Dutch verhogen, to increase).
Incorrect! Try again.
26If a semaphore value is 0, what does sem_wait() do?
A.It increments the value to 1.
B.It blocks (waits) until the value becomes greater than 0.
C.It returns an error.
D.It destroys the semaphore.
Correct Answer: It blocks (waits) until the value becomes greater than 0.
Explanation:
sem_wait decrements the value. If the value is 0, it blocks until it can successfully decrement (i.e., when another thread posts).
Incorrect! Try again.
27What 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.Mutexes can be used between processes, semaphores cannot.
C.Semaphores are faster.
D.There is no difference.
Correct Answer: Mutexes can only be unlocked by the thread that locked them; Semaphores can be signaled by any thread.
Explanation:
Mutexes imply ownership. Semaphores are signaling mechanisms and do not enforce ownership (any thread can sem_post).
Incorrect! Try again.
28Consider a Counting Semaphore initialized to . If 5 threads call sem_wait() almost simultaneously, how many will immediately proceed?
A.3
B.0
C.1
D.5
Correct Answer: 3
Explanation:
The semaphore starts at 3. The first 3 threads will successfully decrement it (3 -> 2 -> 1 -> 0) and proceed. The 4th and 5th will block.
Incorrect! Try again.
29Which function is used to free the resources associated with a mutex?
A.free()
B.pthread_mutex_destroy()
C.pthread_mutex_delete()
D.pthread_mutex_kill()
Correct Answer: pthread_mutex_destroy()
Explanation:
pthread_mutex_destroy() destroys the mutex object.
Incorrect! Try again.
30Which function allows a thread to separate itself from the main thread, such that its resources are automatically released upon termination?
A.pthread_split()
B.pthread_free()
C.pthread_release()
D.pthread_detach()
Correct Answer: pthread_detach()
Explanation:
pthread_detach() marks the thread as detached. When a detached thread terminates, its resources are automatically released back to the system.
Incorrect! Try again.
31In 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 ensure exclusive access to the buffer when adding or removing items.
D.To count the number of items.
Correct Answer: To ensure exclusive access to the buffer when adding or removing items.
Explanation:
Semaphores track the count of full/empty slots, but the mutex protects the critical section (the actual buffer modification) to prevent data corruption.
Incorrect! Try again.
32Which of the following creates a "Deadlock"?
A.A thread sleeping for 10 seconds.
B.High CPU usage by a thread.
C.A thread waiting for a semaphore initialized to 0.
D.Two threads waiting for a resource held by the other.
Correct Answer: Two threads waiting for a resource held by the other.
Explanation:
This is a circular wait condition, a classic deadlock scenario.
Incorrect! Try again.
33What is the return value of sem_trywait() if the semaphore is currently 0?
A.It blocks until available.
B.-1 and sets errno to EAGAIN.
C.0
D.-1 and sets errno to EINTR.
Correct Answer: -1 and sets errno to EAGAIN.
Explanation:
sem_trywait is non-blocking. If it cannot decrement, it returns -1 immediately indicating the resource is unavailable.
Incorrect! Try again.
34To 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.Use global variables only.
D.Cast them to integers.
Correct Answer: Define a structure containing the arguments and pass a pointer to it.
Explanation:
pthread_create accepts only one void * argument. To pass multiple values, you bundle them in a struct and pass the struct pointer.
Incorrect! Try again.
35What is the danger of passing a pointer to a local variable (on the stack) to a thread created via pthread_create?
A.It is perfectly safe.
B.It causes a compile error.
C.The creating function might return and deallocate the stack before the thread reads the variable.
D.The thread cannot access stack memory.
Correct Answer: The creating function might return and deallocate the stack before the thread reads the variable.
Explanation:
Local variables cease to exist when the function returns. If the thread runs after the parent function returns, it accesses invalid memory.
Incorrect! Try again.
36Which of the following is correct regarding User-level threads vs Kernel-level threads?
A.Blocking one user-level thread never blocks the entire process.
B.User-level threads are managed by the OS kernel.
C.Context switching is faster in User-level threads.
D.POSIX threads on Linux are purely user-level threads.
Correct Answer: Context switching is faster in User-level threads.
Explanation:
User-level threads are managed by a library in user space without kernel intervention, making context switching faster (no mode switch).
Incorrect! Try again.
37What is a Spinlock?
A.A hard disk locking mechanism.
B.A lock where the waiting thread loops (spins) checking the lock condition.
C.A lock where the waiting thread sleeps.
D.A lock that rotates between threads.
Correct Answer: A lock where the waiting thread loops (spins) checking the lock condition.
Explanation:
Spinlocks use busy waiting. They are useful for short wait times but waste CPU cycles.
Incorrect! Try again.
38If 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?
Condition variables allow threads to suspend execution and relinquish the processor until some predicate on shared data is satisfied.
Incorrect! Try again.
39Which function signals a condition variable, waking up at least one waiting thread?
A.pthread_cond_init()
B.pthread_cond_wait()
C.pthread_cond_signal()
D.pthread_cond_broadcast()
Correct Answer: pthread_cond_signal()
Explanation:
pthread_cond_signal unblocks at least one of the threads that are blocked on the specified condition variable.
Incorrect! Try again.
40Why must pthread_cond_wait() be called inside a loop checking the condition?
A.To handle 'spurious wakeups'.
B.Because the syntax requires it.
C.It creates a spinlock.
D.It is not required, an if statement is sufficient.
Correct Answer: To handle 'spurious wakeups'.
Explanation:
Threads might wake up without the condition actually being met (spurious wakeup), so the condition must be re-checked.
Incorrect! Try again.
41When pthread_cond_wait(&cond, &mutex) is called, what happens to the mutex?
A.The mutex is passed to the next thread.
B.The mutex is destroyed.
C.The mutex remains locked by the thread.
D.The mutex is automatically released while the thread waits.
Correct Answer: The mutex is automatically released while the thread waits.
Explanation:
Atomically, the mutex is released and the thread goes to sleep. Upon waking up, the mutex is re-acquired.
Incorrect! Try again.
42What is the maximum value a binary semaphore can hold?
A.Depending on OS
B.Infinity
C.0
D.1
Correct Answer: 1
Explanation:
A binary semaphore can only have values 0 or 1.
Incorrect! Try again.
43Can pthread_join be called on a detached thread?
A.Yes, always.
B.No, it results in undefined behavior or error.
C.Yes, but it returns immediately.
D.Only if the thread has finished.
Correct Answer: No, it results in undefined behavior or error.
Explanation:
Detached threads cannot be joined. They release resources automatically upon termination.
Incorrect! Try again.
44In a multithreaded program, which of the following is shared among all threads?
A.Program Counter
B.Register Set
C.Stack Pointer
D.Global Heap Memory
Correct Answer: Global Heap Memory
Explanation:
Threads within the same process share the code section, data section (global variables), and heap (dynamically allocated memory). Each has its own stack and registers.
Incorrect! Try again.
45If a thread attempts to unlock a mutex that it does not own, what is the result?
A.Undefined behavior (usually an error).
B.The mutex becomes unlocked regardless of ownership.
C.It works successfully.
D.The thread gets ownership.
Correct Answer: Undefined behavior (usually an error).
Explanation:
Unlocking a mutex owned by another thread is undefined behavior in Pthreads (unless it's an error-checking mutex, which returns an error).
Incorrect! Try again.
46Which of the following is thread-safe?
A.Using a global variable as a counter without locks.
B.Using strtok (standard static version).
C.Using strtok_r (reentrant version).
D.Modifying a linked list simultaneously without locks.
Correct Answer: Using strtok_r (reentrant version).
Explanation:
Reentrant functions (_r suffix) are designed to be thread-safe by using caller-provided buffers instead of internal static buffers.
Incorrect! Try again.
47What is the concept of Priority Inversion?
A.The scheduler assigns priorities randomly.
B.A high-priority thread waits for a lower-priority thread holding a lock.
C.A low-priority thread runs faster than a high-priority one.
D.All threads have the same priority.
Correct Answer: A high-priority thread waits for a lower-priority thread holding a lock.
Explanation:
This occurs when a lower-priority task holds a resource needed by a higher-priority task, effectively reducing the higher-priority task's execution speed.
Incorrect! Try again.
48How do Named Semaphores (sem_open) differ from Unnamed Semaphores (sem_init)?
A.Named semaphores are identified by a string name and can be used by unrelated processes.
B.Unnamed semaphores use strings for identification.
C.There is no difference.
D.Named semaphores are faster.
Correct Answer: Named semaphores are identified by a string name and can be used by unrelated processes.
Explanation:
Named semaphores are created in the filesystem namespace (usually under /dev/shm), allowing unrelated processes to access them via name.
Incorrect! Try again.
49Which function closes a named semaphore?
A.sem_destroy()
B.sem_exit()
C.sem_close()
D.sem_end()
Correct Answer: sem_close()
Explanation:
sem_close closes the link to the named semaphore, though it doesn't unlink (remove) it from the system.
Incorrect! Try again.
50What happens if you free() a structure that contains an active mutex?
A.The OS cleans it up safely.
B.The program crashes immediately.
C.The mutex is automatically destroyed.
D.Undefined behavior; the mutex should be destroyed first.
Correct Answer: Undefined behavior; the mutex should be destroyed first.
Explanation:
You must explicitly call pthread_mutex_destroy before freeing the memory where the mutex resides.