process concept process scheduling operations on processes ...€¦ · •process concept...

37
Chapter 3: Processes Management Process Concept Process Scheduling Operations on Processes Inter-process Communication Examples of IPC Systems Communication in Client-Server Systems Objectives:

Upload: others

Post on 15-Jun-2020

24 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Chapter 3: Processes Management

•Process Concept

•Process Scheduling

•Operations on Processes

•Inter-process Communication

•Examples of IPC Systems

•Communication in Client-Server Systems

Objectives:

Page 2: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Process Concept• Before:

• Only one program allowed to be run at a time. This program had complete control of the system and had access to all the system’s resources.

• Today:

• Multiple programs to be loaded into memory and executed concurrently.

• Execution of user programs (User Code) and system programs (system code) that is not part of the kernel.

• The CPU (CPU’s) multiplexed among these Processes.

• In a single-user system :• A user may be able to run several programs at one time: a word

processor, a Web browser, and an e-mail package.

• Even the user can execute only one program at a time, the OS have its own internal programmed activities, such as memory management.

• All these activities are called process or job.

• process execution must progress in sequential fashion

Page 3: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

The Process• A process is a program in execution and consists of

multiple parts:• Text Section: the program code that it execute.

• Program counter: the value of the current activity, and specify the next instruction to be executed.

• Processor Registers' contents

• Process stack: Contains temporary data (such as function parameters, return addresses, and local variables).

• Data section: Contains global variables.

• A heap: the dynamically allocated memory during run time.

• Program (passive entity, executable file), and becomes process ( active entity) when the executable file loaded into memory

• Execution of program started via:• GUI by mouse clicks the icon of the executable file

• Command line entry of its name, etc.

• One program can be several processes (multiple users executing the same program).

Page 4: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

The Process Status• As a process executes, it changes state

• New: The process is being created

• Running: Instructions of the process are being executed

• Waiting: The process is waiting for some event to occur (I/O Request)

• Ready: The process is waiting to be assigned to a processor

• Terminated: The process has finished execution

Diagram of Process State

Page 5: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Process Control Block (PCB)OS represent each Process by a PCB which have process Information:

• Process state: the state may be new , ready , running , waiting ,…..

• Program counter: address of the next instruction to be executed for this process

• CPU registers: The registers vary in number and type, depending on the computer architecture.

• CPU scheduling information: process priority and pointer to scheduling queues.

• Memory-management information: the value of the base and limit registers, page table.

• Accounting information: the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on.

• I/O status information: list of I/O devices allocated to the process, a list of open files, and so on.

Page 6: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Process Control Block (PCB)

Process Control Block (PCB) CPU switch from Process to process

Page 7: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Process Representation in LinuxRepresented by the C structure task_structpid_t pid; /* process identifier */

long state; /* state of the process */

unsigned int time_slice /* scheduling information */

struct task_struct *parent; /* process’s parent */

struct list_head children; /* process’s children */

struct files_struct *files; /* list of open files */

struct mm_struct *mm; /* address space of this pro */

• In Linux kernel, all active processes are stored in a doubly linked list of task_struct, and the kernel maintains a pointer —current—to the process currently executing on the system.

Page 8: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Process Scheduling• The process of multi-programming is to have more processes running

at the same time, to maximize CPU utilization, quickly switch processes.

• The objective of time sharing is to switch the CPU among processes sofrequently that users can interact with each program while it is running.

• Process scheduler selects among a list of available processes for next execution on CPU, if the was a single processor one process is running at a time and the others must be wait.

• Maintains scheduling queues of processes• Job queue – set of all created processes in the system• Ready queue – set of all processes residing in main memory, ready

and waiting to execute• Device queues – set of processes waiting for an I/O device• Processes migrate among the various queues

Page 9: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Ready Queue And Various I/O Device Queues

• The queue is stored as a linked list.

• A ready-queue header containspointers to the first and final PCBs inthe list.

• Each PCB have a pointer field to thenext PCB in the ready queue.

• When a process is allocated to theCPU, it executes for a time while it isinterrupted, or waits for I/O request(disk may be busy, The processtherefore may have to wait for thedisk ).

• The processes entered in the devicequeue.

• Each device has its own devicequeue.

Page 10: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Representation of Process Scheduling• A new process is initially

put in the ready queue.

• It waits there until it is selected for execution

• Once the process is executed, one of several events could occur:

The process have an I/O request..

