Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OS Process

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Operating System - Process

The term “process” was first used by the operating system designers of the
MULTICS system way back in 1960s.

Process is dynamic execution context of an executing program.


Or
Process is a program in execution.
Or
Process is an abstraction of a running program.
We can define a process is an executing program, including the current values
of the program counter, registers, and variables.

Program is a group of instructions whereas the process is the activity.


A process executes sequentially, one instruction at a time.
Each process operates running instructions sequentially until it's interrupted or
completes.

Sizes of the text and data sections are fixed, as their sizes do not change during program
run time.

©HIMANSHU NARAYAN PRASAD 1

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Diagram of process state in memory

Processor- bound program: A program having long processor bursts.


I/O- bound program: A program having short processor bursts.
(Burst represents a period of activity where the program is either doing computations (a CPU
burst) or performing I/O operations (an I/O burst))

The process management functions include many terms like:


• Process creation, Termination, Controlling of the process
• Process Scheduling
• Interrupt handling

• Switching between the processes, Inter process communication support


• Process synchronization
• Management of Process Control Blocks.

©HIMANSHU NARAYAN PRASAD 2

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Process at run time can be either–


• Implicit tasking - Implicit tasking means that processes are defined by
the system.
• Explicit tasking- Explicit tasking means that programs explicitly define
each process and some of its attributes

Process can be either independent or cooperating.

Independent process:
• Their state is not shared by any other process.
• Their execution is deterministic (the results of execution depend only
on the input values.)
• Their execution is reproducible, i.e., the results of execution will
always be the same for the same input.
• Their execution can be stopped and restarted without any negative
effect.

Cooperating process:
• Their states are shared by other processes.
• Their execution is not deterministic, i.e., the results of execution
depend on relative execution sequence and cannot be predicted in
advance.

Advantage of Cooperative process:


a) It can improve performance by overlapping activities or performing work
in parallel
b) It enables an application to achieve a better program structure as a set
of cooperating processes, where each is smaller than a single monolithic
program
c) It can easily share information between tasks

©HIMANSHU NARAYAN PRASAD 3

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Process State
A process state may be in one of the following:
 New : The process is being created.
 Ready : The process is waiting to be assigned to a processor.
 Running : Instructions are being executed.
 Waiting/Suspended/Blocked: The process is waiting for some event to
occur.
 Halted/Terminated : The process has finished execution.

3 4

Transition State:
 Transition 4 appears when a process discovers that it cannot continue.
In order to get into blocked state, some systems execute a system call
block().
 Transition 1 and 2 are caused by the process scheduler, a part of the
operating system.
Transition 1 occurs when the scheduler decides that the running process
has run long enough, and it is time to let another process have some
CPU time.
Transition 2 occurs when all other processes have had their share and it
is time for the first process to run again.
 Transition 3 appears when the external event for which a process was
waiting was happened.

©HIMANSHU NARAYAN PRASAD 4

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Implementation of process
To implement the process model, the operating system maintains an array of
structures, called the process table or process control block (PCB) or Switch
frame.
Diagram of PCB:

Information stored in PCB:


 Process state - new, ready, running, waiting or halted
 Process number - each process is identified by its process number called
process ID
 Program counter - address of the next instruction to be executed for this
process
 CPU registers - depending on the microprocessor architecture
 Memory management information - include base and bounds registers
or page table
 I/O status information, Processor scheduling information
 List of open files

©HIMANSHU NARAYAN PRASAD 5

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Process operations

 Process creation –
One process can create other processes to do work.
 The creator is called the parent and the new process is the child
 The parent defines (or donates) resources and privileges to its
children
 A parent can either wait for the child to complete, or continue in
parallel

In Unix, the fork() system call called is used to create child processes
 fork copies variables and registers from the parent to the child
 The only difference between the child and the parent is the value
returned by fork-
In the parent process, fork returns the process id of the child
In the child process, the return value is 0
 The parent can wait for the child to terminate by executing the
wait system call or continue execution
 The child often starts a new and different program within itself,
via a call to exec() system call.

 Process termination –
On process termination, the OS reclaims all resources assigned to the
process.
In Unix-
 a process can terminate itself using the exit system call.
 a process can terminate a child using the kill system call.

List of important system call in process management-


1. fork() - Creates a child process that is identical to the parent process.
2. waitpid(pid, &statloc, opts) - Waits for the specified child process (pid)
to terminate, and retrieves its exit status.
3. wait(&status) - Waits for any child process to terminate and retrieves
the exit status.
4. exit(status) - Terminates the calling process and returns the status to
the parent.
5. getpid() - Returns the process ID of the calling process.
6. getpgrp() - Returns the process group ID of the calling process.
7. exec() - A family of functions like execve() that replace the current
process image with a new executable.

©HIMANSHU NARAYAN PRASAD 6

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Context Switching
A context is the contents of a CPU's registers and program counter at any point
in time.
A context switch (also sometimes referred to as a process switch or a task
switch) is the switching of the CPU (central processing unit) from one process
or to another.
Often, all the data that is necessary for state is stored in one data structure,
called a process control block (PCB).

