muhammad rafi sayed – feel free to learn€¦  · web viewthe init process is root parent for...

19
Process –a program in execution is called process. A program becomes a process(active entity) when an executable(passive) loaded in to memory. Process execution must progress in sequential fashion. Fig 1: Process in Memory A process includes: text (program) data section – which contains global variables heap – which is memory that is dynamically allocated during process run time Process stack – used to store return address and parameter processing to procedure Program counter – Which contains address of next instruction to be executed. Process state: Two state process diagram 1

Upload: others

Post on 28-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Process –a program in execution is called process. A program becomes a process(active entity) when an executable(passive) loaded in to memory. Process execution must progress in sequential fashion.

Fig 1: Process in Memory

A process includes: text (program) data section – which contains global variables heap – which is memory that is dynamically allocated during process run time Process stack – used to store return address and parameter processing to procedure Program counter – Which contains address of next instruction to be executed.

Process state:

Two state process diagram

1

Page 2: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Five state Process Diagram

As a process executes, it changes state. The state of a process is defined in part by the current activity of that process.

As a process executes, it changes state new: The process is being created running : Instructions are being executed waiting(blocked): The process is waiting for some event to occur ( waiting for I/O,

Communication reply) ready: The process is waiting to be assigned to a processor terminated: The process has finished execution

Only one process is being executed on a processor the remaining process will be either in ready state or blocked state. All the five states will be in main memory only.New ->ready: Whenever new process comes it will be kept in the reafy state.Ready ->run: scheduler picks up this processRun -> ready: time slice is completed or scheduler picks up another processRun -> terminated: whenever process completes it will be terminatedRun -> wait: Process waits for inputWait -> ready: input become available

Process Control Block:

Process Control Block (PCB) or task control block

2

Page 3: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Process Identification: Process Identification (PID) : Each process contains unique numeric identifier.Parent Process Identification (PPID) : Identifier of the process that created this process.User Identifier (UID): Every user will have unique ID, that also stored in the process identification.

Processor State Information:All general purpose registers valuesProgram counter: it address of next instruction to be fetched.Flag register and stack pointer.

Process Control Information:Process state information: The state may be ready, run, wait or terminated.CPU Scheduling Information: This information includes process priority and other scheduling information.Memory Management information: This includes the values such as value of base and limit registers, page tables or segment tables depending up on memory management.I/O status Information: The list of I/O devices allocated to the process, a list of open files and so on.

3

Page 4: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Process Scheduling:The objective of multi programming is to enhance the CPU performance. The objective of time sharing is users should interact with program (good response time). To meet the objectives, the process scheduler selects an available ready process. In a single processor system CPU can execute only one process at a time.

Scheduling Queues:A queue is generally store as a linked list. The queue header contains the pointers to first and last PCBs(Process control Block) in the list. Each PCB contains a pointer field that points to the next PCB in the queue.

Fig: Ready Queue and Various I/O queues

Ready Queue: As soon as an executable program submitted to the OS, a process will be created and it will be kept in the rear end of ready queue. The ready process is kept in main memory. The process is waiting to be assigned to a processor.

Run Queue: The scheduler selects one of the processes from ready queue and allocates the CPU, it will be in running state for the given time slice and it will be moved to ready queue after completion of time slice.

Device queues: If a process requests for I/O (Waiting for event) during its run rime, it will be kept in the respective device queue. If an event occurs it will be moved to ready queue.

4

Page 5: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Fig: Representation of Process Scheduling

Schedulers:A process migrates among the various scheduling queues throughout its life time. There are three typed of schedulers 1)Long term 2)Medium term 3)Short term.Long Term Schedulers: The long term scheduler controls degree of multiprogramming. The multi programming means that number of programs that can be loaded in to memory. The long term scheduler is invoked whenever a new program is submitted to the system or a program exits. Whenever new program submitted, it decides whether the program is to be kept in read state (memory) or ready suspended state (disk). If degree of multiprogramming reaches to maximum the new process will be kept on the disk. If one of the running process exits then long term scheduler decides which program should be bring from ready suspended to ready state.

Long term scheduler selects mix of I/O bound processes and CPU bound processes. An I/O bound process is one that spends more of its time doing I/O than it spends doing computations. CPU bound process takes less I/O time and more CPU time.

