Friday, January 31, 2014

pthreads - Outline

When multiple processes, the child process should notify the end to the parent process.
The parent process receives SIGCHLD as follows. Parent process can ignore it as follows:
 
    sa.sa_handler = SIG_IGN;
    sigemptyset( &sa.sa_mask );
    sigaction( SIGCHLD, &sa, 0 );
When process is forked all the copy of the program including the stack and file descriptors are made.
So, the child process can use the (stack information)local variables and file descriptors as it is. But,
in case of threads, though the memory region is shared, the stack is separated the child process cannot
refer the stack information of parent process. That is why the local information is passed by structures.
 
The pthread_create takes the 4th argument and launches the function in 3rd argument. When the function
calls pthread_detach() it declares that it will not notify the parent of its end. So, parent process will not maintain
any of the child process's end records.
 
Difference between multiprocess and multithreading:
 
1. memory space is independent
 
Since the memory is independent, there is no need concern about mutual access of static and global variables.
But, in other side, if there is communication needed between two processes the proper IPC is needed between these.
 
2. Number of processes created affects
 
Zombie -> Child exited. But, parent did not read the status using wait(). So, the entry remains in the process table. Do not hold any memory resource. But, only the PID entry. So, system will run out of PID if zombie remains. Can be checked using 'ps' command and process status will be 'z'. Usually, when child exits, SIGCHLD signal is sent to the parent. Parent will call 'wait' in the signal handler. So, manually, SIGCHLD can be sent to the parent using kill. Or, parent can be killed, so that init process will become new parent  and it will read the status and clean up.
 
Orphan -> Child still running, but parent died. init process becomes the new parent process.
 
There are maximum number of processes in the system.
 
Threads
 
1. Thread specific information
 
Only the following information is independed for each thread.
a. Stack
b. Thread ID
c. Signal mask
d. Substitue signal mask
e. errno variable
f. scheduling policy and priority.
 
    All others are shared. This advantage of not much IPC mechanisms are needed. But, same time, if synchronization is not performed, it will cause deadlock, etc.
 
2. No zombies
Thread is ultimately the resource maintained by process. So, it will be released when process dies.
 
3. Thread safe
 It is important to make sure that the functions are thread safe.
 
a) Function does not process any global or static variables
b) Does not call other non thread-safe functions
c) if uses such variables, uses mutex and does mutual exclusion
 
functions having _r suffix are thread-safe version of functions.
 
Threads very light weight compared to processes in resource consumption. Multi threads can be easily ported to multi-process. But, the reverse is difficult.
 
----
  • int pthread_create( pthread_t * id, pthread_attr_t * attr, void * (*routine)(void *), void * arg);
  • int pthread_join( pthread_t id, void ** thread_return );
  • int pthread_detach( pthread_t id );
  • void pthread_exit(void *retval );
  • pthread_t pthread_self( );
pthread_create -> ID is assgined. pthread_self() can be used to refer it's own ID.
                'attr' will specify the joinable, scheduling off, stack size (default is 10M) are specified. processed with pthread_attr_ function.
 
Third argument is start function and the fourth is the argument passed into it. Return value of start function will be the second argument of pthread_join.
 
pthread_detach will detach the process from parent. Parent does not need to wait for the child. pthread_join will return error.
 
pthread_exit(): will wait for end. returns the return value.
 
guest $ cat /proc/sys/kernel/threads-max

No comments: