Docsity

Processes and Inter-Process Communication in Operating Systems - Prof. Material Engineerin, Study notes of Material Engineering

Toggle sidebar
bg1
Processes and Inter-Process
Communication in Operating
Systems
Process Concept
Process Definition
A process is a program in execution. It is an active entity, unlike a
program which is a passive entity.
A process has multiple components:
The program code, also called the text section
The current activity, including the program counter and processor
registers
A stack containing temporary data (function parameters, return
addresses, local variables)
A data section containing global variables
A heap containing memory dynamically allocated during runtime
Process Representation
A program becomes a process when the executable file is loaded into
memory and its execution is started.
One program can have multiple processes, for example, when multiple
users execute the same program.
The operating system represents a process using a Process Control
Block (PCB) or Task Control Block (TCB), which contains information
such as the process state, process number, program counter, CPU
registers, scheduling information, memory management information,
accounting information, and I/O status information.
Process States
As a process executes, it can be in one of the following states:
New: The process is being created.
Running: Instructions are being executed.
Waiting: The process is waiting for some event to occur.
Ready: The process is waiting to be assigned to a processor.
Terminated: The process has finished execution.
Threads
A process can have multiple threads of execution, each with its own
program counter and set of registers.
Threads allow a process to have multiple locations executing
concurrently.
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Processes and Inter-Process Communication in Operating Systems - Prof. Material Engineerin and more Study notes Material Engineering in PDF only on Docsity!

Processes and Inter-Process

Communication in Operating

Systems

Process Concept

Process Definition

A process is a program in execution. It is an active entity, unlike a program which is a passive entity. A process has multiple components: The program code, also called the text section The current activity, including the program counter and processor registers A stack containing temporary data (function parameters, return addresses, local variables) A data section containing global variables A heap containing memory dynamically allocated during runtime

Process Representation

A program becomes a process when the executable file is loaded into memory and its execution is started. One program can have multiple processes, for example, when multiple users execute the same program. The operating system represents a process using a Process Control Block (PCB) or Task Control Block (TCB), which contains information such as the process state, process number, program counter, CPU registers, scheduling information, memory management information, accounting information, and I/O status information.

Process States

As a process executes, it can be in one of the following states: New: The process is being created. Running: Instructions are being executed. Waiting: The process is waiting for some event to occur. Ready: The process is waiting to be assigned to a processor. Terminated: The process has finished execution.

Threads

A process can have multiple threads of execution, each with its own program counter and set of registers. Threads allow a process to have multiple locations executing concurrently.

Process Representation in Linux

In Linux, a process is represented by the task_struct C structure, which includes information such as the process ID, state, scheduling information, parent process, open files, and address space.

Process Scheduling

Scheduling Queues

The process scheduler maintains the following queues: Ready queue: Contains all processes residing in main memory and ready to execute. Wait queues: Contain processes waiting for an event, such as I/O completion. Processes migrate among the various queues as they change state.

Context Switch

When the CPU switches from one process to another, a context switch occurs, where the system must save the state of the old process and load the saved state for the new process. Context switch time is overhead, as the system does no useful work while switching. Hardware support, such as multiple sets of registers per CPU, can reduce context switch time.

Multitasking in Mobile Systems

Some mobile operating systems, like early versions of iOS, only allow one foreground process to run at a time, with other processes suspended in the background. Android allows both foreground and background processes, with background processes using services to perform tasks.

Operations on Processes

Process Creation

Processes can create child processes, forming a tree of processes. Resource sharing options include: Parent and children share all resources Children share a subset of parent's resources Parent and child share no resources Execution options include: Parent and children execute concurrently Parent waits until children terminate Address space options include: Child is a duplicate of the parent

Communication Models

There are two models of IPC:

Shared memory Message passing

Inter-Process Communication - Shared Memory

Shared memory is an area of memory shared among the processes that wish to communicate. The communication is under the control of the user processes, not the operating system. The major issue is to provide a mechanism that will allow the user processes to synchronize their actions when they access shared memory.

Producer-Consumer Problem

The producer-consumer relationship is a paradigm for cooperating processes, where a producer process produces information that is consumed by a consumer process. There are two variants of the producer-consumer problem: Unbounded-buffer: places no practical limit on the size of the buffer. Bounded-buffer: assumes that there is a fixed buffer size.

Bounded-Buffer - Shared-Memory Solution