New

Ready Suspended Ready

Long Term Scheduler

Long Term Scheduler

5

Page 6: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Medium Term Scheduler:Operating systems like time-sharing systems uses medium term scheduler. Some times it can be advantageous to remove a process from memory and thus reduce the degree of multiprogramming. Later, the process can be reintroduced into memory, and its execution can be continued where it let off. The ready and wait states of processes will be in memory. The ready suspended and blocked suspended process will be on DISK. Removing a process from memory and keep it on the disk is called SWAP OUT and bringing back the process from the disk to memory is called SAP IN.

Short term scheduler or CPU scheduler:It is also known as dispatcher. It selects one of the processes from ready queue and allocates CPU to it. The short-term scheduler must select a new process frequently. It is invoked very frequently compared to long term and medium term scheduler. It is invoked whenever clock interrupt (time slice is over) or I/O interrupt or system call occurs. It must be fast. .

Process Switching or Context Switch:When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process. This is known as context switch. The context switching takes place whether the process in kernel mode or in user mode. The context switching takes place only in the kernel. The context is represented in the PCB (Process control block) of the process; it includes the value of the CPU register, the process state, and memory management information. Context-switch time is overhead; the system does no useful work while switching. So, Multi-Batch system is more efficient than time-sharing system. The context switches time dependents on hardware. The context switching time also depends upon memory utilized by the process, memory speed and operating system. The context switching takes place when clock interrupt, I/O interrupt, memory interrupt occurs or system call occurs.

BlockedSuspended

Blocked Or Wait

Ready Suspended

Ready

Medium term Scheduler

Medium term Scheduler

Ready Run

Short Term Scheduler

6

Page 7: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Fig: CPU Switch From Process to Process

Operations on Process:The process may be created and terminated dynamically. The system must provide mechanism to create and terminate process.

Process Creation:A process may create several new processes, via a create-process system call, during the course of execution. The creation process is called parent process and new processes are called children of that process. Each of these new processes may in turn create other processes, forming a tree of processes. Whenever a new process created each process is given unique process identifier called PID, which is typically an integer number. A new process is created whenever a new program is executed, new user is logged in, OS wants to provide a new service or spawned by existing process.

As soon as Solaris system is booted it creates a process called sched. It in turn creates init, pageout and fsflush processes. The pageout and fsflush processes are responsible for managing memory and file system. The init process is root parent for all user processes. The inetd is responsible for networking services such as telnet and ftp. The dtlogin creates xsession, it in turn creates sdt_shell. The sdt_shell creates csh process. It is a command line interpreter in Solaris and it takes commands from the user. For example, ps –el, displays all the process currently running on the system.

In general, a process will need certain resources (CPU time, memory, files, I/O devices) to accomplish the task. When a process creates a sub process:

that sub process may be able to obtain resources directly from operating system Or it may get subset of resources from parent.

7

Page 8: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Fig: A tree of process on typical Solaris process

When a process creates a new process: The parent continues to execute concurrently with its children The parent waits until its child process terminated

There are also two possibilities in terms of the address space of the new process: The child process is duplicate of the parent process The child process has a new program loaded into it.

In UNIX, a new process is created by fork() system call. The new process consists of a copy of the address space of the original process. An exec system call used after a fork to replace the process’ memory space with a new program.

Fig : Process Creation

#include <stdio.h>#include <sys/types.h>#include <unistd.h>main(){pid_t pid;

8

Page 9: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

/* fork another process */pid = fork();if (pid < 0) { /* error occurred */fprintf(stderr, "Fork Failed");exit(-1);}

if (pid == 0) { /* child process */execlp("/bin/ls", "ls", NULL);}else { /* parent process *//* parent will wait for the child to complete */wait (NULL);printf ("Child Complete");exit(0);}}Fig: C program forking a separate process

Assume that a parent address space is 5000 memory location to 10000. As soon as fork() is executed parent address space is copied to memory location 15000 to 20000. Now the parent is running in address space 5000 to 10000 and child is running in address space 15000 to 20000. As soon as execlp() is executed 15000 to 20000 address space is replaced with “ls” program.

Process Termination:Process terminates when it finishes executing its final statement and asks the operating system to delete it by using exit () system call. The terminating process may return status value to its parent. All the resources of the process- including physical and virtual memory, open files, and I/O buffers- are deallocated by the operating system.

Reasons for process termination:1) The process can be terminated by the owner of the process or super user using kill statement in UNIX or TermianteProcess () in Win32.2) Parent may terminate execution of children processes (abort)

Child has exceeded allocated resources Task assigned to child is no longer required

3) The parent is exiting, and operating system do not allow child to continue if its parent terminates.4) The process try to access a memory location that is not allowed to access.5) The process tries to execute a in valid instruction.6) The process attempts to use an instruction reserved for the operating system.7) The may terminate whenever an arithmetic error has occurred (divide by zero).

Inter Process Communication:Processes executing concurrently in the operating system may be either independent processes or cooperating processes. A process is cooperating if it can affect or be affected by the other processes executing in the system. Any process that does not share data with any other process is independent.

9

Page 10: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Reasons for Process cooperation are Information Sharing, computational speed up, modularity, convenience.Cooperating processes require an Interprocess Communication (IPC) mechanism that will allow them to exchange data and information. There are two fundamental models of inter process communication :(1) Shared Memory and (2) Message Passing.

Fig: Communication Models (a) Message Passing (b) Shared Memory

In the shared-Memory model, a region of memory that is shared by cooperating process is established. Process can then exchange information by reading and writing data to the shared region. In the message passing model, communication takes place by means of messages exchanged between the cooperating processes.

Shared Memory Model Message Passing1)It is useful for exchanging larger amount of data

1)It is useful for exchanging smaller amount of data.

2)read write conflict to be avoided 2)No conflicts need to be avoided3)Difficult to implement 3)Easier to implement than shared memory4)Faster than message passing 4)Slower than shared memory model5) System calls require establishing shared memory region. After that shared memory is accessed like memory only

6) Kernel intervention is required for message passing

Shared Memory Systems:Interprocess communication using shared memory requires communicating processes to establish a region of shared memory. A shared memory-segment region resides in the address space of the process creating the shared-memory segment. Other process that wishes to communicate using shared-memory segment must attach it to their address space. Normally, memory management prevents one process from accessing another process’s memory. Shared memory requires that two or more processes agree to remove this restriction. They can then exchange information by reading and writing data in the shared areas. The form of data and the location are determined by these processes and are not

10

Page 11: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

under operating system’s control. The processes are responsible for ensuring that they are not writing to the same location simultaneously.

Example for cooperative Processing is producer consumer problem.

The producer process (server) produces elements that are consumed by a consumer process (Client). One solution for producer consumer problem uses shared memory, to allow producer and consumer process to run concurrently. This buffer will reside in a region of memory that is shared by both producer and consumer processes. A producer produces one item at a time and consumer consumes one item at a time. The producer and consumer must be synchronized, so that the consumer does not try to consume an item that has not yet produced.Two types of buffers can be used. The unbounded buffer assumes infinite buffer size is available. The consumer may have to wait for new items, but the producer can produce new items. The bounded buffer assumes a fixed buffer size. In this case, the consumer must wait if the buffer is empty, and the producer must wait if the buffer is full.

#define BUFFER_SIZE 10Item buffer[BUFFER_SIZE];Int in =0;Int out = 0;

The shared buffer is implemented as a circular array with two logical pointers in and out. The variable in points to the next free position in the buffer; out points to the first full position in the buffer; The buffer is empty when in == out; the buffer is full when (( in + 1) % BUFFER_SIZE) == out.

Item nextproduced;

while(true) { /* Produce an item in next produced */ While(((in +1) % BUFFER_SIZE) == out) /* Do nothing */

Buffer[in] = nextproduced. In = (in + 1) % BUFFER_SIZE;}

Fig : Producer Process

Item nextconsumed;

while(true) { while(in == out) ; /* Do nothing */

Nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE;

11

Page 12: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

/* Consume the item in next consumed */}Fig : Consumer Process

