CSCI 4061 Quiz 2

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

1/70

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 10:20 AM on 4/16/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

71 Terms

1
New cards

pthread_create signature

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void (start_routine)(void *), void *arg)

2
New cards

pthread_join signature

int pthread_join(pthread_t thread, void **value_ptr)

3
New cards

pthread_cond_wait signature

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)

4
New cards

sem_init signature

int sem_init(sem_t *sem, int pshared, unsigned int value)

5
New cards

pthread_mutex_lock signature

int pthread_mutex_lock(pthread_mutex_t *mutex)

6
New cards

Static mutex initializer

PTHREAD_MUTEX_INITIALIZER

7
New cards

Static condition variable initializer

PTHREAD_COND_INITIALIZER

8
New cards

sem_init second argument (pshared)

0 = shared within process only; nonzero = shared across processes

9
New cards

pthread return convention

Returns 0 on success, nonzero error code on failure. Does NOT set errno. Never returns EINTR.

10
New cards

Thread

An independent stream of instructions within a process; has its own PC, stack, registers, signal mask; shares address space, globals, heap, file descriptors, code with other threads in the process

11
New cards

Race condition

A situation where program outcome depends on the relative execution order of concurrent threads/processes accessing a shared resource

12
New cards

Critical section

A code segment that requires mutual exclusion; all executions are serialized

13
New cards

Atomic operation

An indivisible operation that either executes completely or not at all, and cannot be interrupted

14
New cards

Mutual exclusion

One-at-a-time access to a resource or code segment; only one thread permitted at a time

15
New cards

Mutex

A binary lock (locked/unlocked) where at most one thread owns it at a time; used for short critical sections

16
New cards

Condition variable

A synchronization primitive that atomically unlocks a mutex and blocks a thread until signaled

17
New cards

Semaphore

A synchronization primitive with an integer value; sem_wait decrements (blocks if would go below 0); sem_post increments

18
New cards

Deadlock

A situation where no thread can make progress because each is waiting on a resource held by another

19
New cards

Multiprogramming vs multithreading

Multiprogramming = multiple processes sharing CPU (disjoint address spaces); multithreading = multiple threads in one process (shared address space)

20
New cards

Joinable vs detached thread

Joinable: holds resources until another thread joins; Detached: releases resources immediately on exit, cannot be joined

21
New cards

Three ways to end thread execution

exit() kills entire process; pthread_exit() kills only calling thread; return from start routine = pthread_exit with return value

22
New cards

Shared between threads in a process

Address space, global variables, heap, open file descriptors, code, data, signal handlers

23
New cards

Private to each thread

Program counter, stack, registers, signal mask, errno (in modern implementations)

24
New cards

Three operations hidden inside counter++

Load counter to register; add 1 to register; store register back to counter

25
New cards

Three steps of a bank withdrawal race

Read balance; modify (subtract); write back — another thread can interleave between any two

26
New cards

Check-then-act race

Two threads both pass a condition check before either acts, both then act based on stale assumption (e.g., both withdraw after seeing sufficient balance)

27
New cards

Four parts of a critical section

Entry section (request); critical section (access); exit section (release); remainder section (concurrent resumes)

28
New cards

Three requirements for correct critical section solution

Mutual exclusion; progress (waiting thread can enter if none inside); fairness (no indefinite postponement)

29
New cards

Coffman condition 1

Mutual exclusion — at least one resource held in non-sharable mode

30
New cards

Coffman condition 2

Hold and wait — thread holds one resource while waiting for another

31
New cards

Coffman condition 3

No preemption — only the holder can release a resource; cannot be forcibly taken

32
New cards

Coffman condition 4

Circular wait — a cycle of threads each waiting for a resource held by the next

33
New cards

Deadlock prevention condition

ALL four Coffman conditions must hold simultaneously; eliminating any one prevents deadlock

34
New cards

Single-lock strategy eliminates

Hold-and-wait AND circular wait (only one lock exists)

35
New cards

Backoff (trylock) strategy eliminates

Hold-and-wait (thread releases everything it holds if it cannot acquire next lock)

36
New cards

Total lock ordering eliminates

Circular wait (fixed global acquisition order makes cycles impossible)

37
New cards

Ostrich algorithm

Ignore deadlocks; handle rare occurrences by rebooting or killing processes; used by most real OSes

38
New cards

Which Coffman conditions are hard to eliminate

