Thursday, January 8, 2009

Linux - Processes

Creating Processes:



fork() - Duplicate the process. Returns pid of the child process to the parent. Returns zero to the child process.



exec() - replace the program running in a process with another program. Used to run programs from with in another program.



execvp and execlp - accepts program name and search for a program by that name in the current execution path. For exec() we need to provide the complete path



nice - to assign priority to a process. By default every process has niceness of Zero. Higher nice value means, lower priority and vice versa.

=>nice -n 10 sort 1.txt > 2.txt//will assign a nice value of 10 to the command "sort 1.txt >2.txt"





Signals:



Signal is a special message sent to a process. Signals are asynchronous; when a process receives a signal, it processes the signal immediately



Signal type is specified by a signal number, in programs we usually use signal name.

These are defiened in /usr/include/bits/signum.h. In our programs we include /signal.h/



Program contains signal-handler functions to process the signals.



ex:

SIGBUS - bus error

SIGSEGV - segmentatin fault

SIGFPE - floating point exception



A process may also send a signal to another process. One common use of this is

SIGTERM or SIGKILL



SIGTERM - ask the process to terminate. The process may ignore the request by masking or ignoring the signal.

SIGKILL - always kills the process immediately because the process may not mask or ignore SIGKILL.


Signal handling function:

sigaction function can be used to set a signal disposition. The first parameter is the signal number. The next two parameters are pointers to sigaction structures. The fist of these contains the desired disposition for that signal number, while the second receives the previous disposition. The most important field in the first or second sigaction structure is sa_handler . It takes one of the three values.

- SIG_DFL - default disposition for the signal

- SIG_IGN - signal should be ignored

- A pointer to signal-handler function. The function should take one parameter, ie., signal number and return void.

Signal handler should perform minimum work necessary to respond to the signal and then return control to the main program. A signal handler may be interrupted by another signal. So we need to be careful while placing code in the signal handler. Even assigning a value to a global variable can be dangerous as the assignment may actually be carried out in 2 or more machine instructions and a second signal may occur between them, leaving the variable in a corrupted state.

If we use global variable to flag a signal in a signal-handler function , it should be of special type sig_atomic_t . Linux guarantees the assignment of these variables is carried out in a single instruction.

Ex:

----

kill(child_pid, SIGTERM) - to kill the child from the parent. Include the sys/type.h and signal.h if we want to use kill function.

Wait - Wait system call allow us to wait for a process to finish executing, and enable the parent process to retrieve information about its child's termination.

wait(&child_status) - Waits for the child to exit and gets the return value in child_status

waitpid(child_pid) - Waits for child with pid = child_pid to exit.

wait3 - returns the cpu usage statistics about the exiting child process

wait4 - allows us to specify additional options about which process to wait for.

Zombie processes - a process that has terminated but has not been cleaned up yet.

This happens when a child process teminates, when the parent is not calling wait(). It is the responsibility of the parent process to clean up its zombie children.

When the parent exits without cleaning the zombie child, this child is inherited by the init process (pid =1 ) and init cleans up all the zombie process it inherits

When a child terminates, Linux sends a SIGCHLD signal to the parent process. The signal handler can be modified to clean up the child processes.

No comments: