L11: POSIX Threads Flashcards

1
Q

What are Threads?

A
  • Lightweight Processes
  • Only local variables in a function are specific to a thread
    • Each thread has it’s own stack
  • Most other data is shared between threads
    • global variables
    • the heap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Posix Threads:

  • Standard
  • Library
A

Standard:

pthreads

Library:

pthread.h

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

Reasons to

use Threads

A
  • Efficiency
  • Ease of data sharing
  • Leverages parallelism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Common uses of

Parallelism

A
  • Overlapping I/O
    • Can perform long I/O and CPU tasks at the same time
  • Asynchronous Events
    • Wait for a response AND do something else at the same time
  • Real-Time Scheduling
    • Quickly respond to important tasks
  • Utilize Multiple Processors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Threads

vs

Processes

A
  • Threads use less system resources for the same task
  • But, thread may require more user space, depending on the implementation
  • Threads within the same process share everything but the stack and processor state
    • Many process-based mechanisms have thread based equivalents that can be used:
      • non-blocking I/O
      • Shared Memory (IPC)
      • signals
      • jmp() function
  • Threads use an inherently simpler shared memory mechanism than processes
    • Interthread communication is far easier
    • Much easier to code for parallelism errors, such as race conditions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is

POSIX?

A

Portable Operating System Interface (added X for style)

  • A family of standards specified by the IEEE Computer Society
  • Helps maintain compatibility between operating systems
  • Defines :
    • API
    • Command Line Shells
    • Utility Interfaces
  • For software compatible with varients of Unix and other Operating Systems
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Problems

with

Threads

A
  • Unpredictable functioning( non-determinism)
  • Difficult to Debug
  • Impossible to prove correct
  • Almost impossible to test thoroughly
  • Specific behavior not easily reproducible
  • Simple conceptually, but added complexities for dealing with many pitfalls
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

pthread Library:

Function for

Creating a Thread

A

pthread_create()

Full Declaration:

int pthread_create( p_thread_t *thread,

const pthread_attr_t attr,

void *(*func)(void *),

void *arg);

  • Creates a new thread with the attributes specified in attr
    • attr can be NULL
  • Starts executing func() and passes it arg
  • Consider carefully what to pass as argument
  • Return 0 if ok, nonzero if error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

pthread Libary:

Function to

Rejoin Threads

A

pthread_join()

Full Declaration:

int pthread_join( pthread_t thread,

void **value_ptr);

  • Makes the calling thread wait for the specified thread to terminate
  • value_ptr is assigned its return value, or assigned PTHREAD_CANCELLED
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Important Parts

of

Thread Synchronization

A
  • pthread_join()
    • Similar to wait() for processes
  • mutex variables
    • Similar to binary semaphores for processes
  • Condition Variables
    • wait for an “event”
    • A variable is assigned a certain value, which is “signaled” by another thread
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

pthread Library:

Important Structures

A

pthread_t

pthread_mutex_t

pthread_attr_t

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

pthread Library:

Important Functions

A
  • Thread related
    • pthread_create()
    • pthread_join()
  • Mutex related
    • pthread_mutex_init()
    • pthread_mutex_lock( pthread_mutex_t *mutex)
    • pthread_mutex_trylock( pthread_mutex_t *mutex)
    • pthread_mutex_unlock( pthread_mutex_t *mutex)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Threads and Process Management:

What does an “exec()” call from a thread

do to the threads in the

containing process?

A

All the threads will terminate.

A new thread is created for the

program to be executed.

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

Posix Threads:

Topics (6)

A
  • Background
  • Threads vs Processes
  • Thread Synchronization
  • Mutex Variables
  • Condition Variables
  • Threads and Unix
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What if a thread

blocks

inside a library function?

A

The pthreads library includes many functions which

only block the thread, not the entire process.

The programmer can also turn off blocking in other function.

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

Mutex Variables:

Overview

A

Mutual Exclusion Lock

  • Allows threads to control access to shared data
  • Only one thread can hold a mutex at a time
  • The threads must agree to use the mutex to protect shared data
  • Mutex variables are not managed by the OS
  • This is very efficient because there are no system calls
17
Q

Threads and Process Management:

Guidlines

A
  • Fork from a process with only one thread
  • Fork before creating additional threads
  • Fork only from the main(parent) thread
  • Hold no locks during the fork()
18
Q

Threads and Library Functions:

How do threads

share the same library function

at the same time?

A

Any function that uses global variables, or statically declared variables, may have trouble with this.

Answer:

Thread Safe Libraries

  • Libaries can be made thread-safe by adding mutexes around the library function’s global variables
  • Not all library functions are thread-safe
  • Many libraries have thread-safe alternatives
    • example: ctime vs ctime_r )
19
Q

Threads and Library Functions:

What if a thread

is terminated

while inside a library function?

A

For example, in the middle of changing data

The pthreads library includes

cancellation-safe functions

These clean up upon cancellation

Should be used when possible

20
Q

Basic Code:

Creating a thread

A

pthread_t thread1; //declare the thread

//Create the thread, passing a pointer to a function,

// and a pointer to a shared global variable

pthread_create( &thread1, NULL,

(void*) my_function,

(void*) &global_var1);

//After doing whatever, block until thread is finished

pthread_join( thread1, NULL);

21
Q

Threads and Process Management:

What does a fork() call

from a thread do to

other threads in the containing process?

A

Nothing.

The new child process contains only a single copy of the thread that called fork().

But what happens to mutexes? (follow up on this)

22
Q

Conditional Variables

A
  • Synchronize threads by using events
    • e.g. a variable is assigned a certain value
  • A thread waits for an event
    • The event is signaled by another thread
    • These are NOT Unix Signals
  • The Signal causes the waiting thread(s) to wake up
23
Q

pthread Libary:

Mutex Functions:

Unlock

A

int pthread_mutex_unlock(

pthread_mutex_t *mutex);

Unlocks a mutex.

If any threads are waiting to lock this mutex,

one is woken up.

24
Q

Threads

and

Signals

A
  • Each thread can have its own signal mask and signal actions
  • Signals can be sent to:
    • a specific thread
    • the process that “holds” the thread(s)
  • Synchronous signals get delivered to the thread that caused them
    • SEGV
    • SIGFPE
25
Q

Threads and UNIX:

-Background

A
  • UNIX was originally designed to handle processes before shared memory multiprocessors were available
  • A process can act as a “container” for one or more threads.
  • All the threads share the same process’ memory address space
26
Q

pthread Library:

Mutex Functions:

Try Lock

A

int pthread_mutex_trylock(

pthread_mutex_t *mutex );

Lock an unlocked mutex,

but if locked, DO NOT BLOCK, and return EBUSY.

27
Q

pthread Library:

Mutex Functions:

Lock

A

int pthread_mutex_lock(

pthread_mutex_t *mutex);

Locks an unlocked mutex.

If mutex is already locked,

the thread waits until the mutex becomes unlocked.

28
Q

Diagram:

Splitting Tasks with

Processes

A
  • Global Variables, etc are shared by default
  • Each thread tries to “lock” r3_mutex before accessing r3.
  • If already locked, the thread will wait(blocks) until unlocked
  • Both threads have access to all global variables