KEMBAR78
Operating Systems - Processes | PDF | Process (Computing) | Kernel (Operating System)
0% found this document useful (0 votes)
16 views84 pages

Operating Systems - Processes

The document provides an overview of processes in operating systems, detailing the distinction between programs and processes, the structure of process address space, and the management of processes through process control blocks (PCBs). It explains the concepts of virtual memory, process states, process creation via cloning, and the implications of process termination, including zombies and orphans. Additionally, it highlights the advantages of virtual addressing and the role of shared libraries in process execution.

Uploaded by

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

Operating Systems - Processes

The document provides an overview of processes in operating systems, detailing the distinction between programs and processes, the structure of process address space, and the management of processes through process control blocks (PCBs). It explains the concepts of virtual memory, process states, process creation via cloning, and the implications of process termination, including zombies and orphans. Additionally, it highlights the advantages of virtual addressing and the role of shared libraries in process execution.

Uploaded by

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

Operating Systems – Processes

Dr. M. Brindha
Associate Professor
Department of CSE
NIT, Trichy-15
Executing Apps (Process)
• Process
– A program in execution
– Most important abstraction in
an OS
– Comprises of
$gcc hello.c • Code
from ELF In the
• Data
user space
• Stack of process
ELF • Heap
Executable Process • State in the OS In the kernel
(a.out) $./a.out • Kernel stack space
– State contains : registers, list
of open files, related
processes, etc.
Executable and linkable format (ELF) 2
Program ≠ Process
Program Process

code + static and global data Dynamic instantiation of code +


data + heap + stack + process
state
One program can create several A process is unique isolated entity
processes

3
4
5
6
7
8
Process Address Space
• Virtual Address Map
– All memory a process can Stack
MAX_SIZE

address
– Large contiguous array of
addresses from 0 to Heap
MAX_SIZE Data

Text
(instructions)

9
Process Address Space
• Each process has a different address space
• This is achieved by the use of virtual memory
• Ie. 0 to MAX_SIZE are virtual memory addresses

MAX_SIZE MAX_SIZE
Stack Stack

Heap Heap

Data Data
Process A Process B

Process A Process B
Text Text
Page Table Page Table
(instructions) (instructions)

0 0
1
Virtual Address Mapping
Process A Process B

Stack Stack

Heap Heap
Process A Process B
Data Page Page Data
Table Table

Text Text
(instructions) (instructions)

Virtual Memory Physical Memory Virtual Memory

1
Process Address Map in xv6
0xFE000000 • Entire kernel mapped into
every process address space
Kernel
Text + Data, – This allows easy switching from
DeviceMemory user code to kernel code (ie.
Kernel can access

during system calls)


User Process can access

KERNBASE • No change of page tables


Heap (0x80000000) needed
– Easy access of user data from
Stack
kernel space
Data

Text
(instructions)
0

1
Advantages of Virtual Address Map
• Isolation (private address space)
– One process cannot access another process’ memory
• Relocatable
– Data and code within the process is relocatable
• Size
– Processes can be much larger than physical memory

1
14
15
16
17
18
19
20
21
22
23
Process Stacks
Kernel (Text + Data)

• Each process has 2 stacks


– User space stack Kernel Stack
• Used when executing user code for process

– Kernel space stack


• Used when executing kernel
code (for eg. during system Heap
calls) User stack
– Advantage : Kernel can execute even if for process
user stack is corrupted Data
(Attacks that target the stack, such as
buffer overflow attack, will not affect the Text
kernel) (instructions)

Process Address
Space
2
Process Management-PCB
• Each process has a PCB (process control block)
defined by struct proc in xv6
• Holds important process specific information
• Why?
– Allows process to resume execution after a while
– Keep track of resources used
– Track the process state

ref : proc.h (struct proc) (2353) 10


Summary of entries in PCB
• More entries
Size of process memory
Page directory pointer for
process

Kernel stack pointer

Files opened

Current working directory

Executable name
2
Entries in PCB
• PID
– Process Identifier
– Number incremented sequentially
• When maximum is reached
• Reset and continue to increment.
• This time skip already allocated PID numbers

2
Page Directory Pointer
Page Directory Pointer

Stack

Process A Heap
ProcessA
Data Page
Table

Text
(instructions)

Virtual Memory Physical Memory


2
29
Context Pointer
Kernel (Text + Data)

• Context pointer
– Contains registers used for
context switches. Kernel Stack
for process
– Registers in context : %edi,
%esi, %ebx, %ebp, %eip
– Stored in the kernel stack
Heap
space
Stack
context
Data

Text
(instructions)

3
Entries in PCB
SS
• Pointer to trapframe ESP
EFLAGS
CS
EIP
Error Code
trapframe
Trap Number
ds
es

eax
ecx

esi
edi
esp

(empty)

3
Process States
• Process State : specifies the state of the process

NEW READY

BLOCK RUNNING

EMBRYO  The new process is currently being created


RUNNABLE  Ready to run
RUNNING  Currently executing
SLEEPING  Blocked for an I/O
Other states ZOMBIE (later)
ref : proc.h (struct proc) 2350 13
Scheduling Runnable Processes

Running
Process

Queue of RUNNABLE Processes


CPU
Scheduler

Scheduler triggered to run when timer interrupt occurs or when


running process is blocked on I/O
Scheduler picks another process from the ready queue
Performs a context switch
Creating a Process by Cloning
• Cloning
– Child process is an exact replica of the parent
– Fork system call
Parent Child
Process 1 Process 1 Process 2

system call
fork

Kernel
=> Kernel
(execute fork) (execute fork)

3
Creating a Process by Cloning
(usingfork system call)
• In parent int pid;
– fork returns child
pid p = fork();
if (p > 0){
• In child process printf(“Parent : child PID = %d”, p);
p = wait();
– fork returns 0 printf(“Parent : child %d exited\n”, p);
} else{
printf(“In child process”);
• Other system exit(0);
}
calls
– Wait, returns pid
of an exiting
child

3
37
38
Virtual Addressing Advantage (easy
to make copies of a process)
• Making a copy of a process
is called forking.
Parent
– Parent (is the original) Page
– child (is the new process) Table

• When fork is invoked,


– child is an exact copy of
parent
Child
• When fork is called all pages Page
are shared between parent Table
and child Physical Memory
• Easily done by copying the
parent’s page tables
3
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Modifying Data in Parent or Child

int i=0, pid;


pid = fork();
if (pid > 0){
sleep(1);
Output printf("parent : %d\n", i);
wait();
parent : 0 } else{
i = i + 1;
child : 1 printf("child : %d\n", i);
}

5
56
57
58
59
Copy on Write
(COW)
• When data in any of the shared pages
change, OS intercepts and makes a copy
of the page.
Parent
• Thus, parent and child will have different Page
copies of this page Table
• Why?
– A large portion of executables are not used.
– Copying each page from parent and child
would incur significant disk swapping..
i of child here
huge performance penalties. Child
– Postpone coping of pages as much as Page i of parent here
possible thus optimizing performance Table

This page now is no longer shared

6
How COW works
• When forking,
– Kernel makes COW pages as read only
– Any write to the pages would cause a page
fault
– The kernel detects that it is a COW page and
duplicates the page

6
Executing a Program
(exec system call)
• exec system call
int pid;
– Load into memory and then
pid = fork();
execute if (pid > 0){
• COW big advantage for pid = wait();
} else{
exec execlp("ls", "", NULL);
exit(0);
– Time not wasted in copying
pages.
– Common code (for example
shared libraries) would
continue to be shared

62
63
64
65
66
Virtual Addressing Advantages
(Shared libraries)
• Many common functions such as printf implemented in shared libraries
• Pages from shared libraries, shared between processes

Process A Process B

Process A Process B
Page Page
Table Table

printf(){ …}
printf(){ …} printf(){ …}

Virtual Memory Physical Memory Virtual Memory 26


68
The first process
• Unix : /sbin/init (xv6 initcode.S)
– Unlike the others, this is created by the
kernel during boot
– Super parent.
• Responsible for forking all other processes
• Typically starts several scripts present in /etc/init.d
in Linux

6
Process tree
Processes in the init.d

system arranged in
the form of a tree. NetworkManager lightdm

dhclient dnsmasq
init

pstree in Linux gnome-session

Who creates the first process?


compiz

7
71
72
73
Process Termination
• Voluntary : exit(status)
– OS passes exit status to parent via wait(&status)
– OS frees process resources

• Involuntary : kill(pid, signal)


– Signal can be sent by another process or by OS
– pid is for the process to be killed
– signal a signal that the process needs to be killed
• Examples : SIGTERM, SIGQUIT (ctrl+\), SIGINT (ctrl+c),
SIGHUP

7
75
76
77
78
Zombies
• When a process terminates it becomes a zombie (or
defunct process)
– PCB in OS still exists even though program no longer executing
– Why? So that the parent process can read the child’s exit status
(through wait system call)
• When parent reads status,
– zombie entries removed from OS… process reaped!
• Suppose parent does’nt read status
– Zombie will continue to exist infinitely … a resource leak
– These are typically found by a reaper process

7
Orphans
• When a parent process terminates before its child
• Adopted by first process (/sbin/init)

=>

8
Orphans contd.
• Unintentional orphans
– When parent crashes
• Intentional orphans
– Process becomes detached from user session and
runs in the background
– Called daemons, used to run background services
– See nohup

8
82
83
84
Thank You!!!

You might also like