What is process?
-
Running instance of program. All processes are decedents of swapper
process(PID=0). Both(threads, processes) are independent sequence of
operations
fork() = Create child process
Memory Layout of a Process
-
After compilation obj file is created
ELF(Executable and linking format) format. This ELF file has (.text=CS, .data & .rodata = DS and others) This
file is loaded in memory which creates Memory layout of the process.
Heap [ ]
/\
|
| |<------DATA SEGMENT (static, global) -----------> |
[Kernel] Process-2 |[STACK] shared_libraries {PTR-To-HEAP} {Uninitialized_BSS} {Initialized DS} [TEXT/CODE SEGMENT]|
| -----> BlockStartedBySymbol <-RW--><--RO--> 0x0000|
Object/ELF File ===> |ELF Header|.data Header|.rodata Header|.text Header |.data|.rodata|.text|
/ / \
/ / \
Memory Layout ==> | Data Segment |Code Segment| Stack |
| Segment | Details | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Code/Text Segment (Read Only) Size = 8k |
When a process is run twice OS maintains 1 set of pages kept in main memory. Both processes points to same memory (ie does not have personal CS) |
||||||||||||
| Data Segment {Global, Static} |
|
||||||||||||
| Stack Segment |
Stack Frame: {Input_parameters} {Return_address} {Local_variables}
Popping of Stack frame
Then add() stack is cleared, Similarly main() stack is cleared rsp, rbp are set to old values Stack Overflow
Stack Samshing
MAX Stack Size / Maximum stack allocated to process at start
Why 8MB of stack? Atleast large and difficult to
overflowWhy Stack size != Virtual Memory size (As Heap size = Virtual Memory size)? Every thread has its own Stack.if thread-1 consumes all and nothing left for thread-2. Segmentation Fault
eg: null pointer (a pointer that doesn't point to any valid memory location), Accessing memory beyond bounds a[5]. access a[5] |
||||||||||||
| Heap |
Heap is allocated on Virtual Memory/hard Disk & is used for dynamic Memory allocation. Max Heap Size = Virtual memory size. Heap Overflow / Memory leakWhen process keeps on allocating more and more memory without freeing it causes Heap overflow.Aftermaths: System shutdown, Crash, System becoming dead-slow.
|
Process States
-
When a process executes, it passes through different states.
a. Start: Initial state when a process is first started/created.
b. Ready: The process is waiting to be assigned to a processor.
c. Running(Flag R): Once the process has been assigned to a processor by the OS scheduler state changes to running.
d. Waiting/Sleeping(Flag S): Process moves into the waiting state if it needs to wait for a resource, eg: waiting for user input or file.
e. Terminated or Exit: Once the process finishes its execution, or it is terminated by the OS. Here it waits to be removed from main memory.
Process Table
-
What is Process Table?
Process Table is Array of structures (called Process Control Blocks), one entry per process.
Entry contains important information about the process's state:
Its Program counter: Store address from where to start again
stack pointer
memory allocation
the status of its open files
Process's accounting and scheduling information
And everything else about the process that must be saved when the process is switched from running to ready or blocked state so that it can be restarted later
-
Why Process Table?
The main objective of process table entry is to restart process from state where it left off, as it had never stopped.
Process state which is interrupted using Interrupt is also saved on process table for later resumption.
|
Process Control Block
|
PCB / Process Control Block / Process Context
-
PCB is a data structure(struct task_struct) for storing all
information of a process inside process table
-
Why needed? When process changes state (ie switched from
running to waiting/sleeping state(due to
context switch
or
interrupt))
kernel stores info in PCB, so that process can resumed later from same
point using this information.
Information of child processes is stored in PCB of parent process etc.
Information stored in PCB
Process-state: Any of 5
pid: process-id
Program Counter/Instruction Pointer: Address of next instruction to be executed on Code segment of this process.
priority: This is a number. Process having higher priority execute 1st.
General purpose registers: Stores results of calculations done by process in Memory segment.
List of open files, list of open devices. This all information is called PROCESS CONTEXT.
Ways of Process Creation, Terminaton
Process Creation Ways
-
1. System initialization: At boot numerous processes are created.
2. Execution of a process-creation system call by a running process. fork(), CreateProcess()=Windows
3. A user request to create a new process.
4. Initiation of a batch job.
Process Termination ways
| way | Description | Example |
|---|---|---|
| 1. Normal exit (voluntary) | Most processes terminate because they have done their work | Scheduler terminates them |
| 2. Error exit (voluntary) | process discovers a fatal error | `gcc foo.c` is intended for reading file but does not exists. It may exit |
| 3. Fatal error (involuntary) | Code bug | divide by 0, but this may be handled by process itself using Interrupt(signals). |
| 4. Killed by another process (involuntary) | 1 process tells OS to kill other process | `kill` system call. `TerminateProcess` on windows |