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.

                   |                                                                                               |
                   |                          <------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 |
            
Memory layout of a process
Segment Details
Code/Text Segment (Read Only) Size = 8k (RO) Have Executable Instructions. Neither grows nor shrinks.
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}
Part desc
Initialized DS Initialized by Coder. Again its divided into 2 parts
RO(READ ONLY) Global constants are stored here. Eg: const char *a = "test"
RW(READ WRITE) Stores Initialized globals, extern variables, local static

char s[] = "test";  //Initialized Globals
int a = 1;        
static int b = 1;

extern int a;     //Extern variable

static int a;     //Local static
                                                
Unintialized DS / BSS(Block Started by Symbol) Stores uninitialized global and static. Data is Initialized to 0(by kernel) before code start executing.

int a;
static int b;
                                    
Pointer-to-Heap Data Segment also stored pointer to heap. As memory is allocated/deallocated on heap using malloc() DS grows/shrinks.

malloc()->brk()
realloc()->sbrk()
                                    
Stack Segment Stores: Local variables, Registers, Stack Frame pointer, return address, stack-based parameters, stack frame as LL

Stack Frame: {Input_parameters} {Return_address} {Local_variables}

  Return address: The address to which control should return after the function call completes.

add(int a, int b){          //0x33
    int c;               //Local
    }
main(int argc, char *argv[]){   //0x22
    int a ,b;
    add(a,b);
}   
From OS:Call to main()      //0x11

--------stack allocated in this direction---->
[ argv[] argc | 0x11 | b a ]  [ | b a | 0x22+offset | c | ]     //Input parameters are pushed in reverse order
/\<--- sf of main()  ----->  <------ sf of add()     ---->/\
|                                                         |
|                                                         rsp Stack pointer(Points to the top of the stack)
|
rbp Base pointer(Points to the base of the current stack frame. Remains fixed)
                        
Popping of Stack frame
Function add() finishes, Instruction pointer(is set to prev address)=0x22+offset
Then add() stack is cleared, Similarly main() stack is cleared
rsp, rbp are set to old values

Stack Overflow

When process uses all its stack. At end of process's stack there is a GUARD PAGE, when process goes into it.

fun() {
    fun();
}
                        

Stack Samshing

Stack overflow caused deliberately as part of an attack.

MAX Stack Size / Maximum stack allocated to process at start


8 MB(Default)  #cat /proc/pid/limits     //Linux
1 MB(Default)                            //Windows

$ ulimit -u unlimited                    //Changing stack size
                        
Why 8MB of stack? Atleast large and difficult to overflow
Why Stack size != Virtual Memory size (As Heap size = Virtual Memory size)?
  Every thread has its own Stack.if thread-1 consumes all virtual memory and nothing left for thread-2.

Segmentation Fault

Program tries to access memory that it's not allowed to access (memory violation)
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 not part of process Memory layout, but process stores pointer to its heap area
Heap is allocated on Virtual Memory/hard Disk & is used for dynamic Memory allocation.
Max Heap Size = Virtual memory size.

Heap Overflow / Memory leak

When process keeps on allocating more and more memory without freeing it causes Heap overflow.
Aftermaths: System shutdown, Crash, System becoming dead-slow.

while(1){ 
    char *p = (char *) malloc(12); 
}                            
                        

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 Table

Process Control Block


struct task_struct {
    long                 priority;
    //DOUBLY LL
    struct task_struct   *next_task, *prev_task;
    //Process PID
    int                  pid;
    //Process Group
    int                  pgrp;
    //Pointers to parent, youngest child, silbilings etc
    struct task_struct   *p_opptr, *p_pptr, *p_cptr,
    //UID,GID
    unsigned short       uid,suid,gid,sgid..;
    //File system information
    struct fs_struct     *fs;
    //IPC Information
    struct sem_queue     *semsleeping;
    //memory management info
    struct mm_struct     *mm;
    //signal handlers
    struct signal_struct *sig;
    //Register information.
};
                        

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

Register values of present process, stack pointer (rbp) etc
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