0% found this document useful (0 votes)
30 views7 pages

Process Intro

Uploaded by

bsbuhazra2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Process Intro

Uploaded by

bsbuhazra2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PROCESS

A process is basically a program in execution.

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.

ps ( Process status ) command in UNIX

The ps command reports information on current running processes,


outputting to standard output. It is frequently used to find process
identifier numbers.
ps command can be used to see/list all the running processes.
$ ps

PID TTY TIME CMD


19 pts/1 00:00:00 sh
24 pts/1 00:00:00 ps

The result contains four columns of information.

PID - refers to process ID.


TTY - the name of the console that the user is logged into.
TIME- the amount of CPU in minutes and seconds that the
process has been running.
CMD - the name of the command that launched the process.
For a single process information, ps along with process id is used
$ ps 19

PID TTY TIME CMD


19 pts/1 00:00:00 sh

For more information -f (full) can be used along with ps


$ ps –f

UID PID PPID C STIME TTY TIME CMD


52471 19 1 0 07:20 pts/1 00:00:00f sh
52471 25 19 0 08:04 pts/1 00:00:00 ps -f

Fields described by ps are described as:


UID: User ID that this process belongs to (the person running it)
PID: Process ID
PPID: Parent process ID (the ID of the process that started it)
C: CPU utilization of process
STIME: Process start time
TTY: Terminal type associated with the process
TIME: CPU time taken by the process
CMD: The command that started this process

There are other options which can be used along with ps


command :
-a: Shows information about all users
-x: Shows information about processes without terminals
-u: Shows additional information like -f option
-e: Displays extended information
Functions to get process ids in C

There are two functions which are used to get the process ids, the
functions are:

1. getpid()
2. getppid()

1) getpid() :-

When any process is created, it has a unique id which is called its


process id. This function returns the process id of the calling function.

Syntax:
pid_t getpid();

2) getppid() :-

This function returns the process id of the parent process.

Syntax:

pid_t getppid();

pid_t :- The type of process id, which is an unsigned integer type of


data type.
C program to demonstrate example of getpid() and getppid()
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

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;

//getpid() - will return process id of calling function


process_id = getpid();
//getppid() - will return process id of parent function
p_process_id = getppid();

//printing the process ids


printf("The process id : %d\n",process_id);
printf("The parent process id : %d\n",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.

A process which is killed but still shows its entry in the


process status or the process table is called a zombie process,
they are dead and are not used.
3. Daemon process : They are system-related background
processes that often run with the permissions of root and services
requests from other processes, they most of the time run in the
background and wait for processes it can work along with for ex
print daemon.

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.

• If fork() returns a negative value, the creation of a child process was


unsuccessful.
• fork() returns a zero to the newly created child process.
• fork() returns a positive value, the process ID of the child process,
to the parent.

The returned process ID is of type pid_t defined in sys/types.h.

Normally, the process ID is an integer. Moreover, a process can use


function getpid() to retrieve the process ID assigned to this process.
Example :

#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.

You might also like