0% found this document useful (0 votes)
19 views19 pages

Lect 3

Uploaded by

ygf2024
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)
19 views19 pages

Lect 3

Uploaded by

ygf2024
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/ 19

Lecture 3 : Process Management

INSTRUCTOR : DR. BIBHAS GHOSHAL ( B I B H A S . G H O S H A L @ I I I TA . A C . I N )

1
Lecture Outline
 What is Process?
 Process States
 Process Creation
 Process Address Space
 Creating a process by Cloning
 Process Termination
 Zombie Process
 Orphans Process
 Process Address Map in xv6

2
Process
 Program in Execution Process Comprises of:
#include<stdio.h>  Code
int main(){  Data In User space of process
Char str[] = “Hello World!\n”;  Stack
Printf(“%s”,str);  Heap
}  State in OS
 Kernel stack In Kernel space of process
$ gcc hello.c

ELF Process
Executable(a.out)
$ ./a.out
Program Process
  Code + static & global data  Dynamic instantiation of code + data + heap + stack + process
 One program can create several state
processes  A process is unique isolated entity

3
Data Structure of Process
Stack Recursive calls  Attributes of a Process
 Process Id
Heap Dynamic allocation of data structures  Program counter
Static Variable  Process State
& Once created, it will be there for lifetime
 Priority
Global Variable  General Purpose Register
 List of open files
a.out Executable  List of open devices
 Protection
 While executing a process, you are restricted to process boundary or
else segmentation fault will occur.
 Heap grows upwards while Stack grows downwards.

4
Process State
Process Scheduled Exit
NEW READY RUN Terminate
◦ Created

I/O Request over I/O Request

WAIT

Running: In the running state, a process is running on a processor. This means it is executing instructions.
Ready: In the ready state, a process is ready to run but for some reason the OS has chosen not to run it at
this given moment.
Wait: In the blocked state, a process has performed some kind of operation that makes it not ready to run
until some other event takes place. A common example: when a process initiates an I/O request to a disk,
it becomes blocked and thus some other process can use the processor.

5
Process Creation
 The first thing that the OS must do to run a program is to load its code and
any static data (e.g., initialized variables) into memory, into the address
space of the process. Programs initially reside on disk (or, in some modern
systems, flash-based SSDs) in some kind of executable format; thus, the
process of loading a program and static data into memory requires the OS
to read those bytes from disk and place them in memory somewhere.
 Once the code and static data are loaded into memory, there are a few
other things the OS needs to do before running the process. Some memory
must be allocated for the program’s run-time stack (or just stack).
 The OS allocates this memory and gives it to the process. The OS may also
allocate some memory for the program’s heap. In C programs, the heap is
used for explicitly requested dynamically-allocated data.
 The OS will also do some other initialization tasks, particularly as related to
input/output (I/O). For example, in UNIX systems, each process by default
has three open file descriptors.

6
Process Address Space
 Virtual Address Map MAX_SIZE
Stack

• All memory a process can address


Process A Heap Process B
• Large contiguous array of address from 0 to MAX_SIZE Data
Page Table Text Page Table
• Each Process has different address space [Instructions]

• This is achieved by use of virtual memory  Process Stack


 User Space Stack (used when using user code)
i.e. 0 to MAX_SIZE are virtual memory addresses
 Kernel Space Stack (used when using kernel code)
 Advantages of Virtual Address Space  Advantages: Kernel can execute even when stack
is corrupted (Attacks such as buffer overflow will
• Isolation (Private address space) not affect Kernel)
 Each Process has PCB (Process Control Block) :
• Relocatable (Data & Code within the process is relocatable)
Holds process specific information
• Size (Processes can be much larger than physical memory)  PID : Process Identifier(No. incremented sequentially)

7
Virtualizing The CPU

Result of program cpu.c : Running many programs at once


Turning a single CPU (or small set of them) into
a seemingly infinite number of CPUs and thus
allowing many programs to seemingly run at
once is what we call virtualizing the CPU.
Code that loops and prints (loop.c)

8
Virtualizing The Memory

Result of program mem.c : Running the memory


program multiple times
Each running program has allocated memory
at the same address (0x200000), and yet
each seems to be updating the value at
0x200000 independently! It is as if each
A program that accesses memory (mem.c)
running program has its own private memory,
instead of sharing the same physical memory
with other running programs.
9
Virtual Memory Advantage
 Easy to make copies of process
Parent
 Making a copy of process is called forking
Page
 Parent is the original Table

 Child (New Process)


 When fork() is invoked Child
Page
 Child is exact copy of Parent Table

 All pages are shared between Parent & Child


 Easily done by copying the parent’s page tables

10
Creating A Process by Cloning
 Cloning : Child Process is expect replica of Parent  Operation on Process
 Creation
 fork() system call  Scheduling
Process 1  Execution
Parent Child  Killing/delete
Process 1 Process 2
Kernel
Execute
fork

 In Parent process fork() return child process id


 In Child process fork() return process id of exit child
 wait() : If there is at least one child process running, caller is blocked until of its child exist &
then caller resumes.
11
The fork() System Call

12
The wait() System Call

13
The exec() System Call

14
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
 A signal that the process needs to be killed
 For example : SIGTERM, SIGQUIT(ctrl+\), SIGINT(ctrl+c), SIGHUP

15
Zombie Process
 Child terminates before Parent

 The OS stores some info about the terminated child (PID, termination reason etc.)

 The Parent of the terminated child accesses this info using the wait() system call.

 If a parent did not call wait(), it becomes a zombie

 Process has no allocated resources but its entry in the process table (PCB)

 A zombie process can be noticed using ps command, which prints on the state column of the process (the letter ‘z’)

 PCB in OS still exist even though program no longer executing

 When parent reads status

 zombie entries removed from OS… process reaped!

 When parent doesn’t read status

 zombie will continue to exist infinitely… a resource leak

 These are typically found by a reaper process 16


Orphan Process
 When a parent process terminates before its child
 Adopted by first process (/sbin/init)

17
Orphans contd.
 Unintentional orphans
 When parent crashes
 Intentional orphans
 Process becomes detached from user session and runs in the background
 Called Daemons process, used to run background services
 See nohup

18
Process Address Map in xv6
Kernel
Text + data
Entire Kernel mapped into
every process address space
User Kernel
Process Process
can can Easy Switching from user
access access code to kernel code (during
system calls) : No change of
Text
Page table

 Context Pointer
 Contains register used for Context Switches
 Registers in context: %edi, %esi, %ebx, %ebp, %eip
 Stored in Kernel Space

The xv6 Proc Structures

19

You might also like