Message-Passing Systems:Message passing provides a mechanism to allow a process to communicate and to synchronize their actions without sharing the same address space. It is particularly useful in a distributed environment, where the communicating processes may reside on different computers connected by network. For example, a chat program on the World Wide Web.It provides at least two operations: send and receive. Message sent by a process can be of either fixed or variable size. If only fixed-sized message can be sent, the system-level implementation is forward. If processes P and Q want to communicate, they must send messages to and receive from each other; a communication link must exist between them. Here are several methods for logically implementing a link and the send() / receive() operations:

Direct or indirect communication Synchronous or asynchronous Automatic or explicit buffering

Naming: Processes that want to communicate must have a way to refer to each other. They can use either direct or indirect communication.

Under direct communication, each process that wants to communicate must explicitly name the recipient or sender of the communication.Send() and receive() primitives:

Send(P,message) – Send a message to process P. Receive(Q,message) – Receive a message from process Q.

A communication link in this scheme has the following properties:

A link is established automatically between every pair of processes that want to communicate. The processes need to know only each other’s identity to communicate.

A link is associated with exactly two processes Between each pair of processes, there exists exactly one link. The link may be unidirectional, but is usually bi-directional.

This scheme exhibits symmetry in addressing; i.e, both the sender process and the receiver process must name the other to communicate.

In asymmetry in addressing, only sender names the recipient; the recipient is not required to name the sender. The send() and receive() primitives are defined as follows:

Send(P, message) – Send a message to process P. Receive(id, message) – Receive a message from any process; the variable id is set

to the name of the process with which communication has taken place.The disadvantage in both of these schemes (Symmetric and asymmetric) is the limited modularity of the resulting process definitions. Changing the identifier of a process may necessitate examining all other processes definitions.

12

Page 13: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Indirect Communication: The messages are sent to and received from mailboxes or ports. A mailbox can be viewed abstractly as an object into which messages can be placed by processes and from which messages can be removed. Each mail box has a unique identification. Two processes can communicate only if the processes have a shared mailbox.

Send() and receive() primitives are defined as follows: Send(A, message) - Send a message to mailbox A. Receive(A, Message) – Receive a message from mailbox A.

A communication link has following Properties A Link is established between a pair of processes only if both members of the pair have a shred mail box.

A link may be associated with more than two processes Each pair of processes may share several communication links Link may be unidirectional or bi-directionalA mailbox may be owned either by a process or by the operating system. If the mail box is owned by a process, then we distinguish between the user(Who can send a message) and owner( who receives message through mail box). When a process that owns a mail box terminates, the mail box disappears. Any process that subsequently sends a message to this mail box must be notified that the mail box no longer exists.

A mail box that is owned by the operating system has an existence of its own. It is independent and is not attached to any particular process. The OS then must provide a mechanism that allows a process to do the following:

Create a new mailbox Send and receive messages through mailbox Destroy a mailbox

The process that creates a new mailbox is that mailbox’s owner by default. Initially, the owner is the only process that can receive a message through this mailbox.

Synchronization:Communication between processes takes place through calls to send() and receive() primitives. There are different design options for implementing each primitive. Message passing may be either blocking or nonblocking also known as Synchronous and Asynchronous.

Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available

Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null

When both send() and receive() are blocking, we have rendezvous between sender and receiver.

Buffering:

13

Page 14: Muhammad Rafi Sayed – Feel Free To Learn€¦  · Web viewThe init process is root parent for all user processes. The inetd is responsible for networking services such as telnet

Whether communication is direct or indirect, messages exchanged by communicating processes reside in a temporary queue. Such queues can be implemented by three ways. Zero capacity –0 messages (No buffering) Sender must wait until the recipient receives the message (rendezvous) Bounded capacity –finite length of n messages ( n messages can reside on it) Sender must wait if the queue is full

If the queue is not full, when a new message is sent, the message is placed in the queue and sender can continue the execution without waiting.

Unbounded capacity The queue length is infinite; thus, any number of messages can wait in it. The sender never blocks.

Differences between Program and process

Program ProcessSequence of instructions is called an executable program.

The program in running is called a process

It is a static entity It is a active entitySingle instance is available Each running instance of a program is a

Separate process. Notepad is one program and can be opened twice

Stored in secondary storage devices Stored in main memoryProgram is a static entiry Process will under go several states during

it’s execution

14