The shared data for the bounded-buffer solution is defined as follows:

 ## define BUFFER_SIZE 10 typedef struct { // buffer elements // in, out, count } buffer_type; buffer_type buffer; ``` ## Producer-Consumer Problem using Shared ## Memory ## Shared Memory Buffer c } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; The provided code snippet represents a shared memory buffer used for the producer-consumer problem. The buffer can hold BUFFER_SIZE number of item elements. The in and out variables keep track of the indices for the next item to be produced and consumed, respectively. ## Producer Process c item next_produced; while (true) { /* produce an item in next_produced */ while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; } The producer process continuously produces new items and stores them in the shared buffer. It checks if the buffer is full (i.e., (in + 1) % BUFFER_SIZE == out) before adding a new item. If the buffer is full, the producer process waits until there is space available. ## Consumer Process c item next_consumed; while (true) { while (in == out) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; /* consume the item in next_consumed */ } The consumer process continuously consumes items from the shared buffer. It checks if the buffer is empty (i.e., in == out) before removing an item. If the buffer is empty, the consumer process waits until a new item is available. **Inter-Process Communication (IPC) - Message** **Passing** ## Message Passing Mechanism **Message System** : Processes communicate with each other without using shared variables. **IPC Facility** : Provides two operations: send(message): Sends a message. receive(message): Receives a message. The message size can be either fixed or variable. ## Establishing Communication If processes P and Q wish to communicate, they need to: 1. Establish a communication link between them. 2. Exchange messages via send and receive operations. ## Implementation Issues The implementation of the message passing mechanism involves several considerations: - How are communication links established? - Can a link be associated with more than two processes? - How many links can there be between every pair of communicating processes? - What is the capacity of a link? - Is the size of a message that the link can accommodate fixed or variable? - Is a link unidirectional or bi-directional? Non-blocking: Non-blocking send: The sender sends the message and continues. Non-blocking receive: The receiver receives a valid message or a null message. Different combinations are possible, including a rendezvous (both send and receive are blocking). **Producer-Consumer Problem using Message** **Passing** ## Producer Process c message next_produced; while (true) { /* produce an item in next_produced */ send(next_produced); } The producer process continuously produces new items and sends them using the send operation. ## Consumer Process c message next_consumed; while (true) { receive(next_consumed) /* consume the item in next_consumed */ } The consumer process continuously receives items using the receive operation and consumes them. **Buffering** The communication link can have different buffering mechanisms: - **Zero capacity** : No messages are queued on the link. The sender must wait for the receiver (rendezvous). - **Bounded capacity** : The link has a finite length of n messages. The sender must wait if the link is full. - **Unbounded capacity** : The link has an infinite length. The sender never waits. **Examples of IPC Systems** ## POSIX Shared Memory The process first creates a shared memory segment using shm_open(). The size of the shared memory object is set using ftruncate(). The shared memory is mapped to the process's address space using mmap(). Reading and writing to the shared memory is done using the pointer returned by mmap(). ## Mach Message Passing Mach communication is message-based, even for system calls. Each task gets two ports at creation: Task Self port and Notify port. Messages are sent and received using the mach_msg() function. Ports are created using mach_port_allocate(). Send and receive operations have flexible options, such as waiting indefinitely, waiting for a limited time, returning immediately, or temporarily caching a message. ## Windows Local Procedure Calls (LPC) Windows uses an advanced LPC facility for message-passing communication. Communication works between processes on the same system. Processes use ports (like mailboxes) to establish and maintain communication channels. The client opens a handle to the subsystem's connection port object, sends a connection request, and the server creates two private communication ports, returning the handle to one of them to the client. **Pipes** Pipes act as a conduit allowing two processes to communicate. Issues: Is communication unidirectional or bidirectional? Must there exist a relationship (e.g., parent-child) between the communicating processes? Ordinary pipes: Cannot be accessed from outside the process that created it. Typically, a parent process creates a pipe and uses it to communicate with a child process. Named pipes: Can be accessed without a parent-child relationship. **Ordinary Pipes** Ordinary Pipes allow communication in standard producer-consumer style. The producer writes to one end (the write-end of the pipe), and the consumer reads from the other end (the read-end of the pipe). Ordinary pipes are unidirectional. Ordinary pipes require a parent-child relationship between the communicating processes. **Named Pipes** Named pipes are more powerful than ordinary pipes. Communication is bidirectional. No parent-child relationship is necessary between the communicating processes. Several processes can use the named pipe for communication. Named pipes are provided on both UNIX and Windows systems.