3a Process Management
3a Process Management
MANAGMENT
System calls
Systems
System calls
Outline
•Introduction
•What is a process
•Process and program
•Process states
•System calls
•Fork
•Use of fork examples
•Getpid
•Getppid
Systems
Introduction
UNIX Architecture – A programmer perspective
Applications
User mode
C lib & other libraries
Process
File subsystem
subsystem
Hardware
Systems
Introduction
User mode & kernel mode execution(recap)
•Multiuser & multitasking operating systems such as UNIX have
two modes of execution, namely, user mode and kernel mode
•Such a mechanism is required to ensure reliability and integrity
of the operating system
•An application in UNIX spends its execution either in user mode
or kernel mode.
•Much of the execution of an application takes place in user
mode, which is less privileged mode.
•A number of restrictions are imposed on what an application
can do while it is executing in user mode
•For example, can’t get access to data structures associated with
the kernel or of the process itself
•Kernel mode execution is privileged mode
•Through system calls, a process can get access to and also can
manipulate data structures of its own or of the kernel
•Operations performed in privileged mode is determined by the
logic of the program.
Systems
Introduction
User mode & kernel mode execution(recap)
•When a system call is invoked, the process has to switch from
user mode to kernel mode
•How the switch takes place depends on the processor. For x86
processors, software interrupt number 128 (i.e., 0x80) serves
the purpose
•On return from a system call, the process reverts back to user
mode
•The command time provides the time spent in user & kernel
mode by an application (passed as argument to the command)
Systems
Introduction
System calls
•System Call is an interface or entry point to operating system.
•System call execution takes place in kernel mode.
•System calls are broadly classified into
• File & file system related
• Process & scheduler related
• Signals
• Inter-process communication
• Socket related
• Kernel related
•Each system call has a unique number, which is specified in
the file <asm/unistd.h>, as shown below.
#define __NR_exit 1
#define __NR_fork 2
#define __NR_read 3
#define __NR_write 4
#define __NR_open 5
#define __NR_close 6
Systems
Process Management
What is a process
•A process is an instance of a running program
•One of the most interesting ideas in computer science(A
process is the basic unit of a program in execution).
• When a program
is running, it
takes up
memory.
• Each running
program has its
own memory
layout, separated
from other
programs.
Systems
Process Management
What is a process
Each process(running program) has its own memory Memory
layout, separated from other programs. The layout
Stack
of this memory is made up of the following; Heap
• Stack: contains temporary data (local variables, Data
return addresses..) Code
• Heap: contains memory that is dynamically
allocated at run-time CPU
• Data: It contains the global variables separated Registers
into initialized and uninitialized variables
• Text/Code: Contains the executable code
In order to identify each memory location in a
program’s memory, we assign each byte of memory
an “address”.
The addresses go from 0 to the largest possible
address, depending on the machine
An address space is a range of valid addresses in
memory that are available for a program or process.
Systems
Process Management
What is a process
Memory
Stack
Process provides each program with two Heap
key abstractions: Data
Code
• Logical control flow
• Each program seems to have CPU
exclusive use of the CPU Registers
Systems
Process Management
What is a process
Systems
Process Management
Multiprocessing
Systems
Process Management
Multiprocessing
Systems
Process Management
Multiprocessing
Systems
Process Management
Multiprocessing- Multiple CPUs
Multicore processors
• Multiple CPUs (“cores”) on
single chip
• Share main memory (and
some of the caches)
• Each can execute a separate
process
• Kernel schedules processes to
cores
Systems
• Still constantly swapping
Process Management
Assume only one
Concurrent process
CPU
Each process is a logical control flow
Two processes run concurrently (are concurrent) if their
instruction executions (flows) overlap in time
Otherwise, they are sequential
Example: (running on single core)
Concurrent: A & B, A & C
Sequential: B & C
Systems
Process Management
Assume only one
Concurrent process
CPU
• Control flows for concurrent processes are
physically disjoint in time
• CPU only executes instructions for one process
at a time
Systems
Process Management
Assume only one
Context switching
CPU
Processes are managed by a shared chunk of OS code
called the kernel
The kernel is not a separate process, but rather runs
as part of a user process
In x86-64 Linux:
Same address in each process
refers to same shared
memory location
Systems
Process Management
Assume only one
Context switching
CPU
Processes are managed by a shared chunk of OS
code
called the kernel
The kernel is not a separate process, but rather runs as
part of a user process
Context switch passes control flow from one
process to another and is performed using kernel
code
Systems
Process Management
Program and Process
Systems
Process Management
Program and Process
•In order to execute a program, the O.S must first create a
process and make the process execute the program.
• Program - A set of instructions which is in human readable
format. A passive entity stored on secondary storage.
• Executable program - A compiled form of a program
including machine instructions and static data that a
computer can load and execute. A passive entity stored on
secondary storage.
• Process - A program loaded into memory and executing or
waiting. A process typically executes for only a short time
before it either finishes or needs to perform I/O (waiting).
Systems
Process Management
Program and Process
•A process is an active entity and needs resources such as CPU
time, memory etc to execute.
•The first generation of operating systems only allowed one
process to run at a time.
Systems
Process Management
Process States
•Processes can be created in two ways
• System initialization: one or more processes created
when the OS starts up
• Execution of a process creation system call: something
explicitly asks for a new process
•System calls can come from
• User request to create a new process (system call
executed from user shell)
• Already running processes
• User programs
• System daemons
Systems
Process Management
Process States
•As a process executes, it changes state. The state of a process
is defined in part by the current activity of that process. Each
process may be in one of the following states i.e. as a process
executes, it moves from state to state
• New State: The process being created.
• Running: A process is said to be running if it has the CPU,
that is, process actually using the CPU at that particular
instant.
• Ready: Waiting to be assigned to the CPU
• Ready to execute, but another process is executing
on the CPU
• Waiting: A process is said to be ready if it is waiting to be
assigned to a processor. A process in this state is waiting
for an event, e.g., I/O completion. It cannot make progress
until event is signaled (disk completes)
• Terminated state: The process has finished execution
• Process Management with C
• fork()
• exec() Systems
Process Management
Process States Transitions between states
1 - Process enters ready
queue
2 - Scheduler picks this
process
3 - Scheduler picks a different
process
4 - Process waits for event
(such as I/O)
5 - Event occurs
6 - Process exits
7 - Process ended by another
process
Systems
Process Management
Creating a process
•A new process is created using fork() system call. The fork
system call is an operation where a process creates a copy of
itself.
•Fork is implemented in the Kernel.
•This function creates a new copy called the child process out of
the original parent process, that is called the parent process.
•If the parent process closes or crashes for some reason, it also
kills the child process.
•The O.S creates a unique process id for every process to keep
track of all processes.
•Executing fork() system call return an integer value as follows;
• 0: The child process (the process created).
• > 0(Positive value): The parent process.
• < 0 (Negative value): if an error occurred
•A process can obtain its own ID by calling the getpid() system
call, and any process can obtain its parent process’s ID by
calling the getppid() system call
Systems
Process Management
Creating a process
• fork-exec model (Linux):
• fork() creates a copy of the
current process
• exec*() replaces the current
process’ code and address
space with the code for a
different program
• Other variants of exec*() -
execv, execl, execve, execle,
execvp, execlp
• fork() and execve() are
system calls.
• Other system calls for
process management:
• Fork system call creates new • getpid()
processes • getppid()
• execve system call is used after a • exit()
fork to replace the processes memory • wait(), waitpid()
space with a new program
Systems
Process Management
Creating a process
•In multitasking operating systems, processes (running
programs) need a way to create new processes, to run other
programs.
•In operating systems like Unix that support virtual memory ,
the fork operation creates a separate address space for the child
process.
•Therefore the child process has an exact copy of all the memory
segments of the parent process.
Purpose of fork()
•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.
•Unix will make an exact copy of the parent's address space and
give it to the child. Therefore, the parent and child processes
have separate address spaces.
Systems
Process Management
Creating a process- Example
fork
The output will be:• The output of the above • The number of times
program are created by the the line of code after
parent and child processes; fork statement is
i.e. the output came from executed is 2n ,
the parent process and the where n is the
other one from the child number of fork()
process. system calls.
Systems
Process Management
Creating a process- Example The output will be:
Systems
Process Management
Creating a process- Example- Using system wait until some or
all of its children
Systems
Process Management
Creating a process- Example- Using system wait until some or
all of its children
• The wait call system
wait(NULL) will make the
parent process wait until
the child process has
executed all of its
commands
Systems
Process Management
Creating a process
• Fork system call is used
for creating a new
process, which is called
child process, which runs
concurrently with the
process that makes the
fork() call (parent process)
Systems
Process Management
Obtaining process information
•The getpid() function is used to get the process id of the current
process.
•Every process has a parent process.
•The process ID of the parent process can obtained using
getppid() function.
•The getpid() and getppid() are declared in <unistd.h> library.
Systems
Process Management
Process termination
•Process executes last statement and then
asks the operating system to delete it using
the exit() system call.
• Returns status data from child to parent (via
wait())
• Process’ resources are deallocated by operating
system
Parent may terminate the execution of
•
Systems
Process Management
Process termination
Conditions which terminate processes
• Normal exit (voluntary) - A process terminates when it
finishes executing its final statement and asks the OS to
delete it by using the exit() system call.
• Error exit (voluntary)
• Fatal error (involuntary)
• Killed by another process (involuntary)
•Process executes last statement and then asks the operating
system to delete it using the exit() system call.
•Returns status data from child to parent (via wait())
•Process’ resources are deallocated by operating system
•Parent may terminate the execution of children processes
using the abort() system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting and the operating systems does
not allow a child to continue if its parent terminates
Systems
Process Management
2-State Process Model
•The lifetime of a process is made up of two elements;
• CPU bursts and
• I/O bursts.
•So simplest possible model can be constructed by observing
that at any particular time, a process is either being ;
• Executed by a processor(running) or
• Waiting for an I/O(not running
•There may be a number of processes in the “not running” state
but only one process will be in “running” state.
Systems
Process Management
I/O and CPU Bound Process
Systems
Process Management
5-State Process Model
Systems
Process Management
5-State Process Model
Systems
Process Management
5-State Process Model
•The following services are provided by Process Control System
calls that are used to control a process.
Systems
Process Management
Zombies
•A process which has finished the execution but still has entry in
the process table to report to its parent process.
•Zombie process is also known as "dead" process. Ideally when a
process completes its execution, it's entry from the process table
should be removed but this does not happen in case of zombie a
process.
•When a process terminates, it still consumes system resources
• For example, various tables maintained by OS
• Such a proces is called a “zombie” (a living corpse, half
alive and half dead)
•Reaping is performed by parent on terminated child
•Parent is given exit status information and kernel then deletes
zombie child process
•What if parent doesn’t reap?
•If any parent terminates without reaping a child, then the
orphaned
•child will be reaped by init process (pid == 1)
•Note: on more recent Linux systems, init has been renamed
systemd Systems
Process Management
Process Management Summary
• fork makes two copies of the same process (parent &
child)
• Returns different values to the two processes
• exec* replaces current process from file (new
program)
•Two‐process program:
• First fork()
• if (pid == 0) { /* child code */ } else { /* parent
code */ }
•Two different programs:
• First fork()
• if (pid == 0) { execv(...) } else { /* parent code
*/ }
•wait or waitpid used to synchronize parent/child execution
and to reap child process
Systems
Process Management
Summary
• At any given time, system has multiple active
processes
• On a one‐CPU system, only one can execute at a
time, but each process appears to have total control
of the processor
• OS periodically “context switches” between active
processes
• Implemented using exceptional control flow
• Process management
• fork: one call, two returns
• execve: one call, usually no return
• wait or waitpid: synchronization
• exit: one call, no return
Systems