Intro Fork
Intro Fork
1
Users, Programs,
Processes
Users have accounts on the
system
Users launch programs
Many users may launch same
program
One user may launch many instances
of the same program
Processes:
an executing program (the unit of 2
Process States
3
Linux: 5 State Process
Model
Add states for creating and deleting
process
Add transitions Timeout, Dispatch,
Event Occurs
4
How to List all Processes?
On Windows: run Windows task
manager
Hit Control+ALT+delete
Click on the “processes” tab
On UNIX
$ ps –e
Try “man ps” to understand more
about its usage.
5
Process Identification
UNIX identifies processes via a unique Process ID
Each process also knows its parent process ID since each
process is created from a parent process.
Root process is the ‘init’ process
‘getpid’ and ‘getppid’ – functions to return process ID (PID)
and parent process ID (PPID)
Example 1
#include <stdio.h>
#include <unistd.h>
int main (void) {
printf(“I am process %ld\n”, (long)getpid());
printf(“My parent id is %ld\n”, (long)getppid());
return 0;
} 6
Creating a Process – fork()
pid_t fork() creates a duplicate of the calling
process:
Both processes continue with return from fork()
Child gets exact copy of code, stack, file
descriptors, heap, globals, and program counter
fork() returns
-1 if fork fails
0 in child process
child’s PID in parent process 7
Creating a child Process –
fork()
fork() creates a new process by duplicating the
calling process. The new process, referred to as the
child, is an exact duplicate of the calling process,
referred to as the parent, except for the following
points:
9
Creating a Process in Unix
Example 2
#include <stdio.h>
#include <unistd.h>
int main(void) {
pid_t x;
x = fork();
if (x == 0)
printf(“In child: fork() returned %ld\n”, (long) x);
else
printf(“In parent: fork() returned %ld\n", (long) x);
}
11
Chain and Fan
Fan
… …
Parent Child Child Child Child
12
Process Operations:
Creation
A new process needs resources such as CPU,
memory, file descriptors
Child can get resources from OS or from parent
Child address space is a duplicate of parent
process
Child process is given a subset of parent
resources
Prevents too many processes from overloading system
Execution possibilities are
Parent continues concurrently with child
Parent waits until child has terminated
13
Process Termination
Normal exit (voluntary)
Returning zero from main()
exit(0)
Error exit (voluntary)
exit(1)
Fatal error (involuntary)
Divide by 0, seg fault, exceeded
resources
Killed by another process 14
Process Operations:
Termination
When a child process terminates:
Open files are flushed and closed
tmp files are deleted
Child’s resources are de-allocated
File descriptors, memory, semaphores, file
locks, …
Parent process is notified via signal
SIGCHLD
Exit status is available to parent via wait()
15
Process Hierarchies
Parent creates a child process, a child
process can create its own child
processes
Forms a hierarchy
UNIX calls this a "process group"
Windows has no concept of process
hierarchy
all processes are created equal
16
wait(), waitpid() System
Calls
pid_t wait(int *status); errno Cause
ECHILD Caller has no
unwaited-for
wait() causes parent children
process to wait (block)
until some child EINTR Function was
interrupted by
finishes signal
wait() returns child’s EINVAL Options
pid and exit status to parameter of
parent waitpid was
invalid
waitpid() waits for a
specific child 17
wait(), waitpid() System
Calls
pid_t wait(int *status);
WIFEXITED(status)
returns true if the child terminated normally, that is, by calling
exit, or by returning from main().
WEXITSTATUS(status)
returns the exit status of the child. This consists of the
least significant 8 bits of the status argument that the child
specified in a call to exit or as the argument for a return
statement in main(). This macro should only be employed if
WIFEXITED returned true. 19
wait() & “zombie”
A child that terminates, but has not been waited for becomes a
"zombie". The kernel maintains a minimal set of information
about the zombie process (PID, termination status, resource
usage information) in order to allow the parent to later perform
a wait to obtain information about the child.
20
Waiting for a child to finish
(Try “man –s 2 wait”)
#include <errno.h>
#include <sys/wait.h>
pid_t childpid;
childpid = wait(NULL);
if (childpid != -1)
printf(“waited for child with pid %ld\n”, childpid);
21