Friday, February 22, 2008

[Programming] Processes (1)

From today, we will digress to processes and threads programming.

One standard definition of process (IEEE Std 1003.1) is, "an address space with one or more threads executing within that address space, and the required system resources for those threads". It's like a circular defintion, because in order to understand this definition, you have to know what are threads first. This may not be a problem for those computer gurus, but for beginners, it's better to find a simple explanation, as follows.

The description of process in wikipedia is easy to understand: an instance of a computer program that is being sequentially executed. Further to say, a computer program is just a passive collection of instructions, while a process is the actual execution of those instructions. This is described from the view of the user. On the other hand, in the perspective of operating system, you can say process is the operating system's basic scheduling unit.

So what elements does a process consist of? It's easy to answer, since process is actaully a program. It contains: program code, data, variables, current context (execution status), working directory, access rights (mode and ownership), memory and other system resources allocated to the process etc. For Linux system, processes may share code and system libraries.

In Linux/Unix, you can
a) start a new process by the system() function;
b) replace the current process with a new process by the set of exec() funtions (execl, execv, execlp etc.);
c) or duplicate a new process by the fork() function.

In practice, system() is rarely used to start new processes, because it invokes the new program using a shell, which may cause security problems. So it's preferable to use exec() and fork(). The difference between exec() and fork() is: fork() creates a new child process which is an exact copy of its parent process, but exec() causes a process to cease being an instance of one program and to instead become an instance of another problem.

The flows of the system(), exec() and fork() call are shown in the following figures.


No comments: