Introduction
We have studied using multiple processes using fork.
If these processes need to communicate, an external mechanism (such as signals or pipes) must be used.
A thread is a a flow of control in a process.
Multiple threads share the same address space.
If one thread changes a variable, this change can be seen by other threads.
You create a thread by indicating the name of a function for the thread to execute.
Unlike an ordinary call, the created and creating threads execute concurrently.
A thread of execution for an ordinary call to process_fd.
A new thread executes process_fd.
Motivating Problem: Monitoring file descriptors:
#include <pthread.h> int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); void pthread_exit(void *value_ptr); int pthread_join(pthread_t thread, void **value_ptr);pthread_create:
pthread_exit: allows a return value to be passed to another thread that joins to it.
pthread_join: wait for the thread with a given thread ID.
Look at Program 9.4: using select
Look at Program 9.6: a function that monitors a file descriptor
Look at Example 9.3: a code segment that calls process_fd as an ordinary function
Look at Example 9.4: a code segment that creates a thread ro run process_fd
Look at Program 9.7: a function to monitor an array of file descriptor using threads.
POSIX Threads
Description | POSIX function |
Thread Management | pthread_create |
pthread_exit | |
pthread_kill | |
pthread_join | |
pthread_self | |
Mutual Exclusion | pthread_mutex_init |
pthread_mutex_destroy | |
pthread_mutex_lock | |
pthread_mutex_trylock | |
pthread_mutex_unlock | |
Condition Variables | pthread_cond_init |
pthread_cond_destroy | |
pthread_cond_wait | |
pthread_cond_timedwait | |
pthread_cond_signal | |
pthread_cond_broadcast |