LU8 Thread Programming Flashcards

1
Q

What is a thread?

A

The smallest unit of execution within a process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do threads improve program performance?

A

perform multithreading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are multiple strands of execution in a single program called?

A

Threads

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When you invoke a program with threads, what does Linux create first?

A

A new process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Initially, how many threads does a Linux process have?

A

One

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

True or False: A single process can have multiple threads executing different parts of the program at the same time.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Compared to processes, what is one key advantage of using threads for concurrent execution?

A
  • lightweight
  • less overhead for creation/termination/context switching.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What part of thread process do threads not share?

A

Stack Pointer and registers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What part of process do threads share?

A

Code, Heap, Static Data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does stack pointer points to?

A

Points to the current stack where local variables, function parameters, and return addresses are stored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are some of the disadvantages of processes vs threads

A

Processes:
* do not share resources very well
* context switching cost is very high

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If a crash happens on a thread, what can potentially happen?

A

Crash the entire process!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are three benefits of using threads?

A
  • lightweight
  • Takes less time to create/terminate/context switching
  • inter-thread communication without invoking the kernel
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Under what criteria is a task suitable for threading?

A
  • Has multiple parallel sub-tasks, blocking and long waits,
  • use many CPU cycles
  • needs to respond to asynchronous event
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe a scenario of when a thread can be use.

A
  • Window manager
  • multhreaded server
  • spreadsheet
  • etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What library do you need to use if you want to code with threads?

A

libpthread for POSIX threads

POSIX: portable operating system interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the four major groups functions to handle the race condition in multithreading?

A
  • Thread management (Creating, Detaching, Joining, Setting Attributes)
  • Mutexes (Creating, Destroying, Locking, Unlocking)
  • Condition variables (Creating, Destroying, Waiting, Signaling)
  • Synchronization (Read/Write Locks, Barriers)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the purpose of Thread management in threads?

A

Routines that work directly on threads - creating, detaching, joining, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the purpose of Mutexes in threads?

A

synchronization

ensure that only one thread can access a shared resource at any given time. This prevents race conditions and data corruption by enforcing mutual exclusion.

think of public restroom

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Name a few functions you can do with Mutex.

A

creating, destroying, locking and unlocking mutexes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the purpose of Condition variables?

A

Routines that address communications between threads that share a mutex.

This group includes functions to create, destroy, wait and signal based upon specified variable values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Is a routine to manage read/write lock a part of Synchronization thread?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What API standard is POSIX threads a part of?

A

Standardized portable thread API

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What header file is needed for POSIX threads?

A

<pthread.h>
</pthread.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How does the function `*thread_function (void *arg)` differ than the usual `*function_name()` ``` int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); ```
pthread_t object that we can use to refer to the thread afterward-function to call and a parameter to pass to it
26
What does the function res = pthread_join(a_thread, &thread_result) do?
1. This function will **wait** until the other thread terminates before it returns. 2. It then **return** the value 3. and **exits**.
27
what does pthread_create argument thread do?
return the ID of the newly created thread
28
What does pthread_create argument attr do?
An opaque attribute object that may be used to set thread attributes
29
Where is the value stored?
start_routine
30
Which value will the thread execute?
start_routine
31
How do two threads synchronize and protect?
semaphores and mutexes
32
What are some advantages of threads rather than new process?
faster communication
33
How do we prevent race condition on a thread?
Mutexes
34
Does a thread create additional thread?
Yes * concurrency/parallelism * asynchronous operations * responsiveness
35
what does inter-process communication require
[ SPMs ] Requires specific mechanisms like: * signals * pipes * message queue * shared memory
36
What does a semaphore act as around piece of code?
Act as gatekeepers
37
What does mutex act as around piece of code?
Act as mutual exclusion device to protect sections of code
38
A single thread can access _______ time?
Once at a time
39
Is it better to protect sections of code via counting a semaphore?
No
40
How do one obtain mutex lock?
pthread_mutex_lock
41
A mutex will act like a _______
Lock
42
Can mutex be an array?
Yes it refers to as multiple mutex objects that protect different resources.
43
If there are several threads waiting, what will the program do?
One wins, the rest block, queue blocked thread
44
True or False: When multiple threads are involved, it is typical to create and initialize a mutex variable.
True
45
What is the command line compilation required to used pthread_mutex_unlock?
gcc -o helloWorld helloWorld.c -lpthread
46
How do I unlock a mutex?
pthread_mutex_unlock
47
What are the different Returns for Creating a Mutex
0 on success, Error number on failure
48
EAGAIN mean?
The system lacked the necessary resources
49
ENOMEM mean?
Insufficient memory
50
EPERM mean?
Caller does not have privileges
51
EBUSY mean?
An attempt to re-initialise a mutex
52
EINVAL mean?
The value specified by attr is invalid
53
Is creating new threads good?
Depends on the hardware
54
What is the primary function of mutexes in multithreaded programming?
* To protect shared data * To prevent race conditions
55
In multithreading, what does a 'race condition' refer to?
Unpredictable behavior due to threads accessing shared data concurrently.
56
What can you pass to the parameters in pthread?
NULL or attribute object
57
What is a signal interrupt?
Inter-thread communication
58
Is TCP socket needed for internal processing?
No
59
To fix the race condition with counter, should you lock and unlock
Yes
60
If the mutex are never unlocked, is there an issue?
Yes (deadlock)
61
What will happen if a mutex is destroyed before unlocked
Undefined outcome
62
63
Multiple strands of execution in a single program is know as...
Threads
64
When multiple threads access shared variables, it can result in...
Race conditions
65
True or False: Thread is lighter than processes
True
66
Are data and stack shared within threads?
Data is shared, but stack is not
67
What is mutual exclusion mechanism?
Mutex
68
What value should we pass through arg in pthread to ensure data
A reference
69
Which operation should you use to unlock a mutex?
pthread_mutex_unlock
70
True or False, shared memory has some type of mutex?
True
71
If mutex is destroyed, do I have access?
No
72
Does each thread have unique Stack and data?
Unique Stack
73
What is one function to create threads?
pthread_create
74
What is one function to create mutex lock?
pthread_mutex_init
75
What do you need to do after the work with mutex is done?
Destroy the mutex object
76
Threads communicates by...
Sharing Memory
77
Each process has its own
Stack pointer (SP), and program counter (PC)
78
True or False: Threads can be created with mutex attribute
True
79
Explain the importance of synchronizing data access among threads in a single process.
To prevent **data corruption** and ensure **consistent** results.
80
What is the benefit of inter-thread communication not invoking the kernel?
Increased performance due to lower overhead.
81
Can threads exist without process
No
82
True/False: All threads run the different program in the same process
False
83
Mutex variable acts like a
Lock
84
What are functions associated with thread
Creating, detaching, joining, set or query, thread attribute
85
What is a mutex and when would you use one?
Used when multiple writes occur.
86
Does process or threads requires more memory
Process
87
Threads can _______ to create more threads
Fork/Lead
88
Thread has its own stack pointer for ______
Local variable function