In above diagram - Two processes P0 and P1 are in ready queue.


 CPU is executing process P0 and process P1 is in wait state.
 If an interrupt occurs for P0, the operating system suspends the
execution of the P0, and stores the context(Program
Counter+Registers+Memory Information) to PCB of P0.
 OS then handle the interrupt. After handling, load the context of P1 from
PCB of P1. The CPU now continues executing P1
Process of switching from P0 to P1 (or vice versa) is example of context switching.

Context-switch times are highly dependent on hardware support.


©HIMANSHU NARAYAN PRASAD 7

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Processes use inter-process communication (IPC) methods, such as pipes,


sockets, shared memory, message passing to communicate with each other.
Pipes- A pipe in Unix/Linux is a method of inter-process communication (IPC)
that allows data to be passed from one process to another in a unidirectional
flow (one direction).

Pipes are implemented using the pipe() system call in programming.


“|” symbol represents a pipe in shell commands (like in Unix/Linux shells such
as Bash)
Example –
Cat file1 file2 file3 fil4 | grep “abcd”
Above program concatenate(join) file1, fil2, fil3 and file4(one process) then
after joining search for “abcd” text pattern (another process).

Sockets- It is a fundamental communication mechanism used for network-


based inter-process communication (IPC).It can be bidirectional(support two-
way communication)
It allows two processes, potentially running on different machines, to exchange
data over a network.
Sockets use standard protocols like TCP (Transmission Control Protocol) for
reliable, connection-oriented communication, or UDP (User Datagram
Protocol) for faster, connectionless communication.
Each socket is defined by a combination of an IP address and a port number,
which serve as the communication endpoints.

©HIMANSHU NARAYAN PRASAD 8

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Shared memory
Shared memory allows multiple processes to access the same block of memory
(provides fast data exchange).
This model enables direct access to a common memory area, avoiding the
overhead of copying data between processes.
Once a shared memory segment is created using the shmget() system call, it
can be attached to a process's address space with shmat() system call.
Shared memory is often used in high-performance applications where the
speed of data transfer is crucial, such as in real-time systems, multimedia
applications, or large-scale simulations.
Shared memory can be faster than message passing because message passing
systems are typically implemented using system calls and which require the
more time-consuming task of kernel intervention.
Message passing
In this model, messages are explicitly passed between processes using system
calls like msgsnd() to send messages and msgrcv() to receive them.
This method is inherently safer than shared memory, as it avoids the need for
explicit synchronization.
Message passing is useful for exchanging smaller amounts of data, because no
conflicts need be avoided. It is also easier to implement in a distributed system
than shared memory.

©HIMANSHU NARAYAN PRASAD 9

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Example:
Create 4 process using fork()
#include <stdio.h>
#include <unistd.h> // for fork(), getpid(),getppid()
#include <sys/types.h> // for pid_t, the data type

int main() {
// Creating 4 processes using fork
for (int i = 0; i < 2; i++) {
fork(); // Creates a new process each time
}
// Print the process ID and parent process ID
// Each process (parent or child) with details
printf("Process ID: %d, Parent ID: %d\n",
getpid(), getppid());
return 0;
}
Output:

©HIMANSHU NARAYAN PRASAD 10

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Program to display parent and child process


#include <stdio.h>
#include <unistd.h>// for fork(), getpid(),getppid()
#include <sys/types.h>

int main() {
pid_t pid; // To store the process ID
pid = fork();// Create a new process using fork()
// If fork() returns 0, it's the child process
if (pid == 0) {
printf("This is the child process. PID: %d, Parent
PID: %d\n", getpid(), getppid());
}
// If fork() returns a positive value, it's the parent
else if (pid > 0) {
printf("This is the parent process. PID: %d, Child
PID: %d\n", getpid(), pid);
}
// If fork() returns a negative value, process failed
else {
printf("Failed to fork.\n");
return 1;
}
return 0;
}
Output:

©HIMANSHU NARAYAN PRASAD 11

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Socket program in Java [ from Operating System Concept Book ]

Date Server
import java.net.*;
import java.io.*;

public class DateClient {


public static void main(String[] args) {
try {
// Connect to the server on localhost, port 6013
Socket sock = new Socket("127.0.0.1", 6013);
// Create input stream to receive data from the server
BufferedReader in = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
// Read and display the date from the server
String date = in.readLine();
System.out.println("Date received from server: " +
date);
sock.close();// Close the connection
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}

©HIMANSHU NARAYAN PRASAD 12

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process

Date client
import java.net.*;
import java.io.*;
public class DateClient {
public static void main(String[] args) {
try {
// connection to server socket at localhost on port 6013
Socket sock = new Socket("127.0.0.1", 6013);
// Get input stream to read data from the socket
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new
InputStreamReader(in));
// Read and print the date from the server
String line;
while ((line = bin.readLine()) != null) {
System.out.println(line);
}
sock.close();// Close the socket connection
} catch (IOException ioe) {
System.err.println(ioe);
}
}

}
Step:
1. Start the Server:
javac DateServer.java
java DateServer

2. Run the Client:


javac DateClient.java
java DateClient

©HIMANSHU NARAYAN PRASAD 13

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition

You might also like