# Unit - 5 ## Structure of Process: A process is an instance of a running program in an operating system. It consists of several components that define its behavior and execution state. The main components of a process include: 1. Process ID (PID): Each process is assigned a unique identifier called a Process ID. This ID is used by the operating system to identify and manage the process. 2. Process State: A process can be in one of several states, including: a. Running: The process is currently being executed by the CPU. b. Ready: The process is waiting to be assigned to a CPU for execution. c. Blocked: The process is waiting for a particular event or resource to become available. d. Terminated: The process has finished its execution or has been explicitly terminated. 3. Program Counter (PC): The Program Counter is a register that keeps track of the address of the next instruction to be executed in the process. 4. Register Set: The Register Set consists of various CPU registers that store temporary data and execution context during the process execution. 5. Memory Management Information: This includes information about the process's address space, such as the base address, limit, and memory segments (code, data, stack). 6. Open Files: Processes may have open file descriptors that point to files or I/O devices opened by the process. 7. Process Control Block (PCB): The PCB is a data structure maintained by the operating system for each process. It stores all the necessary information about a process, including its state, program counter, register values, memory management details, open file descriptors, and other relevant data. ### Process States and Transitions: A process can transition between different states based on its execution and events occurring in the system. Common state transitions include: 1. New: The process is being created. 2. Ready: The process is waiting to be assigned to a CPU. 3. Running: The process is currently being executed by the CPU. 4. Blocked: The process is waiting for an event or resource. 5. Terminated: The process has finished execution or has been explicitly terminated. ![](https://hackmd.io/_uploads/rk-qrBIS3.png) ### Layout of System Memory: The system memory of a process is divided into different regions: 1. Text Segment (Code Segment): This region contains the executable code of the process, typically read-only. 2. Data Segment: This region holds initialized static variables and global variables. 3. BSS Segment: The Block Started by Symbol (BSS) segment contains uninitialized or zero-initialized static and global variables. 4. Heap: The heap is a dynamically allocated memory region used for dynamic memory allocation during runtime. 5. Stack: The stack is used for storing local variables, function call information, and return addresses. It grows and shrinks automatically as functions are called and return. ### The Context of a Process: The context of a process refers to the set of data and resources that need to be saved and restored when switching between processes. It includes: 1. Register Values: The values of CPU registers, including the program counter, stack pointer, and general-purpose registers. 2. Process State: The current state of the process, such as running, ready, or blocked. 3. Memory Management Information: Information about the process's memory allocation and address space. 4. Open File Descriptors: The list of open files or I/O devices associated with the process. ### Saving the Context of a Process: When a context switch occurs (e.g., when the operating system switches from one process to another), the context of the current process is saved to the process's PCB. This involves saving the register values, process state, memory management information, and other relevant data. ### Manipulation of the Process Address Space: Processes can manipulate their address space by allocating and deallocating memory dynamically. This is typically done through system calls such as malloc and free, which allocate and deallocate memory from the heap region. The process can also manipulate its stack by pushing and popping values during function calls and returns. ### Sleep: The sleep system call is used by a process to voluntarily relinquish the CPU and enter a blocked state for a specified amount of time. The process remains in the blocked state until the sleep duration elapses or until it receives a signal to wake up. ## Process Control: 1. Process Creation: Processes can be created by the operating system or by other processes using system calls such as fork or exec. The fork system call creates a new process by duplicating the existing process, while the exec system call replaces the current process image with a new program. 2. Signals: Signals are software interrupts that allow processes to communicate with each other or with the operating system. They can be used to notify a process about events or to request a specific action. 3. Termination: Processes can terminate either voluntarily by calling the exit system call or involuntarily due to an error or termination signal. When a process terminates, it releases its resources and exits the system. 4. Awaiting Process: Processes can wait for the completion of other processes or specific events using system calls such as wait or waitpid. These calls allow a process to pause its execution until the desired condition is met. 5. Invoking Other Programs: Processes can invoke other programs or utilities by using system calls such as exec. The exec system call replaces the current process image with a new program, allowing the process to execute a different program. 6. User ID (UID) of a Process: Each process is associated with a User ID (UID), which identifies the user who owns the process. The UID is used for access control and determining permissions for various resources. 7. Changing Size of a Process: Processes can dynamically change their memory size by using system calls such as brk or mmap. These calls allow the process to increase or decrease its memory allocation dynamically. 8. The Shell: The shell is a command-line interface that provides a user-friendly way to interact with the operating system. It interprets user commands and executes them as processes or programs. 9. The System Boot and the INIT Process: During system boot, the INIT process (PID 1) is the first process created by the kernel. The INIT process is responsible for initializing the system and starting other essential processes and services. It acts as the parent process for all other processes in the system. Processes are fundamental entities in an operating system, responsible for executing programs, managing resources, and enabling multitasking and concurrent execution. The operating system provides various mechanisms and system calls to control, manage, and interact with processes effectively.