You are on page 1of 23

Process & Process Management

Prachi Pandey
prachip@cdac.in
Hybrid Computing Group (SSDH)
CDAC, Bangalore

Outline
Process concept
Process address space
PCB

Process Creation and Management


Fork, exec and other system calls

Process life cycle

Process
The process is the OSs abstraction for
execution
A Program in Execution
An active instance of a program

Process is often called a job, or task

Program and Process


Program
Hello.c Compiler a.out
Resides in secondary memory

Process
is created by OS to execute a program
OS puts it in the main memory
Creates a data structure

Process Address Space

Process Control Block (PCB)


Information associated with each process

Process Id
Process state
Program counter
CPU registers
Priority
Memory-management info
List of open files
Protection

Process Management
Complete activity required for managing a
process throughout its lifecycle
Allocate resources to processes
Enable processes to share and exchange
information
Protect the resources of each process from other
processes
Enable synchronization among processes.
Maintain a data structure for each process
Execution and Control of processes

Process Management System Calls

Fork()
Execv()
Wait()
Getpid()

How To Create New Processes?


Underlying mechanism
A process runs fork to create a
child process
Child process is a duplicate of the
parent process
Both parent and child execute next
line
Parent and children execute
concurrently

parent

fork()

child

Fork
Child and parent are identical
child returns a 0
parent returns nonzero (child PID)

PID and PPID differs


Synopsis
pid_t fork(void);

Fork()
Splits into . Process
The child process
inherits from parent

fork()

Identical copy of
memory
CPU registers
all files that have been
opened by the parent

Stack

PCB of child is copy of


parents context at
time of call

Text

Stack

ret = 0

Data

Data
ret = xxx

Text

Example: Process Creation in Unix


The fork syscall returns twice: it
returns a zero to the child and the
child process ID (pid) to the

int pid;
pid = fork()
if (pid > 0)
{
/* parent */
..
}
else if (pid == 0)
{
/* child */
..
exit(status);
}

parent.

Fork Example 1: What Output?


int main()
{
pid_t pid;
int x = 1;
pid = fork();
if (pid != 0) {
printf(parent: x = %d\n, --x);
exit(0);
} else {
printf(child: x = %d\n, ++x);
exit(0);
}
}

Fork Example 2
Both parent and child can continue forking
void fork2()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("Bye\n");
}

L0

L1

Bye
Bye

L1

Bye
Bye

exit()
Closes all open files, connections and
deallocates memory
Checks if parent is alive
If parent is alive, holds the result value until the
parent requests it (with wait); in this case, the
child process does not really die, but it enters a
zombie/defunct state

Waiting for the Child to Finish


Waiting for some child to terminate: wait()
Blocks until some child terminates
Returns the process ID of the child process
Or returns -1 if no children exist (i.e., already
exited)

Waiting for a specific child to terminate:


waitpid()
#include
Blocks<sys/types.h>
till a child with
#include <sys/wait.h>

particular process ID

terminates

pid_t wait(int *status);


pid_t waitpid(pid_t pid, int *status, int options);

Unixs execv
The system call execv executes a file, transforming the
calling process into a new process.
After a successful execv, there is no return to the calling
process.
execv(const char * path, char * const argv[])
- path is the full path for the file to be executed
- argv is the array of arguments for the program to
execute
- each argument is a null-terminated string
- the first argument is the name of the program (argv[0])
- the last entry in argv is NULL

Example: A Simple Shell


Shell is the parent
process

bash

E.g., bash

Creates a new process

fork

fork

Child executes a new


process (ls)
execv

Parent waits for Child


process
Wait

execvp
ls

wait

execv Example
#include <stdio.h>
#include <unistd.h>
char * argv[] = {/bin/ls, -l, 0};
int main()
{
int pid, status;

Note the NULL string


at the end

if ( (pid = fork() ) < 0 )


{
printf(Fork error \n);
exit(1);
}
if(pid == 0) { /* Child executes here */
execv(argv[0], argv);
printf(Exec error \n);
exit(1);
} else
/* Parent executes here */
wait(&status);
printf("Hello there! \n);
return 0;
}

Variations of execv()
execv
Program arguments passed as an array of strings

execvp
Searches for the program name in the PATH
environment

execl
Program arguments passed directly as a list

execlp
Searches for the program name in the PATH
environment

Other useful system calls: getpid, getppid


getpid returns the identifier of the calling
process. Example call (pid is an integer):
pid = getpid();
getppid returns the identifier of the parent.
ppid = getppid();

Process State
As a process executes, it changes state
new: The process is being created
ready: The process is waiting to be assigned to a
processor
running: Instructions are being executed
waiting: The process is waiting for some event to
occur
terminated: The process has finished execution

Diagram of Process State

You might also like