Spread the love

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,

#include<stdio.h>
#include<unistd.h>
Int main()
{
    printf(“Fork System call Demo\n”);
      fork();
   printf(“Hello fork\n”);
  return 0;
}        

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:-

main()
{
   Pid_t pid;
   Printf(“Fork system call demo\n”); 
   Pid=fork();
if(pid==0){
printf(“Child Process with new PID %d \n”,getpid());
fork();
printf(“fork called from child process\n”);
}
else{
printf(“Parent Process with PID %d \n”,getppid());
fork();
printf(“fork called from duplicate parent process\n”);
}                                                                              

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 :

#include <unistd.h>

int execl(const char *path, const char *arg, ...

                       /* (char  *) NULL */);

 

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 :-

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
	char v;
	printf("\nHello now replacing the current process image\n");
	v=execl("./myprog","Hello How Are U","I am Compiler","I am Fine",NULL);
	if(v==-1)
	{
		perror("\nExecl Failed....\n");
		exit(1);
	}
	else
	{
		printf("\nYou can't get me because u have overwritten me\n");
	}
}                   

 

myprog.c  :-

#include<stdio.h>
int main(int argc,char *argv[])
{
	int i;
	printf("\nIts my new command.....\n");
	printf("\nCommand Line Arguments Are:\n");
	for(i=0;i<argc;i++)
	{
		printf("\nargv[%d]:%s\n",i,argv[i]);
	}
} 

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.