0% found this document useful (0 votes)
26 views41 pages

Process

Uploaded by

Jaianshu Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views41 pages

Process

Uploaded by

Jaianshu Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Unix hierarchy


A link is an association between a filename and an inode.
UNIX has two types of links: hard and symbolic (also called
soft).
Directory entries are called hard links because they directly link
filenames to inodes.
Each inode contains a count of the number of hard links to the
inode.
When a file is created, a new directory entry is created an a new
inode is assigned.
Additional hard links can be created with
ln newname oldname or with the link system call.
A new hard link to an existing file creates a new directory entry
but assigns no other additional disk space.
A new hard link increments the link count in the inode.
A hard link can be removed with the rm command or the unlink
system call.
These decrement the link count.
The inode and associated disk space are freed when the count is
decremented to 0.
Symbolic links or soft links..


Programs, Processes and Threads
• A program is a prepared sequence of instructions to
accomplish a defined task.
• A compiler translates the instructions to produce an
executable module.
• When the program is run, the executable module
becomes a program image in main memory.
• A process is an instance of a program that is executing.
• A process has an address space and starts with a single
flow of control.
• The flow of control executes a sequence of
instructions called a thread of execution.
• A thread of execution is a logically related sequence of
instruction addresses assigned to the program counter
during the execution of a program's code.
Process

Process : Operating System abstraction to represent the


resources needed to run a single program.
– Often called a “Heavy Weight Process”
– A single sequential stream of execution in its own address
space.
Process Identity
• Process ID (PID). getpid(); getppid();
– A unique positive integer to identify the process
– Every process has a parent process
– Process 0 created & used by kernel for swapping
– Process 1 is ‘init’ process & adopts all orphaned process
– used to specify processes to the operating system when an
application makes a system call to signal, modify, or wait for
another process.
• Credentials.
– Each process must have an associated user ID & group IDs
– Positive integer associated with ‘login name’
– /etc/passwd;m /etc/group/; process descends from the shell
inherits this user id
Process
Address
Space in
Memory
Linux/Unix Address Space
• A process has five conceptually different areas of
memory allocated to it
– Code ( text segment)
– Initialized data (data segment)
• Static & global data initialized with nonzero.
• Each running process has own data segment
– Zero-initialized data
• Block Started by Symbol(BSS)
• Statically & global data initialized with zero
• When running, these are placed in the data segment
– Heap dynamic memory comes; obtained by malloc()
– stack segment is where local variables are allocated
Process
 When a program is running, the initialized data, BSS,
and heap areas are usually placed into a single
contiguous area: the data segment.
 The stack segment and code segment are separate
from the data segment and from each other.
• Processes contain information about program
resources and program execution state, including:
– Process ID, process group ID, user ID, and group ID
– Environment, Working directory, Program instructions,
Registers, Stack, Heap
– File descriptors, Signal actions, Shared libraries,
– Inter-process communication tools (such as message
queues, pipes, semaphores, or shared memory).
Process creation
system(); fork();
fork()
• PCB of child is the copy of parent’s context at the time of call
– new page table is constructed
– new main memory is allocated for the data and stack segments
of the child process
• Child Process inherits
– Identical copy of memory
– CPU registers
– Open file descriptors and shared file pointers
• Child Process not inherits pid
• Child accumulated execution time is set to ‘0’
• Child gets Return value as ‘0’ ; parents get child pid
• Under Linux fork is implemented using copy-on-write pages.
So the only penalty incurred by fork is the time and memory
required to duplicate the parent’s page tables and to create
the child’s task structure.
Process Characteristics

• Each process has a unique process id


• Process id non-negative
• Each process has a parent
• Each process can determine parent id
• Process with pid = 1 (init process) is
• the ancestor for all processes
• init adopts orphaned processes
• Init waits for orphaned zombies as well
Process characteristics..

• Each process has an environment


• Each process is associated with
uid,gid,euid,egid
• Each process is asscociated with current
working directory
• Each process has pid, ppid and pgid
Process support calls
• #include <sys/types.h>
• #include<unistd.h>
• pid_t getpid()
• pid_t getppid(void)
• pid_t getpgid(pid_t pid)
• pid_t setpgid(pid_t pid, pid_t pgid)
copy-on-write
• A number of processes may access same memory
at the same time if they do not write
• new and old process use same memory after fork
• pages used by both processes are marked as write-
protected & can’t be modified by either process
• If one process needs to carry out a write, a page
fault is triggered by the memory management
hardware (MMU)
• process is interrupted
• kernel copies pages of memory and assigns the
writing process a copy of its own.
Environment
• extern char **environ;
int main( int argc, char *argv[ ], char *envp[ ] )
environment environment
environment
list strings
pointer

environ: HOME=/home/stevens\0

PATH=:/bin:/usr/bin\0

SHELL=/bin/sh\0

