Chapter 3: Processes
Operating System Concepts with Java – 7 th Edition,
Prof. dr. Hanafy M. Ismail
Objectives
To introduce the notion of a process – a program in execution, which forms the basis of all computation. To describe the various features of processes, including scheduling, creation, termination, and communication.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.2
Chapter 3: Processes
Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.3
Process Concept
An operating system executes a variety of programs: Batch system executes jobs Time-shared systems executes user programs or tasks Textbook uses the terms job and process almost interchangeably Process – a program in execution; process execution must progress in sequential fashion A process includes: text section which contains the program code program counter and the contents of processor’s registers to represent the current activity stack which contains temporary data ( such as function parameters, return address, and local variables) data section which contains global variables heap which is memory dynamically allocated during process run time
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.4
Process in Memory
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.5
Program versus process
A program is a passive entity, such as a file containing a list of instructions stored on disk (an executable file). A process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources. A program becomes a process when an executable file is loaded into memory Although two processes may be associated with the same program, they are considered two separate execution sequences. For example a user may invoke many copies of the web browser program, each of these is a separate process (data, heap, and stack sections vary)
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.6
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 to occur (such as I/O completion or reception of a signal) ready: The process is waiting to be assigned to a processor terminated: The process has finished execution
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.7
Diagram of Process State
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.8
Process Control Block (PCB) Each process is represented in the OS by a process control block (PCB). It contains Information associated with a specific process, including these: Process state (new, ready, running, waiting, halted) Program counter. The address of the next instruction to be executed for this process. CPU registers. They include accumulators, index registers, stack pointers, and general-purpose registers. CPU scheduling information. It includes a process priority pointers to scheduling queues and any other scheduling parameters. Memory-management information. It includes memory limits and other information to be discussed later. Accounting information. It includes the amount of CPU and real time used, time limits, account numbers, job or process numbers. I/O status information. It includes the list of I/O devices allocated to the process, a list of open files. Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.9
Process Control Block (PCB)
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.10
CPU Switch From Process to Process
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.11
Process scheduling
The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization. The objective of time sharing is to switch the CPU among processes so frequently that users can interact with each program while it is running. To meet these objectives, the process scheduler selects an available process for program execution on the CPU. For a single-processor system, there will never more than one running process.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.12
Process Scheduling Queues
Job queue – set of all processes in the system; as a process enter the system, it is put into the job queue. Ready queue – set of all processes residing in main memory, ready and waiting to execute. It is stored as a linked list. A ready-queue header contains pointers to the first and final PCBs in the list. Each PCB includes a pointer field that points to the next PCB in the ready queue.
Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.13
Ready Queue And Various I/O Device Queues
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.14
Queueing-diagram Representation of Process Scheduling
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.15
Queueing-diagram Representation of Process Scheduling
Each rectangular box represents a queue. The circles represent the resources that serve the queues. The arrows indicate the flow of processes in the system. A new process is initially put into the ready queue. Once the process is allocated the CPU and is executing, one of several events could occur: The process could issue an I/O request and then be placed in an I/O queue. The process could create a new subprocess and wait for its termination. The process could be removed forcibly from the CPU; as a result of an interrupt, and put back in the ready queue. A process continues this cycles until it terminates, at which time it is removed from all queues and has its PCB and resources deallocated.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.16
Schedulers
In a batch system, more processes are submitted than can be executed immediately . These processes are spooled to a massstorage device (typically a disk), where they are kept for later execution. Long-term scheduler (or job scheduler) – selects processes from this pool and loads into main memory for execution (selects which processes brought into the ready queue).
Short-term scheduler (or CPU scheduler) – selects which ready process should be executed next and allocates CPU to it.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.17
Addition of Medium Term Scheduling
Some operating systems (such as time-sharing systems) introduces medium-term scheduler: It removes processes from the main memory and thus reduces the degree of multiprogramming. Later, the process can be reintroduced into memory, and its execution is continued where it left off. This scheme is called swapping
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.18
Addition of Medium Term Scheduling (cont.)
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.19
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
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.20
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 Context-switch time is overhead; the system does no useful work while switching Time dependent on hardware support
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.21
Process creation
A process may create several new processes, via a create-process system call, during the course of execution. The creating process is called a parent process and the new processes are called the children of that process. Each of these new processes may create other processes, form a tree of processes. Most operating systems identify processes according to a unique process identifier (or pid). A typical process tree for the Solaris operating system, showing the name of each process and its pid. (see next slide)
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.22
A tree of processes on a typical Solaris
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.23
Process Creation (cont.)
Parent process create children processes, which, in turn create other processes, forming a tree of processes In terms of resource sharing, three possibilities exist: Parent and children share all resources (CPU time, memory, files, I/O devices) Children share subset of parent’s resources (such as memory or files) Parent and child share no resources; a child obtains its resources directly from the operating system.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.24
Process Creation (Cont.)
In terms of execution, two possibilities exist: Parent and children execute concurrently Parent waits until some or all of its children terminate. In terms of address space, two possibilities exist: Child process is a duplicate of the parent process (it has the same program and data as the parent). Child has a new program loaded into it
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.25
Process Creation (Cont.)
To illustrate these differences, let us consider UNIX operating system: each process is identified by its process identifier (integer number). fork() system call creates new process. The new process consists of a copy of the address space of the original process. Both process continue execution at the instruction after the fork(). The return code for the fork() is zero for child, whereas the (nonzero) process identifier of the child is returned to the parent. exec() system call used after a fork to replace the process’ memory space with a new program (loads a binary file into memory) (destroying the memory image of the program containing the exec system call). The parent can issue a wait() system call to move itself off the ready queue until the termination of the child.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.26
Process Creation
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.27
Process Creation in POSIX (C program illustrates the UNIX system calls)
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.28
C Program Forking Separate Process
Two different processes running a copy of the same program. The value of pid for the child process is zero and for the parent is an integer greater than zero. execlp() is a version of the exec() system call to overlay its address space. When the child process completes, the parent process resumes from the call to wait(). It completes using the exit() system call.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.29
Process Creation in Win32
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.30
Process Termination
A process terminates when it finishes executing its last statement and asks the operating system to delete it by using the exit() system call. At that point: The process return a status value to its parent (via wait() system call)) Process’ resources are deallocated by operating system Parent may terminate execution of children processes (abort) (TerminateProcess() system call is used in Win32) for a variety of reasons: Child has exceeded allocated resources Task assigned to child is no longer required If parent is exiting Some operating system do not allow child to continue if its parent terminates All children terminated - cascading termination
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.31
Interprocess Communication
Processes executing concurrently in the operating systems may be either independent or cooperating processes: 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 Advantages of process cooperation Information sharing : to allow concurrent access to the same piece of information Computation speed-up: break the task into subtasks, each of which will be executing in parallel. ( if the computer has multiple processing elements) Modularity: dividing the system functions into separate processes or threads. Convenience. A user may work on many tasks at a time (editing, printing, compiling in parallel.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.32
Interprocess Communication
Cooperating processes require an interprocess communication (IPC) mechanism that will allow them to exchange data and information. There are two fundamental models of IPC: 1. Shared memory 2. Message passing
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.33
Interprocess Communication (cont.)
In shared memory model, a region of memory that is shared by cooperating processes is established. Processes can then exchange information by reading and writing data to the region. Advantages: It is faster than message passing: system calls are required only to establish shared-memory regions, all other access are routine memory access. Message passing is implemented using system calls; such require more time-consuming of kernel intervention.
In the message passing model, communication take place by means of message exchanged between the cooperating processes. Advantages: Useful for exchanging smaller amounts of data Easier to implement
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.34
Interprocess Communication Message Passing
Ref: Operating System Concepts with Java – 7th Edition
Shared Memory
Prof. Dr. Hanafy Ismail
3.35
Shared-memory systems
A shared-memory region resides in the address space of the process creating the shared-memory segment. Other processes that wish to communicate using the shared-memory must attach it to their address space. The form of the data and the location are determined by the processes. The processes are also responsible not writing to the same location simultaneously. The concept of cooperating processes is illustrated using the producer-consumer problem.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.36
Producer-Consumer Problem
Paradigm for cooperating processes producer process produces information that is consumed by a consumer process. For example, a compiler may produce assembly code, which is consumed by an assembler. The assembler may produce object modules which are consumed by the loader. In a client-server paradigm, consider the sever as a producer and the client as a consumer. For example, a web server produces HTML files and images which are consumed (read) by the client web browser requesting the resource
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.37
Producer-Consumer Problem
One solution to the producer-consumer problem uses sharedmemory. To allow producer and consumer processes to run concurrently. A buffer will reside in a region of memory that is shared by the producer and consumer processes: A producer can produce one item while the consumer is consuming another item. The producer and consumer must be synchronized. Two types of buffers can be used: unbounded-buffer places no practical limit on the size of the buffer. The consumer may have to wait for new items, but the producer can always produce new items. bounded-buffer assumes that there is a fixed buffer size. The consumer must wait if the buffer is empty, and the producer must wait if the buffer is full.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.38
Bounded-Buffer – Shared-Memory Solution
The following variable reside in a region of memory shared by the producer and consumer: #define BUFFER_SIZE 10 typedef struct { ... } item; item 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. The variable 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) Solution is correct, but can only use BUFFER_SIZE-1 elements
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.39
Bounded-Buffer – Insert() Method producer process item item; while (true) { /* Produce an item */ while (((in = (in + 1) % BUFFER SIZE count) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; } Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.40
Bounded Buffer – Remove() Method consumer process item
item;
while (true) { while (in == out) ; // do nothing -nothing to consume
// remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; Ref: Operating System Concepts Prof. Dr. Hanafy Ismail } with Java – 7th Edition
3.41
Message Passing
Message system – processes communicate with each other without sharing the same address space. It is useful in a distributed environment : The communicating processes may reside on different computers connected by a network. For example the chat program used in the WWW can be designed so that the participants communicate by exchanging messages. Message passing facility provides two operations: send(message) – message size fixed or variable receive(message)
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.42
Message Passing (cont.)
If processes 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) logical (e.g., logical properties). There are several methods for implementing a communication link and the send()/receive() operations: Direct or indirect communication Synchronous or asynchronous buffering Automatic or explicit buffering
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.43
Direct Communication
Under direct communication, each process that wants to communicate must explicitly name the recipient or sender of the communication: send (P, message) – send a message to process P receive(Q, message) – receive a message from process Q Properties of this communication link Links are established automatically between every pair of processes that wants 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 there exists exactly one link The link may be unidirectional, but is usually bi-directional
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.44
Direct Communication (cont.)
A variant of this scheme employs asymmetry in addressing: send (P, message) – send a message to process P receive (id, message) – receive a message from any process; id is set to the name of the process with which communication has take place. The disadvantage in both schemes (symmetric and asymmetric) is the limited modularity of the resulting process definition. Changing the identifier of a process may necessitate examining all other process definitions; all references to the old identifier must be modified.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.45
Indirect Communication
Messages are sent to and received from mailboxes (also referred to as ports) A mailbox can be viewed abstractly as an object into which messages can be placed and from which messages can be removed. Each mailbox has a unique id. POSIX message queues uses an integer value to identify a mail box. A process can communicate with other process via a number of different mailboxes. Processes can communicate only if they share a mailbox
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.46
Indirect Communication (cont.)
The 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 Properties of this 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, each corresponding to one mailbox Link may be unidirectional or bi-directional
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.47
Indirect Communication (cont.)
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 arbitrarily the receiver. Sender is notified who the receiver was.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.48
Indirect Communication
A mailbox may be owned either by a process or by the operating system. If the mailbox is owned by a process, then each mailbox has a unique owner and no confusion about who should receive a message sent by a user to this mailbox. When the process terminates, the mailbox disappears and any process sends a message to this mail box must be notified that the mailbox is no longer exist.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.49
Indirect Communication
If the mailbox is owned by the operating system, the mailbox has an independent existence and not attached to any process. The operating system must allow a process to do the following operations: create a new mailbox the creator process is the owner of the mailbox. The ownership and receiving privilege may be passed to other processes through appropriate system calls; this could results in multiple receivers for each mailbox. send and receive messages through mailbox delete a mailbox
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.50
Synchronization
Message passing may be either blocking or non-blocking Blocking is considered synchronous Blocking send : the sender process is blocked until the message is received by the receiving process or by the mailbox. Blocking receive : the receiver blocks until a message is available Non-blocking is considered asynchronous Non-blocking send : the sending process sends the message and continue operation. Non-blocking receive : the receiver receives a valid message or null The solution to the producer and consumer problem becomes trivial when we use blocking send() and receive() statements. The producer invokes the send() call and waits until the message is delivered to the receiver or the mailbox. The consumer invokes the blocking receive() , it blocks until a message is available.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.51
Buffering
Whether communication is direct or indirect, messages exchanged by communicating processes reside on a temporary queue. Such queues can be implemented in one of three ways 1. Zero capacity – the queue has a maximum length of 0. the link cannot have any messages waiting in it. The sender must block until the receiver receives the message (sender must wait for receiver) 2. Bounded capacity – the queue has a finite length of n messages. If the queue is not full when a message is sent, the message is placed in queue (or a pointer to it), and the sender continue execution. Sender must wait if link full until space is available. 3. Unbounded capacity – infinite length Sender never waits
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.52
Examples of IPC systems: POSIX API Shared memory
A process must first create a shared memory segment using shmget() system call: segment_id = shmget (IPC_PRIVATE, size, S_IRUSR |S_IWUSR);
The first parameter is the identifier of the shared memory segment. IPC_PRIVATE means new shared memory is created Size: the size of shared memory in bytes The third parameter identifies the mode (S_IRUSR (reading ), S_IWUSR (writing). segment_id return value represents an identifier for the shared memory segment.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.53
POSIX API Shared memory
Processes wants to access a shared memory segment must attach it to their address space using shmat() shared memory attach system call shared_memory = (char *) shmat (id, NULL, 0); NULL means the OS selects the location of where the shared memory will be attached 0 allows read and write to the shared region Returns a pointer to beginning location of where the shared memory will be attached We could write to the shared-memory region as follows: sprintf (shared-memory, “ writing to shared memory”); To detach the region of shared-memory: use the system call shmdt (shared-memory); To remove the shared memory segment: use the system call shmct1(id, IPC_RMID, NULL);
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.54
POSIX API Shared memory
#include <stdio.h> #include <sys/shm.h> #include <sys/stat.h> int main() { int segment_id; char* shared-memory; const int size = 4096; /* allocate a shared memory segment */
segment_id = shmget (IPC_PRIVATE, size, S_IRUSR |S_IWUSR);
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.55
POSIX API Shared memory
/* attach the shared memory segment */ shared_memory = (char *) shmat (id, NULL, 0);
/* writea message to the shared-memory segment */: sprintf (shared-memory, “ writing to shared memory”); /* print out the string from the shared memory */ printf ( “ * %s\n”, shared_memory); /* detach the region of shared-memory: use the system call */ shmdt (shared-memory); /* remove the shared memory segment: use the system call */ shmct1(id, IPC_RMID, NULL); return 0; }
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.56
Message Passing in Windows XP
In windows XP, application programs communicate via a message-passing mechanism. The message passing facility in Windows XP is called the local procedure call (LPC) facility. It communicates between two processes on the same machine. A port object is used to establish and maintain a connection between two processes. Windows XP uses two types of ports: connection ports and communication ports.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.57
Message Passing in Windows XP (cont.)
Connection ports are named objects and are visible to all processes; they give applications a way to set up communication channels. The connection 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 the server use the corresponding port handle to send messages or callbacks and to listen for replies.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.58
Message Passing in Windows XP
Ref: Operating System Concepts with Java â&#x20AC;&#x201C; 7th Edition
Prof. Dr. Hanafy Ismail
3.59
Exercises 1. 2. 3. 4. 5.
Describe the actions taken by a kernel to context-switch between processes. Distinguish between a process and a program. Describe the sections of a process in memory. Describe the states of a process? Sketch a diagram of process state transitions. There are two fundamental models of Inter Process Communication (IPC). State the two models and illustrate the difference between them. Describe the differences among short-term, medium-term, and long-term scheduling. Sketch a queueing-diagram representation of process scheduling. Using the following program, explain what will be output at Line A. int value = 5; int main() { pid_t pid; pid = fork(); if (pid == 0) { /* child process */ value += 15;; } else if (pid > 0) { /* parent process */ wait (NULL); cout << “ parent value = “ << value; // Line A exit(0); }
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.60
Exercise (PROBLEM 3.6 IN BOOK)
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 Write a C program using the fork() system call that that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.61
Exercises (problem 3.7 in book) ď °
Repeat the preceding exercise, this time using the CreateProcess() in the Win32 API. In this instance, you will need to specify a separate program to be invoked from CreateProcess(). It is this separate program that will run as a child process outputting the Fibonacci sequence. Perform necessary error checking to ensure that a non-negative number is passed on the command line.
Ref: Operating System Concepts with Java â&#x20AC;&#x201C; 7th Edition
Prof. Dr. Hanafy Ismail
3.62
Exercise (problem 3.10 in book)
In Exercise 3.6, the child process must output the Fibonacci sequence, since the parent and child have their own copies of the data. Another approach to designing this program is to establish a shared-memory segment between the parent and child processes. This technique allows the child to write the contents of the Fibonacci sequence to the shared memory segment and has the parent output the sequence when the child completes. Because the memory is shared, any changes the child makes to the shared memory will be reflected in the parent process as well. This program will be structured using POSIX shared memory.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.63
Exercise (problem 3.10 in book)
The program first requires creating the data structure for the shared-memory segment. This is most easily accomplished using a struct. This data structure will contain two items: (1) a fixed-sized array of size MAX SEQUENCE that will hold the Fibonacci values; and (2) the size of the sequence the child process is to generate sequence size where sequence size ≤ MAX SEQUENCE. These items can be represented in a struct as follows:
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.64
Exercise (problem 3.10 in book)
#define MAX SEQUENCE 10 typedef struct { long fib sequence[MAX SEQUENCE]; int sequence size; } shared data; The parent process will progress through the following steps: a. Accept the parameter passed on the command line and perform error checking to ensure that the parameter is ≤ MAX SEQUENCE. b. Create a shared-memory segment of size shared data. c. Attach the shared-memory segment to its address space. d. Set the value of sequence size to the parameter on the command line. e. Fork the child process and invoke the wait() system call to wait for the child to finish. f. Output the value of the Fibonacci sequence in the shared-memory segment. g. Detach and remove the shared-memory segment.
Ref: Operating System Concepts with Java – 7th Edition
Prof. Dr. Hanafy Ismail
3.65
Exercise (problem 3.10 in book) ď °
Because the child process is a copy of the parent, the shared-memory region will be attached to the childâ&#x20AC;&#x2122;s address space as well. The child process will then write the Fibonacci sequence to shared memory and finally will detach the segment. One issue of concern with cooperating processes involves synchronization issues. In this exercise, the parent and child processes must be synchronized so that the parent does not output the Fibonacci sequence until the child finishes generating the sequence. These two processes will be synchronized using the wait() system call; the parent process will invoke wait(), which will cause it to be suspended until the childprocess exits.
Ref: Operating System Concepts with Java â&#x20AC;&#x201C; 7th Edition
Prof. Dr. Hanafy Ismail
3.66
End of Chapter 3
Operating System Concepts with Java â&#x20AC;&#x201C; 7 th Edition,
Prof. dr. Hanafy M. Ismail