Mutual exclusion (inherent to protected resources) and no preemption (forcible lock-stealing would leave data inconsistent)

39
New cards

pthread_cond_signal vs pthread_cond_broadcast

Signal wakes ONE waiting thread; broadcast wakes ALL waiting threads

40
New cards

When to use broadcast over signal

When multiple waiters may need to proceed (barriers, wait groups) or when unsure how many waiters exist

41
New cards

Why while loop around pthread_cond_wait

(1) POSIX allows spurious wakeups; (2) predicate may have become false again between signal and reacquisition of mutex

42
New cards

What pthread_cond_wait does atomically

Releases the mutex AND blocks the thread as one indivisible operation

43
New cards

Bug if unlock and block were separate in cond_wait

Another thread could signal between unlock and block, causing signal to be lost forever

44
New cards

Mutex vs semaphore: ownership

Mutex can only be unlocked by the thread that locked it; semaphore has no ownership — any thread can sem_post

45
New cards

Mutex vs semaphore: counting

Mutex is strictly binary; semaphore can be initialized to any non-negative N (counting semaphore)

46
New cards

Semaphore initialized to 1 acts like

A mutex (binary lock)

47
New cards

Semaphore initialized to 0 is used for

Signaling/ordering — a thread blocks on sem_wait until another thread sem_posts

48
New cards

exit() from any thread

Terminates the entire process; all threads die immediately

49
New cards

pthread_exit() from main thread

Only main thread exits; process continues running until last thread terminates

50
New cards

return from start routine

Equivalent to pthread_exit with the returned pointer as value

51
New cards

pthread_equal vs ==

pthread_t may be a struct on some implementations, so always use pthread_equal to compare thread IDs

52
New cards

pthread_self()

Returns the calling thread's own thread ID

53
New cards

pthread_join(pthread_self(), NULL)

Causes deadlock — thread waits for itself to finish

54
New cards

Never return from a thread

A pointer to an automatic (stack-allocated) local variable — stack is deallocated on thread exit, pointer becomes dangling

55
New cards

Safe return values from a thread

malloc'd memory, static variables, string literals, or memory provided by the creator via the argument

56
New cards

errno in multithreaded programs

Modern implementations make errno a per-thread macro, so each thread has its own effective copy

57
New cards

Thread-unsafe functions (examples)

strerror, rand, ctime, gmtime, localtime, readdir, strtok

58
New cards

Thread-safe function naming convention

_r suffix (e.g., strerror_r, rand_r)

59
New cards

Program 12.9 bug

All threads share &i pointer; by the time threads dereference, i has changed (often all see final loop value)

60
New cards

Fix for shared-pointer-to-loop-variable bug

Give each thread its own storage: per-thread array element, malloc'd struct, or cast scalar directly to void*

61
New cards

pthread_cancel behavior

Asynchronous request; returns immediately; target terminates based on its cancellability state (ENABLE/DISABLE) and type (DEFERRED/ASYNCHRONOUS)

62
New cards

Default cancellation type

Deferred — thread only acts on cancellation at designated cancellation points (certain blocking calls)

63
New cards

Mutex self-deadlock

Locking a mutex already held by the calling thread; POSIX says MAY return EDEADLK but detection not required

64
New cards

Producer-consumer with one cond variable problem

Signal may wake wrong kind of waiter (another producer when consumer needed); broadcast works but wastes CPU

65
New cards

Producer-consumer with two cond variables

'items' signaled when item added (wakes consumers); 'slots' signaled when slot freed (wakes producers)

66
New cards

Busy-waiting inside a held lock

Catastrophic deadlock — lock-holder spins forever, no other thread can acquire lock to change the condition

67
New cards

Why mutex alone insufficient for producer-consumer

Waiting for buffer to become non-empty/non-full can be unbounded in duration; need condition variables or semaphores

68
New cards

Wait group purpose (Lab 9)

Lets one thread block until N other threads complete their work (Go-style synchronization primitive)

69
New cards

Why waitgroup_done uses broadcast

Multiple threads may be waiting on the wait group; signal would only wake one and leave others stuck

70
New cards

Lab 7 key insight

When work partitions cleanly (distinct rows of output), no mutex needed — threads write to disjoint memory

71
New cards

Correct mutex-protected check-and-act pattern

lock → check condition → act if condition holds → unlock (entire sequence under one lock)