USER=stevens\0

LOGNAME=stevens\0

NULL
Process image overlay - exec
• Exec is a family of similar functions that do
the following :
• Replace current process image with a new
one
• Do not return if successful
• Same process and pid
• Inherit many properties before exec
• Supports a wide range of functions based
• on execve() system call
exec()
• Executes a file, transforming the calling process into a
new process
• Never return to the calling process
Family of functions for replacing process’s program with the one inside the
exec() call.
int execl( const char *path, const char *arg, ... );
int execlp( const char *file, const char *arg, ... );
(search path)
int execle( const char *path, const char *arg
, ..., char *const envp[] );(manual pass arg)
int execv( const char *path, char *const argv[] );
int execvp( const char *file, char *const argv[] );
int execve( const char *filename, char *const argv [],
char *const envp[] );
exec(..) Family

execl() execle() execlp()


Create argv Create argv Create argv

execv() execvp()
Add envp Convert file to path

execve()
Destroying Processes
• exit() library function
– Terminates and store the status
– Close all open files descriptors
– Only system call that never returns and can’t be
misused
• Process removal
– Releasing the process descriptor of a zombie
process by release_task()
• Kill function
Special Exit Cases
A child exits when its parent is not currently executing
wait()
– the child becomes a zombie
– status data about the child is stored until the parent
does a wait()
• Instruction, user-data, system data are discarded
• But process table slot is maintained in the kernel
A parent exits when 1 or more children are still running
– children are adopted by the system’s initialization process
(/etc/init) -> ppid = 1
• it can then monitor/kill them
Process life cycle

• New : The process is being created.


• Running : Instructions are being executed.
• Waiting : The process is waiting for some event to occur.
• Ready : The process is waiting to be assigned to a processor.
• Terminated : The process has finished execution.
Process scheduling
Process Control Block
• Contain the current state of process
– It is a “snapshot” of the execution and protection
environment.
– Only one PCB active at a time.
– Contains
• process state , process number, program counter
• Registers, memory limits, list of open files, etc
Dispatcher
• Gives control of the CPU to the process
selected by the scheduler.
• It involves:
– Switching context
• Save current state
• Load new state
• Switching to user mode
• Jumping to the proper location in the user
program to restart that program
Context Switch
• Context Switching the CPU to another process
requires performing a state save of the current
process and a state restore of a different
process.
• Context of a process represented in the PCB.
Process switch
Process scheduling
CPU Scheduler
• It selects a process from the processes in
memory that are ready to execute and
allocates the CPU to that process.
• Two types :
– Non-preemptive scheduling
• When process terminates or a system request cause
wait state
– Preemptive scheduling
• When interrupt occurs
• When new process become ready
Scheduling Metrics & Criteria
• Metrics
– CPU Utilization-> CPU busy in terms of %
– Throughput -> number of completed processes per time
unit.
– Turnaround Time -> interval from the time of
submission to that of completion.
– Waiting time -> total time spent waiting in the ready
queue.
– Response time -> time from request submission to first
response.
• Criteria
– Maximize : CPU utilization and Throughput
– Minimize: turnaround, waiting and Response time
Algorithms
• First come First served (FCFS)
– A non pre-emptive simplest algo; uses FIFO queue
– Complete the job in the order of arrival
Analysis
Process Burst Arrival priority Waiting time
time time
p1 0
p1 7 0 5
p2 7
p2 4 1 2
p3 11 Avg waiting time
p3 5 2 1
p4 16 56 / 5 = 11.2
p4 6 3 3
p5 22
p5 8 4 4

p1 p2 p3 p4 p5

time
0 7 11 16 22
• Shortest Job First (SJF)
– Can be preemptive & non pre-emptive
– Complete the job with shortest next CPU burst
• Round Robin
– A pre-emptive FCFS
– Predefined time quantum which should be large
w.r.t context switch time
• Priority associated with each process
– Can be preemptive & non pre-emptive
– CPU is allotted with process having highest priority
– Problem is Indefinite blocking or starvation
• Multilevel Queue
– partitions the ready queue into several separate queues
based on priority, memory size, process type etc.,
– Scheduling based on individual queue or collectively
• Multilevel feedback Queue
– Process can move between various queues to avoid aging
Process Burst Arrival priority
time time
p1 7 0 5
Waiting Time
p2 4 1 2
p3 5 2 1
p1 23 priority
p2 5
p4 6 3 3 Avg waiting time
p3 0
p5 8 4 4 52 / 5 = 10.1
p4 9
p5 15

Waiting Time Waiting Time


p1 15
p1 16 SRTF
p2 0
p2 1-1=0
p3 4 Avg waiting time
p3 5-2=3
p4 9 50 / 5 = 10
p4 16-3=13
p5 22
p5 22-4=18

SJF Avg waiting time


50 / 5 = 10

You might also like