0% found this document useful (0 votes)
2 views50 pages

3a Process Management

The document provides an overview of process management in UNIX, detailing the concepts of processes, system calls, and their states. It explains the distinction between user mode and kernel mode, the creation of processes using the fork() system call, and the execution of processes in a multitasking environment. Additionally, it covers process states, context switching, and the relationship between programs and processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views50 pages

3a Process Management

The document provides an overview of process management in UNIX, detailing the concepts of processes, system calls, and their states. It explains the distinction between user mode and kernel mode, the creation of processes using the fork() system call, and the execution of processes in a multitasking environment. Additionally, it covers process states, context switching, and the relationship between programs and processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 50

PROCESS

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

System call interface

Process
File subsystem
subsystem

Kernel mode IPC Memory Network


exection
Device Drivers

Hardware Abstraction Layer

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

• Provided by kernel mechanism called


context switching
• Private address space
• Each program seems to have
exclusive use of main memory
• Provided by kernel mechanism called
virtual memory
Systems
Process Management
What is a process

Systems
Process Management
What is a process

Computer runs many processes simultaneously


• Applications for one or more users – Usually user level
e.g. Web browsers, email clients, editors, …
• Background tasks – Kernel/O.S level
e.g. Monitoring network & I/O devices

Systems
Process Management
Multiprocessing

 When program starts, it runs in a single process. It has


a process id (an integer) that the OS assigns. A
program can get its own process id with the getpid
system call:

Systems
Process Management
Multiprocessing

• All programs are associated with one or more


processes, and a process is scheduled to run its
code by the OS.
• The OS has to schedule all currently-running
processes (i.e., processes that are waiting to run
their next line of code), and it does so on a time-
shared basis.
• In other words: there may be tens, hundreds, or
thousands of processes "running" at once, but on a
single-processor system, only one can run at a
time, and this is coordinated by the OS.
• On multi-processor machines (like most modern
computers), multiple programs can literally run at
the same time, one-per processor Systems
Process Management
Multiprocessing

 Single processor is able to execute multiple processes


concurrently
• Process executions interleaved, CPU runs one at a time
• Address spaces managed by virtual memory system
• Execution context (register values, stack, etc) for other
processes are saved in memory
Systems
Process Management
Multiprocessing
• A context switching is a
process that involves
switching of the CPU from
one process or task to
another.

• A context switch is the


process of storing the state
of a process or thread, so
that it can be restored and
resume execution at a later
point.
• Occurs when the kernel
Context switch transfers control of the CPU
1) Save current registers in memory
from an executing process to
another that is ready to run
2) Schedule next process for execution

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

• However, the user can think of concurrent


Process A Process B Process C Process A Process B Process C
processes as executing at the same time, in parallel
User View

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.

•A multi-tasking operating allow multiple tasks to run at a time.


• Single processor computers only have 1 CPU, the
multiple task must share this CPU.
• Most often, the CPU will time-slice the task, so they
get a fair amount of CPU time.
• The time slice is a unit of time given to each process
by the scheduler.
• The time slice is the time period during which a
process can use the CPU before it is preempted by the
scheduler

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

Functions for controlling


processes
•exit() - Terminate the process
•fork() - Create a child process
•wait() - Wait for child process
termination
•execvp() - Execute a program in
current process Systems
Process Management
Exceptions and interrupts-
•Interrupts and exceptions are used to notify the CPU of events
that needs immediate attention during program execution.
•Both are events that alters the normal sequence of instructions
executed by a processor. Such events correspond to electrical
signals generated by hardware circuits both inside and outside
the CPU chip.
•Interrupts - Interrupts are used to notify the CPU of external
events.
•Interrupts are generated by hardware devices outside the CPU
at arbitrary times with respect to the CPU clock signals and are
therefore considered to be asynchronous.
Exceptions - Another name for exception is trap. A trap (or
exception) is a software generated interrupt.
•Exceptions are used to handle address internal errors in a
program e.g. overflow, division by zero and bad data.
•Produced by CPU control unit while executing instructions and
are considered to be synchronous because the control unit
issues them only after terminating the execution of an
instruction. Systems
Process Management
Creating a 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

Hello world Hello World

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:

• When a process creates a new process, then there are two


possibilities for the execution exit:
•The parent continues to execute concurrently with its
child.
•The parent waits until some or all of its children have
terminated.

• In the above examples, the parent and child processes are


executed concurrently.

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)

•From one process we can create another process. To do this


fork() library function will be used. fork() split one process into
two processes:- parent process, and child 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.

C program to get the Process ID and parent Process ID


•Below is a sample program for obtaining process id and parent
process id using getpid() and getppid() functions.

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

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 itsSystems
parent
Process Management
Process termination
Conditions which terminate processes

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

•I/O bound processes


• Processes that are most of the time waiting for an
event: mouse event, keyboard, Ethernet packet
arrives etc.
• This type of process is in ready/running state only
for a short period of time: E.g. update mouse
cursor after mouse movement, show a character
after typing a key.
• Most processes are in waiting state.
•CPU bound process:
• These processes need the CPU for long periods of
time: Scientific/Numerical Applications,
Compiler/Optimizer, Renderer, Image processing
• These processes are mostly in ready or running
state
• Most applications are I/O bound Systems
Process Management
5-State Process Model
•Broadly speaking the life of a process consist of CPU burst and
I/O burst but in reality
• A process may be waiting for an event to occur; e.g. a
process has created a child process and is waiting for
it to return the result
• A process may be waiting for a resource which is not
available at this time
• Process has gone to sleep for some time
•So generally speaking a Process may be in one of the following
five states:
• New: The process is being created (Disk to Memory)
• Ready: The process is in main memory waiting to be
assigned to a processor
• Running: Instructions are being executed
• Waiting: The process is waiting for some event to
occur (I/O completion or reception of a signal)
• Terminated: The process has finished execution

Systems
Process Management
5-State Process Model

•As a process executes, it changes state:


• New: The process is being created
• Ready: The process is waiting to run
• Running: Instructions are being executed
• Waiting: Process waiting for some event to occur
• Terminated: The process has finished execution

Systems
Process Management
5-State Process Model

•As a process executes, it changes state:


• New: The process is being created
• Ready: The process is waiting to run
• Running: Instructions are being executed
• Waiting: Process waiting for some event to occur
• Terminated: The process has finished execution

Systems
Process Management
5-State Process Model
•The following services are provided by Process Control System
calls that are used to control a process.

• To forcefully abort the process, simply end it normally.


• Execute a process after loading it into the main memory.
• Terminate the current process before starting a new one.
• Wait for a process to complete running. Wait until a specific
event happens, then announce it once it has.
• Allocate memory to a process, then release the memory if the
process is terminated.

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

You might also like