11 - Threads Flashcards

1
Q

What is a Thread in computing?

A
  • A Thread is an independent stream of instructions that can be scheduled to run by the OS.
  • Think of it as a “procedure” running independently from the main program.
  • In multi-threaded programs, several procedures can be scheduled to run simultaneously and/or independently by the OS.
  • A Thread exists within a process and uses the process’s resources.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What resources do threads use and how are they characterized?

A
  • Threads only duplicate essential resources needed for independent scheduling.
  • A thread will terminate if the parent process dies.
  • Threads are “lightweight” as most overhead is already achieved through the creation of the process.
  • They have their private processor registers and stack, while sharing memory and resources like files with the process.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the primary motivations for using threads in programming?

A
  • The main motivations are improving program performance and easing development.
  • Threads can be created with much less OS overhead and need fewer system resources to run.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In what scenarios are threads particularly useful?

A
  • Threads are beneficial for handling multiple types of messages in the same communication channel, with each thread managing a specific message type.
  • They allow for the management of multiple channels, with each thread responsible for a different channel.
  • Threads are also useful for synchronized waiting or event notification, where one thread can be blocked waiting for an event, enabling shared memory utilization between parallel executions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the shared and not shared elements between threads inside processes?

A
  • Shared elements include the Process ID (PID)/Parent Process ID (PPID), terminal, open files, timers, and resource limits.
  • Elements not shared are Thread ID, signal mask, thread-specific data, and the alternative signal stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the advantages of multithreaded programs and how are threads utilized?

A
  • Multithreaded programs are best used with programs that can be organized into discrete, independent tasks which can execute concurrently.
  • In these programs, threads can be interchanged, interleaved, or overlapped to optimize program execution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the basic functionality of a simple threads API?

A
  • void thread_create (thread, func, arg)
    • Create a new thread, storing information about it in thread. Concurrently with the calling thread, thread executes the function func with the argument arg.
  • void thread_yield ()
    • The calling thread voluntarily gives up the processor to let some other thread(s) run. The scheduler can resume running the calling thread whenever it chooses to do so.
  • int thread_join (thread)
    • Wait for thread to finish if it has not already done so; then return the value passed to thread_exit by that thread. Note that thread_join may be called only once for each thread.
  • void thread_exit (ret)
    • Finish the current thread. Store the value ret in the current thread’s data structure. If another thread is already waiting in a call to thread_join, resume it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the key aspects of the Thread API?

A
  • Common thread libraries include C-Threads (user level) and Pthreads.
  • POSIX, published in 1995 as IEEE 1003.1c, defines functions for the management of threads, prefixed with pthread_.
  • The definitions are available in the pthread.h file, and code using POSIX threads should be linked with the pthread library (-lpthread)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What functionalities are provided by the Pthread API?

A
  • The Pthread API includes thread creation, termination, joining, detaching, and cancellation.
  • Each thread has a unique identifier of type pthread_t, which it can obtain by calling pthread_self().
  • To compare thread identifiers, the function pthread_equal(pthread_t, pthread_t) is used.
  • For printing thread identifiers, the format %lu (long unsigned) is used
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are threads created and managed in Pthreads?

A
  • The main() method comprises a single default thread.
  • pthread_create() is used to create a new thread and make it executable.
  • The maximum number of threads that can be created by a process is implementation-dependent.
  • Once created, threads are peers and may create other threads.
    pthread_create() takes parameters for thread identifier, attributes, start routine, and arguments
  • A thread is started with
    ~~~
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void (start_routine) (void *), void *arg);
    ~~~
    • 1st parameter - Pointer to thread identifier (out)
    • 2nd parameter - Pointer to thread attributes (IN)
      • Can be NULL .
    • 3rd parameter - Pointer to function containing the thread code
      • Function should be: void * (func*) (void * arg).
    • 4th parameter - Pointer to thread arguments
      • Pointer to array, structure, int, …, (can be NULL)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is data transferred in and out of threads?

A
  • Data can be transferred into threads using global variables, accessible by all threads, or through the 4th parameter of pthread_create(), which points to any data structure defined by the programmer.
  • Out data follows a similar pattern as in data transfer.
  • It’s important not to use the same memory location for multiple threads as coherency is not guaranteed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the methods to terminate a thread in Pthreads?

A
  • A thread can terminate when its function completes and returns.
  • The pthread_exit() method can be called to terminate a thread. Retval points to the value accessbile by another joining thread. The other threads will continue to exist. It does not close files.
  • Another option is to invoke the pthread_cancel() method. Sends a cancellation request to a thread. Depending on the thread configuration, it can terminate or not, and a clean up code can be called.
  • Additionally, calling the exit() method terminates all threads.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose and process of thread joining in pthreads?

A
  • Thread joining is used for a thread to wait for another thread to finish executing.
  • pthread_join(pthread_t thread, void **retval) is used to release resources and fetch returned data.
  • The function waits for the specified thread to terminate, and if the thread has already terminated, it returns immediately.
  • Only one thread can wait or join another thread
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is thread detaching?

A
  • A detached thread is immediatly realeased when terminated. Not returned value can be retrieved with a join.
  • int pthread_detach(pthread_t); - resources are ijmmediatly released when terminating. pthread_join() returns error.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the key differences between processes and threads?

A
  • Processes have a hierarchical structure (parent-child), independent data spaces, no shared variables, and their creation is expensive.
  • Threads, on the other hand, have a flat structure (all are siblings), share data spaces and heap, and their creation is significantly cheaper and faster than processes​
  • Sharing data between thread is easy, contrary to processes that require more work.
  • Thread creation is faster than process creation
  • Context-switch time may be lower for threads than for processes
  • In threads, called functions should be thread-safe
  • No isolation between threads - a bug in one thread can damage all of the threads in the process
  • Threads share open file descriptors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly