Professional Documents
Culture Documents
2 1 4 LinuxProcesses
2 1 4 LinuxProcesses
2 1 4 LinuxProcesses
Linux Processes
A Linux Process has by default one Thread and a memory address space. All Process begin with one
thread of execution that runs main() function. You can create additional POSIX threads using
pthread_create().
Each process is associated with a Process Id (pid). After booting the first process created is the ‘init’
process, with pid = 1.
A Linux process is created using system call ‘fork()’. ‘fork’ creates a child process exactly identical to
the parent, including stack, heap, file descriptors, variables etc, but the child runs in another address
space. So after ‘fork’ both parent and child processes run.
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
pid = fork();
if(pid == 0) {
else {
wait(&child_status);
return 0;
Compile this code and run it. Assuming that you have saved this code into file ‘process.c’ and you are
using ‘gcc’ for compilation, the commands for compilation is following.
$ cc process.c -o process
Run it by typing
$ ./process
In the above code, the code in blue is for the child process and the code in green for the parent process.
The child and parent processes are recognised based on the PID returned as described earlier. You can
observe that the parent process is waiting for the child to terminate.
Why should one create a child process? It is to perform a separate task in the child process. Hence we
should know how to run a different program inside he created child process.
You can run a separate program inside the child process by using ‘exec’ family of system calls.
There are many in this family, we will use the following call here in our example. You may explore
other calls as well.
int execl(const char *path, const char *arg, …)
1st argument = path of the program
2nd argument = 1st argument of the program,
It is the executable file name itself
next arguments = Pass the argument required by the program,
Pass NULL if the program doesn’t require any arguments
What should the parent process do then ? It will do its assigned work and then it should wait for the
child to terminate. This can be done using one of the following system calls.
wait(NULL) : suspends the current process until one of its child terminates
waitpid(pid, &child_status, 0) : suspends the process until the child process specified by the
pid changes its state. Here change of state means termination.
In our example, we have only one child. So the effect of both of these call will be same.
We will modify our earlier code to make the child process to execute command entered by the user.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
char cmd_str[128];
int pid;
int child_status;
printf("sh> ");
scanf("%s", cmd_str);
pid = fork();
if(pid == 0) {
/*child process*/
printf("-------------------------\n");
perror("execl() failure!\n\n");
exit(1);
}
else if(pid == -1) {
return 1;
else {
wait(&child_status);
printf("-------------------------\n");
return 0;
The line where the child process is executing the command using ‘execl’ has been marked in blue in
this code.
Compile run it. After executing the program, if you enter ‘/bin/ls’, you should see the list of files and
directories in the current directory printed on the command window.