The process could create a new sub-process and wait for the sub-process’s termination.

The process removed from the CPU, as a result of an interrupt, and be put back in the ready queue.

Page 11: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Schedulers• Scheduler: part of OS used to select process from scheduling queues to be executed by the CPU

in some fashion.• Long-term scheduler (or job scheduler) – when run a batch (more created processes)

• Al list of these processes are swapped to the disk , selects which processes should be brought from disk into the main memory (ready queue)

• Must Be Fast• controls the degree of multiprogramming (how many process in memory)• Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow)

• Short-term scheduler (or CPU scheduler)• selects from the processes in the ready queue (main memory), allocates the CPU to one of them.• Must Be Fast to avoid wasting of time in the process of selecting new process for execution.• If it takes 10 milliseconds to decide to execute a process for 100 milliseconds, then 10/(100 + 10) = 9

percent of the CPU is being used (wasted) simply for scheduling the work.• Invoked very frequently (milliseconds)

• Most processes can be described as either:• I/O-bound process : spends more of its time doing I/O than it spends doing computations.

• A CPU-bound process, spends more of its time doing computation than it spends doing I/O.

• Long-term scheduler must select mix-good to prevent empty ready queues and empty I/O queues.

• The• system with the best performance must have a combination of CPU-bound and I/O-bound.

Page 12: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Addition of Medium Term Scheduling• Swapping: to remove processes from memory and thus reduce the

degree of multiprogramming. Later, the process can be reentered into

memory, and its execution can be continued where it left off.

• The process is swapped out, and is later swapped in, by the medium-

term scheduler.

• Swapping may be necessary to improve the process mix good.

Page 13: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Context Switch• When an interrupt occurs the system needs to:

• Save the current context of the process running on the CPU

• Restore that context when its processing is done, (suspending/resuming).

• Context of a process represented in the PCB of the process.

• When CPU switches to another process, OS perform the following:• State save : the system must save the state of the old process

• State restore: restore the saved state for the new process via a context switch

• Context-switch time is overhead; the system does no useful work while switching

• The more complex the OS and the PCB -> longer the context switch

• Time dependent on hardware support

• Some hardware provides multiple sets of registers per CPU -> multiple contexts loaded at once

Page 14: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Operations on the Process: Process Creation• Parent process can create a new processes while it is executed (child

processes), which, in turn can create other processes (Process Tree)

• Each process is identified and managed via a process identifier (pid)

• Resource sharing Cases (CPU Time , Memory , Files ,I/O devices)• Parent and children share all resources• Children share subset of parent’s resources• Parent and child share no resources

• Execution Cases• Parent and children execute concurrently• Parent waits until children terminate

• Address space• Child duplicate of parent• Child has a program loaded into it

• UNIX examples• fork system call creates new process• exec system call used after a fork to replace the process’ memory space with a

new program

Page 15: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Operations on the Process: Process Creation Linux#include <sys/types.h>

#include <studio.h>

#include <unistd.h>

int main()

{

pid_t pid;

pid = fork(); /* fork another process */

if (pid < 0) { /* error occurred */

fprintf(stderr, "Fork Failed");

return 1;

}

else if (pid == 0) { /* child process */

execlp("/bin/ls", "ls", NULL);

}

else { /* parent process */

/* parent will wait for the child */

wait (NULL);

printf ("Child Complete");

}

return 0;

}

A Tree of Processes on Solaris

Page 16: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Operations on the Process: Process Creation Windows#include <stdio.h>

#include <windows.h>

int main(VOID)

{

STARTUPINFO si;

PROCESS_INFORMATION pi; // allocate memory

ZeroMemory(&si, sizeof(si));

si.cb = sizeof(si);

ZeroMemory(&pi, sizeof(pi));

// create child process

if (!CreateProcess(NULL, "C:\\WINDOWS\\system32\\mspaint.exe", NULL, NULL, FALSE,0,

NULL, NULL, &si, &pi)){

fprintf(stderr, "Create Process Failed");

return -1; } // parent will wait for the child to complete

WaitForSingleObject(pi.hProcess, INFINITE);

printf("Child Complete");

CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);}

Creating a separate process using the Win32 API.

Page 17: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Operations on the Process: Process Creation Windows#include <stdio.h>

#include <windows.h>

int main(VOID)

