Process Intro
Process Intro
Process ID (PID)
PID refers to process ID, which is commonly used by most operating
system kernels, such as Linux, Unix, MacOS and Windows.
This is a unique ID that is automatically assigned to each process
when it is created. A process is a running instance of a program.
Parent Process ID (PPID)
A parent process is a process that has created one or more child
processes. Each child process is given a Parental Process ID (PPID),
and the parent process kills the child when it completes their
operation.
There are two functions which are used to get the process ids, the
functions are:
1. getpid()
2. getppid()
1) getpid() :-
Syntax:
pid_t getpid();
2) getppid() :-
Syntax:
pid_t getppid();
int main(void)
{
//variable to store calling function's process id
pid_t process_id;
//variable to store parent function's process id
pid_t p_process_id;
return 0;
}
Output
The process id : 31120
The parent process id : 31119
Header files
1. sys/types.h - it is used for pid_t type, that is the data type of the
variables which are using to store the process ids.
2. unistd.h - it is used for getpid() and getppid() functions.
Types of Processes
1. Parent and Child process : The 2nd and 3rd column of the
ps –f command shows process id and parent’s process id
number. For each user process there’s a parent process in the
system, with most of the commands having shell as their parent.
2. Zombie and Orphan process : After completing its
execution a child process is terminated or killed and SIGCHLD
updates the parent process about the termination and thus can
continue the task assigned to it. But at times when the parent
process is killed before the termination of the child process,
the child processes becomes orphan processes, with the parent
of all processes “init” process, becomes their new ppid.
When ps –ef is executed, the process with ? in the tty field are
daemon processes.
Process Creation
1). fork()
System call fork() is used to create processes. It takes no
arguments and returns a process ID.
The purpose of fork() is to create a new process, which becomes
the child process of the caller. After a new child process is created,
both processes will execute the next instruction following the fork()
system call. Therefore, we have to distinguish the parent from the
child. This can be done by testing the returned value of fork():
fork() creates an exact replica of parent ( calling )process. After
fork() returns, the parent process and child process now have different
PIDs.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid;
pid = fork();
if ( pid == 0)
{
// this is the child process;
printf(“ Child Process\n”);
}
else
{
// This is the Parent process.
printf(“ Parent Process\n”);
}
}
2). wait()
This function blocks the calling process until one of its child
processes exits.
Syntax :
int wait ( int * status);
The function returns the process identification number of the
terminated processes. Further information concerning the terminated
process is write n into the location whose address is supplied as the
function parameter. One of the main purposes of wait() is to wait for
completion of child processes.
3). exit()
The system call exit() is used to terminate the current process. The
system call wait() is used
when a process wishes to wait until a child process terminates and
determine the exit code of the child process.