Techno World Inc - The Best Technical Encyclopedia Online!

THE TECHNO CLUB [ TECHNOWORLDINC.COM ] => Linux => Topic started by: Taruna on January 03, 2007, 01:47:46 PM



Title: Process In UNIX
Post by: Taruna on January 03, 2007, 01:47:46 PM
In this article I want to explore the basics of a UNIX process and it s relationship with other processes etc. A process is a program in execution. It is created by the Unix to run a program. Each process in UNIX is allocated a time slice. When a process consumes the time slice in execution, it is preempted by the OS so that it can schedule another process.

When the system starts up first process to be run is scheduler. The actual process name can be different depending upon the flavor of the OS. The scheduler brings init process to execution and init takes care of system start up. In UNIX each process is identified with a ID (integer) which is assigned during process creation. For example the scheduler process which I said in the beginning of this paragraph has process ID 0 and init will have process ID 1. The init reads several script files and execute them before the system is started.. These script files are located in /etc under various directories. /etc/inittab file contains the information which is required to bring the system up. It contains the default run level and location of the directories where the script files for each run level is stored. The UNIX OS may switch different run levels during its lifetime. For example if the run level is 3 we can use UNIX machine as multi-user machine. To say as another example when we shut down the system init process changes its run level so that it will execute the script files which are required for house keeping actions, to gracefully kill the running daemons, processes etc. If u have a system with UNIX kernel u can check urself in depth regarding these details.

In UNIX a new process is created by using fork() system call. The process which executes fork is called parent process and the newly created process is child process. If everything goes well the return value of fork is child process s ID in parent process and 0 in child process. As a process can have several children the above strategy of returning value from fork seems logical. From nowonwards the parent and child process can run concurrently and when the child completes its execution it supplies the exit status to the parent so that parent can know about it. To get the exit status of the child the parent has to execute waitpid() or wait(). When a process is completed its execution all the resources are reclaimed from it and its details are removed from the process table. Here comes a interesting scenario of parent and child process. When a child completed its execution and parent not yet executed the wait system call to get the status of the child, the kernel reclaims all resources of the child but it keeps minimal information in the process table which can be used to return the exit status to the parent. This state of the child is called Zombie state. The process is called Zombie process. As soon as the parent executes wait the status returned to the parent and child process details are removed from the process table.. One more scenario is what happens when the parent exited before child completes its execution. The creators of the UNIX are very generous and they don t wanted to leave the child as orphan. init becomes the parent of this orphan process. There is no special reason for choosing init process for this purpose. The only reason is the init process lives as long the UNIX system is up and running.

Here I will explain some practical scenario of process. We execute a command from the shell. What goes in background is interesting. The shell forks a new process and loads the corresponding executable file and runs it. The shell is parent process here and it executes wait system call. wait doesn t return till the child executes exit. For example the command u want to execute is ls. the shell forks a new process and to load the /bin/ls it uses the exec family of system calls. Immediately the parent executes the wait. The child now runs the command as a separate process and as soon as its execution completed it return the exit status to the parent which is parent.