Process in Linux

process

Process 

  • A process is something in which a program executes.
  • An address space with one or more threads executing within that address space.
  • Each process has a unique number called PID (Process Identifier).
  • It is a +ve integer between 2 and 32,768. The PID number 1 is for init process which is reserved and is responsible to manage other processes.

Note: When a process is started, the next unused process number in sequence is chosen and that numbers  is restarted at 2 so that they wrap around.

PROCESS TABLE : It describes all of the process that are currently running and loaded with their PID.

We can see all processes by “ps” or “ps –e” command in LINUX.

getpid() :- is a system call that returns the process ID of the invoking processes parent.

In general, each process is started by another process known as its parent.

When LINUX started, it runs a single process, i.e. init process and called process manager.

STARTING A NEW PROCESS

1.system() :-

system

We can start a new process by the system library function.

Syntax –   int system(const char *);

It will return either 0 or -1. 0 in case of success and in case of error -1. And accept a const char * type argument.

For example, $ system(“clear”);

It will clear the screen of terminal.

In this function call, current process will be in sleep until system call completes. It basically call the existing process.

2.fork() :-

fork

It creates a duplicate process in terms of memory area.(Data Segment Area). i. means create two copies  process. One is with the same PID and a new child process with another PID.

Syntax: pid_t fork(void);

Fork system call working : –

When a form system call is executed it will create a duplicate copy of process with new PID .i.e. child process. So two processes are created parent and child process.

fork1

For example,

Output:-

Fork system call Demo

Hello fork

Hello fork

 And if we write fork call thrice suppose,

Then “Hello fork” will display 8 times.Fork system call display or copies the item in power of 2.

fork2

i.e. 2^(times fork called).

If will call fork system call again in parent and child called then again parent and child process will be created.

Example:-

When a fork call is executed it will return 0 on success(i.e. created a child duplicate process) or -1 on error.

When a fork system call is called there is not the surety which process i.e. either parent or child is first executed. It is dependent on the kernel. After call to fork, the two processes and created and which process is executed first is dependent on kernel.

vfork() :-

The only difference between fork and vfork system call is that in vfork call, first child process will get executed and after that parent process will get executed. This can be monitored by printing PID of all processes on terminal by using getpid() call.

Rest all work is same in case of vfork call.

Syntax – int vfork(void);

Zombie Process :-

zombie

Zombie process is a process state when the child dies before the parent process.
In this case the structural information of the process is still in the process table.

ORPHAN PROCESS :-

An Orphan Process is nearly the same thing which we see in real world. Orphan means someone whose parents are dead. The same way this is a process, whose parents are dead, that means parents are either terminated, killed or exited but the child process is still alive.

3. execl call :-

An exec function replaces the current process with a new process specified by the path or file argument. Use exec functions to “hand off” execution of your program to another.

Syntax :

 

It will return 0 if success and -1 is error.

Let it illustrated by an example.

We have two programs – exec.c and myprog.c

exec.c :-

 

myprog.c  :-

Compile these program – gcc exec.c –o exec and gcc myprog.c –o myprog

Run – ./exec

Output – 

Hello now replacing the current process image.

After it through execl call the process image is changed to ./myprog executable file.

And in myprog.c there are command line arguments.which we passed i.e. Hello How are you , I m Compiler , I am Fine.

So after this execl call a new process is open i.e. ./myprog and this program will execute.

And after this program execution control will not come back to this.

So we will not see this printf statement. – “You can’t get me because u have overwritten me”

So execl will replace the whole current process with another process and the first processs will be terminated.