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
Java Process
C++ 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.
| 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 & Process Control Block
Process Table
Process Table is Array of structures (called Process Control Blocks),
one entry per process.
Process or Thread Control Block (struct task_struct)
What is PCB/TCB? struct task_struct containing all
information about the process's state example:
- Execution Context: Registers,
Program Counter (Store address from where to start again), Stack Pointer (SP).
- State: Running, Sleeping, Zombie, etc.
- Wait Queues: When a thread blocks on a mutex, socket, or condition
variable, the kernel places a node (wait_queue_entry_t)
- Process's accounting and scheduling information
PCB Objective? 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. (due to
context switch
or
interrupt)
struct task_struct When we create a process using
fork() or a thread using pthread_create(),
the kernel allocates a task_struct. Linux uses task_struct for both
processes and threads
|
PCB/TCB (Process/Thread Control Block)
|
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 |