Professional Documents
Culture Documents
Process Management Compatibility Mode
Process Management Compatibility Mode
Prachi Pandey
prachip@cdac.in
Hybrid Computing Group (SSDH)
CDAC, Bangalore
Outline
Process concept
Process address space
PCB
Process
The process is the OSs abstraction for
execution
A Program in Execution
An active instance of a program
Process
is created by OS to execute a program
OS puts it in the main memory
Creates a data structure
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
Fork()
Execv()
Wait()
Getpid()
parent
fork()
child
Fork
Child and parent are identical
child returns a 0
parent returns nonzero (child PID)
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
Text
Stack
ret = 0
Data
Data
ret = xxx
Text
int pid;
pid = fork()
if (pid > 0)
{
/* parent */
..
}
else if (pid == 0)
{
/* child */
..
exit(status);
}
parent.
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
particular process ID
terminates
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
bash
E.g., bash
fork
fork
execvp
ls
wait
execv Example
#include <stdio.h>
#include <unistd.h>
char * argv[] = {/bin/ls, -l, 0};
int main()
{
int pid, status;
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
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