{

STARTUPINFO si;

PROCESS_INFORMATION pi; // allocate memory

ZeroMemory(&si, sizeof(si));

si.cb = sizeof(si);

ZeroMemory(&pi, sizeof(pi));

// create child process

if (!CreateProcess(NULL, "C:\\WINDOWS\\system32\\mspaint.exe", NULL, NULL, FALSE,0,

NULL, NULL, &si, &pi)){

fprintf(stderr, "Create Process Failed");

return -1; } // parent will wait for the child to complete

WaitForSingleObject(pi.hProcess, INFINITE);

printf("Child Complete");

CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);}

Creating a separate process using the Win32 API.

Page 18: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Operations on the Process: Process Creation Windows

Page 19: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Operations on the Process: Process Termination• A process terminates when it finishes executing of its final statement and

asks the OS to delete it by using the exit() system call. • Process may return a status value to its parent process (wait() system call).

• All the resources of the are de-allocated by the operating system.

• Termination can occur in other circumstances as well. • A process can terminate another process by a suitable system call

(TerminateProcess() in Win32). (Some times only parent process)• Users could arbitrarily kill each other’s jobs.

• A parent may terminate the execution of one of its children for a variety of reasons, such as these:• Child has exceeded allocated resources (the parent must have a mechanism to

inspect the state of its children.)

• The task assigned to the child is no longer required.

• The parent is exiting, and the OS does not allow a child to continue.

Page 20: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Inter Process Communication IPC• Processes executing concurrently in the OS may be:

• Independent: it cannot affect/affected by the other processes executing in the system. It does no share of data with any other process.

• Cooperating: it can affect/affected by the other processes executing in the system. It shares data with other processes is a cooperating process.

• Reasons for cooperating processes:

• Information sharing: users interested in the same piece of information.

• Computation speedup: break it into subtasks, each of which will execute in parallel with the others.

• Modularity: dividing the system functions into separate processes/threads.

• Convenience: user may work on many tasks at the same time.

• Cooperating processes need inter-process communication (IPC), Two models of IPC:

• Shared memory.• Message passing.

Page 21: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Inter Process Communication• Shared memory: a region of memory that is shared by cooperating

processes is established, exchange information by reading and writing data to the shared region.

• Message passing: communication takes place by means of messages exchanged between the cooperating processes.

Page 22: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Inter Process Communication – Shared Memory•Communicated Processes establish a region of shared memory

resides in the address space of the creating process.•Other processes that wish to communicate using this shared-

memory segment must attach it to their address space.•Recall that, normally, the OS tries to prevent 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 the data and the location are determined by these

processes and are not under the operating system’s control.• The processes are also responsible for ensuring that they are

not writing to the same location simultaneously.

Page 23: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Producer-Consumer Problem• Producer process produces information that is consumed by a consumer

process• a compiler produce assembly code that is consumed by an assembler.

• (Client-server) a web server produces HTML files and images, which areconsumed by the client web browser.

• Solution to allow producer and consumer processes to run concurrently

• We must have available a buffer of items:• that can be filled by the producer and emptied by the consumer.

• This buffer a region of shared memory between the producer and consumer.

• A producer can produce one item while the consumer is consuming another item.

• The producer and consumer must be synchronized.

• unbounded-buffer places no practical limit on the size of the buffer

• bounded-buffer assumes that there is a fixed buffer size

Page 24: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Producer-Consumer Problem – Bounded Buffer

Page 25: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Producer-Consumer Problem – Using Java

Page 26: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Inter-process Communication – Message Passing• Allow processes to communicate and to synchronize their actions Using

Messages.

• Useful in a distributed environment, the communicating processes mayreside on different computers connected by a network.

• A chat program on the WWW could be designed so that chat participantscommunicate with one another by exchanging messages.

• A message-passing facility provides at least two operations:

• send(message) – message size fixed or variable

• receive(message)

• If P and Q wish to communicate, they need to:

• establish a communication link between them

• exchange messages via send/receive

• Implementation of communication link

• physical (e.g., shared memory, hardware bus, or network)

• logical (e.g., logical properties)

Page 27: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Implementation Questions

• How are 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?

Page 28: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Direct Communication• Processes must name each other explicitly:• send (P, message) – send a message to process P• receive(Q, message) – receive a message from process Q

• Properties of communication link• Links are established automatically• A link is associated with exactly one pair of communicating

processes• Between each pair there exists exactly one link• The link may be unidirectional, but is usually bi-directional

Page 29: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

In-Direct Communication• Messages are directed and received from mailboxes (also referred to as ports)

• Each mailbox has a unique id , Processes can communicate only if they share a mailbox

• Properties of communication link• Link established only if processes share a common mailbox• A link may be associated with many processes• Each pair of processes may share several communication links• Link may be unidirectional or bi-directional

• Operations• create a new mailbox - send and receive messages through mailbox - destroy a

mailbox

• Primitives are defined as:• send(A, message) – send a message to mailbox A , • receive(A, message) – receive a message from mailbox A

• Mailbox sharing• P1, P2, and P3 share mailbox A• P1, sends; P2 and P3 receive• Who gets the message?

• Solutions• Allow a link to be associated with at most two processes• Allow only one process at a time to execute a receive operation• Allow the system to select the receiver. Sender is notified who the receiver was.

Page 30: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Synchronization• Message passing may be either blocking or non-blocking

• 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: The sending process sends the message and resumes operation.

• Non-blocking receive: The receiver retrieves either a valid message or a null.

• messages exchanged by communicating processes reside in a temporary queue. Basically, such queues can be implemented in three ways:

1. Zero capacity – 0 messagesThe queue has a maximum length of zero; thus, the link cannot have any messages waiting in it. In this case, the sender must block until the recipient receives the message.

2. Bounded capacity – finite length of n messages• The queue has finite length n; thus, at most n messages can reside in it. • If the queue is not full, the sender can continue execution without waiting. • If the link is full, the sender must block until space is available in the queue.

3. Unbounded capacity – infinite length an number of message can wait on the link, the Sender never waits

Buffering

Page 31: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Examples of IPC Systems - POSIX• POSIX Shared Memory

• Process first creates shared memory segment

segment id = shmget(IPC PRIVATE, size, S IRUSR | S IWUSR);

• Process wanting access to that shared memory must attach to it

shared memory = (char *) shmat(id, NULL, 0);

• Now the process could write to the shared memory

sprintf(shared memory, "Writing to shared memory");

• When done a process can detach the shared memory from its address space

shmdt(shared memory);

• Mach communication is message based• Even system calls are messages

• Each task gets two mailboxes at creation- Kernel and Notify

• Only three system calls needed for message transfer

msg_send(), msg_receive(), msg_rpc()

• Mailboxes needed for commuication, created via

port_allocate()

Page 32: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Examples of IPC Systems - POSIX• Message-passing centric via local procedure call (LPC) facility

• Only works between processes on the same system• Uses ports (like mailboxes) to establish and maintain communication

channels• Communication works as follows:

• The client opens a handle to the subsystem’s connection port object.• The client sends a connection request.• The server creates two private communication ports and returns the handle to one

of them to the client.• The client and server use the corresponding port handle to send messages or

callbacks and to listen for replies.

Page 33: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

1. Sockets• A socket is defined as an endpoint for communication

• Concatenation of IP address and port

• The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8

• Communication consists between a pair of sockets

Communications in Client-Server Systems

• The server waits for incoming client

requests by listening to a specified port.

• Once a request is received, the server

accepts a connection from the client

socket to complete the connection.

• Servers implementing specific services

listen to well-known ports (a telnet server

listens to port 23; an FTP server listens to

port 21; and a Web, or HTTP, server

listens to port 80).

Page 34: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Communications in Client-Server Systems2. Remote procedure call (RPC)

• Remote procedure call (RPC) between processes on networked systems

• Stubs – client-side proxy for the actual procedure on the server

• The client-side stub locates the server and marshalls the parameters

• The server-side stub receives this message, unpacks the marshalled parameters, and performs the procedure on the server

Execution of RPC

Page 35: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Communications in Client-Server Systems3. Pipes : Acts as a tube allowing two processes to communicate

• Issues• Is communication unidirectional or bidirectional?

• Must there exist a relationship (i.e. parent-child) between the communicating processes?

• Can the pipes be used over a network?

• Ordinary Pipes• allow communication in standard producer-consumer style

• Producer writes to one end (the write-end of the pipe)

• Consumer reads from the other end (the read-end of the pipe)

• Ordinary pipes are therefore unidirectional

• Require parent-child relationship between communicating processes

Page 36: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

Communications in Client-Server Systems4. 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• Provided on both UNIX and Windows systems

Page 37: Process Concept Process Scheduling Operations on Processes ...€¦ · •Process Concept •Process Scheduling •Operations on Processes •Inter-process Communication •Examples

End of Chapter 3