2 1 4 LinuxProcesses

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

2.1.4.

Linux Processes

2.1.4.1. Processes in Linux

A Process is a program in execution.

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.

2.1.4.2. Linux Process Creation

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.

‘fork’ returns two PIDs:

 ‘0’ to the child process

 ‘positive number’ to the parent process.

 ‘-1’ for failure

Following is an example of creating a Linux Process using ‘fork()’.


#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

int main(int argc, char* argv[])

int pid, child_status;

pid = fork();

if(pid == 0) {

printf("I am the child process \n");

else if(pid == -1) {

printf("Error in process creation! \n\n");


return 1;

else {

printf("I am the parent process, child pid = %d \n", pid);

/* Wait until the child terminates */

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.

2.1.4.3. Running a Different Program in the Child Process

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>

int main(int argc, char* argv[])

char cmd_str[128];

int pid;

int child_status;

printf("sh> ");

scanf("%s", cmd_str);

pid = fork();

if(pid == 0) {

/*child process*/

printf("I am the child process \n");

printf("cmd = '%s'\n", cmd_str);

printf("-------------------------\n");

execl(cmd_str, cmd_str, (char*)NULL);

/* We should not return from 'execl'.

We will go to next line, if error occurs */

perror("execl() failure!\n\n");

printf("This print should not come if execl were successful! \n\n");

exit(1);

}
else if(pid == -1) {

/* Process creation failed */

printf("Error in process creation! \n\n");

return 1;

else {

/* It is the parent process */

printf("I am the parrent process, child pid = %d \n", pid);

/* Wait until the child terminates */

wait(&child_status);

printf("-------------------------\n");

printf("Done, status = %d\n", child_status);

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.

You might also like