570 likes | 712 Views
Chapter 3: Processes. Chapter 3: Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server Systems. 3.1 Process Concept.
E N D
Chapter 3: Processes • Process Concept • Process Scheduling • Operations on Processes • Interprocess Communication • Examples of IPC Systems • Communication in Client-Server Systems
3.1 Process Concept • Process – a program in execution; a process (an active entity) is more than the program code (a passive entity, known as the text section). It also includes the current activities. • A process includes: • the value of the program counter • the contents of processor’s registers • text section • stack • data section global variables & possibly a heap
Process in Memory Function parameters, return adress, local variales Dynamically allocated memory Global variales Program code
Process Control Block (PCB) Information associated with each process • Process state (new, ready, running, waiting, terminated…) • Program counter • CPU registers (accumulators, index registers, stack pointers, …) • CPU scheduling information (process priority, pointer to scheduling queues, …) • Memory-management information (base reg., limit reg., segment tables, page tables, …) • Accounting information (CPU & real-time used, time limits, …) • I/O status information (I/O devices allocated, files opened, …) Each process is represented in the OS by a process control block (also called a task control block)
Process State • As a process executes, it changes state • new: The process is being created • running: Instructions are being executed • waiting: The process is waiting for some event (such as I/O) to occur • ready: The process is waiting to be assigned to a processor • terminated: The process has finished execution
3.2 Process Scheduling Scheduling queues • Job queue – set of all 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 Multiprogramming and time-sharing requires a process scheduler to select an available process for execution on the CPU. A uniprocessor system only one running process, others have to wait in a queue until CPU is free and be scheduled
Ready Queue And Various I/O Device Queues Ready queues P7 & P2: in ready state Device queues P3, P14, P6 & P5: in waiting state
Representation of Process Scheduling may be multiple queues for processes of different priorities dispatched wait for I/O device put back in ready queue wait for child to terminate wait for some event to occur such as an I/O completion or reception of a signal
Schedulers executes infrequently • Long-term scheduler(or job scheduler) – selects which processes should be brought into the ready queue • Short-term scheduler(or CPU scheduler) – selects which process should be executed next and allocates CPU • Medium-term scheduler (see next slide) ) – selects which swapped-out process should be brought into the ready queue executes frequently Some systems do not have a long-term scheduler (UNIX, Windows for example). Every process is put in memory for the short-term scheduler.
Addition of Medium Term Scheduling Key idea: Sometimes it is advantageous to remove processes from memory to reduce the degree of multiprogramming. (swapped out) Later, the process can be reintroduced into memory and its execution can be continued where it left off. (swapped in) This scheme is called swapping Scheduled using medium-term scheduler
Schedulers (Cont) • Short-term scheduler is invoked very frequently (milliseconds) (must be fast) • Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) • The long-term scheduler controls the degree of multiprogramming • Processes can be described as either: • I/O-bound process– spends more time doing I/O than computations, many short CPU bursts • CPU-bound process– spends more time doing computations; few very long CPU bursts
Context Switch The context of a process is represented by the PCB of the process (process descriptor) • 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 via a context switch • Context of a process represented in the PCB • Context-switch time is overhead; the system does no useful work while switching • Time dependent on hardware support (some processors have multiple set of registers. Each set can be used for a process to store its state. In this case, Context switch changing the pointer to the current register set if there are enough sets.
3.3 Process Creation • Parent process create children processes, which, in turn create other processes, forming a tree of processes • Generally, process identified and managed via a process identifier (pid) • Two possibilities in terms of execution • Parent and children execute concurrently • Parent waits until children terminate • Two possibilities in terms of the address space of the new process • The child process is a duplicate of the parent process (same program and data as the parent) • The child process has a new program loaded into it.
Process Creation (Cont) • UNIX examples • fork system call creates new process Child & parent share all open files, but do not share memory space (Child is a duplicate of the parent) • exec system call can be used after a fork to replace the process’ memory space with a new program execl, execlp, execle, execv, execvp, execve - execute a file
C Program Forking Separate Process int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else 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); } } fork() creates a new process, which consists of copy of the address space of the original process Both processes continue execution starting here fork() returns 0 for the child returns the child’s PID (> 0) for the parent To terminate the list of aguments Name of the process file execlp() loads a binary file into the memory. The child process overlays its address with the UNIX command file /bin/ls
Process Creation in Java start() returns an instance of a Process object This specifies the command Results from the command
Process Termination • Process executes last statement and asks the operating system to delete it (via exit()) • May return a status value (typically an integer) from child to parent (via wait()) • Process’ resources are deallocated by operating system • Parent may terminate execution of children processes (abort()) if • Child has exceeded allocated resources • Task assigned to child is no longer required • Parent is exiting Some OS do not allow child to continue if its parent terminates • All children terminated - cascading termination • Parent needs to know the ID of its children. This is why the ID of a newly created process is passed to the parent
Process Representation in Linux • Represented by the C structure task_structpid_tpid; /* process identifier */ longstate; /* state of the process */ unsigned int time_slice /* scheduling information */ struct task_struct*parent; /* this process’s parent */ struct list_headchildren; /* this process’s children */ /* a (doubly) linked list */ struct files_struct*files; /* list of open files */ struct mm_struct*mm; /* address space of this process */
A tree of processes on a typical Solaris 1st process running, also called swapper responsible for scheduling Root process of all user processes Responsible for networking services telnet, ftp, .. Periodically (30 seconds) checks the page Flushes modified pages to file system Controls paging out of memory to disk and back in again (replacement algorithm) Display manager, responsible for user login user login successful, a session starts A command processor You can use ps-ef to find out info. about the processes running at the time of execution
3.4 Interprocess Communication • Processes within a system may be independent or cooperating • Independent process cannot affect or be affected by the execution of another process • Cooperating process can affect or be affected by the execution of another process • Cooperating processes need interprocess communication (IPC) • Two models of IPC • Shared memory model: a region of memory is established to allow cooperating processes to read/write data to the shared region • Message passing system: communication takes place by means of messages exchanged between the cooperating processes.
Communications Models • Message passing • Shared memory • allows for maximum speed and convenience of communication • system calls required only for establishing shared-memory regions • No kernel assistance for memory accesses • Useful for exchanging smaller amount of data • Easier to implement for IPC • Implemented using system calls • Require kernel intervention
Cooperating Processes • Cooperating process can affect or be affected by the execution of another process • Advantages of process cooperation • Information sharing (eg., several users may be interested in the same piece of information) • Computation speed-up (task subtasks, executed in parallel) • Modularity (system functions separate processes or threads) • Convenience (editing, printing, compiling at one time) (piping the output of a process to the input of another process)
Producer-Consumer Problem • Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process • Two types of buffers: • unbounded-bufferplaces no practical limit on the size of the buffer (the producer can always produce new items) • bounded-buffer assumes that there is a fixed buffer size (the producer must wait if the buffer is full) • In either case, the consumer must wait if the buffer is empty)
Bounded-Buffer – Shared-Memory Solution • Shared data #define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; • Solution is correct, but can only use BUFFER_SIZE-1 elements in * * * * out Implemented as a circular array
Bounded-Buffer – Producer item nextProduced; // a local variable of Producer while (true) { /* Produce an item */ while (((in = (in + 1) % BUFFER_SIZE) == out) ; /* do nothing -- no free buffers */ buffer[in] = nextProduced; in = (in + 1) % BUFFER SIZE; }
Bounded Buffer – Consumer item nextConsumed; // a local variable of Consumer while (true) { while (in == out) ; // do nothing -- nothing to consume // remove an item from the buffer nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; // consume the item in nextConsumed }
Interprocess Communication – Message Passing • Mechanism for processes to communicate and to synchronize their actions • Message system – processes communicate with each other without resorting to shared variables (without sharing the same address space) • IPC facility provides two operations: • send(message) – message size fixed or variable • receive(message) • If P and Q wish to communicate, they need to: • establish a communicationlink between them • exchange messages via send/receive • Implementation of communication link • physical (e.g., hardware bus, shared memory, or network) • logical (e.g., logical properties) • We are concerned with only logical implementation here.
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 (logical 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 Addressing: symmetric by the O.S.
Indirect 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
Indirect Communication • Operations • create a new mailbox • send and receive messages through mailbox • destroy a mailbox • Primitives are defined as: send(B, message) – send a message to mailbox B receive(B, message) – receive a message from mailbox B
Synchronization • Message passing may be either blocking or non-blocking • Blocking (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 (Asynchronous) • Non-blocking send has the sender send the message and continue • Non-blocking receive has the receiver receive a valid message or null
Buffering • Queue of messages attached to the link; implemented in one of three ways • Zero capacity – 0 messagesSender must wait for receiver (rendezvous) to receive the message • Bounded capacity – finite length of n messagesSender must wait if link full 3.Unbounded capacity – infinite length Sender never waits
Examples of IPC Systems - POSIX POSIX: Portable Operating System Interface , an open operating interface standard • POSIX Shared Memory • To create a shared memory segment segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR); • To attach the shared memory to a processes’s address space shared_memory = (char *) shmat(segment_id, NULL, 0); • To write to the shared memory sprintf(shared_memory, "Writing to shared memory"); • To detach the shared memory from its address space shmdt(shared_memory); • To remove the shared memory segment shmctl(segment_id, IPC_RMID, NULL); identifier for the SM segment a char pointer casting, shmat returns a void pointer; not required in C pointer to a shmid_ds structure, defined in <sys/shm.h> command
Communications in Client-Server Systems • Sockets • Remote Procedure Calls • Remote Method Invocation (Java)
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 • Java example: ServerSocket s = new ServerSocket(47301); Socket s = new Socket(“127.0.0.1”, 47301); DataOutputStream dos = new DataOutputStream(s.getOutputStream()) DataInputStream dis = new DataInputStream(s.getInputStream()) dos: for writing data to the stream, dis: for reading data from the stream
Socket Communication See DateServer.java & DateClient.java
Remote Procedure Calls • Remote procedure call (RPC) abstracts procedure calls between processes on networked systems • Stubs – hide communication details • The client-side stub locates the server and marshalls the parameters transmits the message to the server • The server-side stub receives this message, unpacks the marshalled parameters, performs the procedure on the server, and pass back return values, if necessary, to the client
Marshalling Parameters unmarshalling unmarshalling marshalling marshalling Stub Client-side proxy for the remote object • Skeleton • Server-side proxy • Accepts a remote invocation from the client • Dispatch the invocation to the target method
Remote Procedure Calls - Example /* msg.x: Remote msg printing protocol in RPC language*/ program MESSAGEPROG { version PRINTMESSAGEVERS { int PRINTMESSAGE(string) = 1; } = 1; } = 0x20000001; program name version name procedure # procedure name version # Program # See http://www.cs.cf.ac.uk/Dave/C/node34.html
Remote Procedure Calls rpcgen: a compiler generates C code to implement an RPC protocol. the input to rpcgen is a C-like language called Remote Procedure Call Language % rpcgen msg.x msg.h, msg_clnt.c, msg_svc.c (a header file, a client stub, server stub) % cc rprintmsg.c msg_clnt.c -o rprintmsg –lnsl rprintmsg.c: msg_clnt.c: % cc msg_proc.c msg_svc.c -o msg_server -lnsl msg_proc.c: msg_svc.c: % For an rpcgen tutorial, see http://www.cs.cf.ac.uk/Dave/C/node34.html • -lnsl: • link the library libnsl, which includes the networking function of RPC and XDR