OS Process
OS Process
OS Process
The term “process” was first used by the operating system designers of the
MULTICS system way back in 1960s.
Sizes of the text and data sections are fixed, as their sizes do not change during program
run time.
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process
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.
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.
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:
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.
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).
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process
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.
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:
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process
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:
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System - Process
Date Server
import java.net.*;
import java.io.*;
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
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition