Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interprocess Communication

Similar presentations


Presentation on theme: "Interprocess Communication"— Presentation transcript:

1 Interprocess Communication

2 Interprocess Communication :
Spawning a new process, parent and child processes, assigning a task to child processes, need for communication between processes, modes of communication, pipes, shared files, shared memory, message based IPC, signals as IPC, The distributed computing environment.

3 Multithreading Multithreading refers to the ability of an OS to support multiple, concurrent paths of execution within a single process

4 Single and Multithreaded Models

5 Multithreading In a multithreaded environment, a process is defined as the unit of resource allocation and a unit of protection. all of the threads of a process share the state and resources of that process. They reside in the same address space and have access to the same data. As with processes, the key states for a thread are Running, Ready, and Blocked. There are four basic thread operations associated with a change in thread state

6 Thread States • Spawn: Typically, when a new process is spawned, a thread for that process is also spawned. Subsequently, a thread within a process may spawn another thread within the same process, providing an instruction pointer and arguments for the new thread. The new thread is provided with its own register context and stack space and placed on the ready queue. • Block: When a thread needs to wait for an event, it will block (saving its user registers, program counter, and stack pointers).The processor may now turn to the execution of another ready thread in the same or a different process. • Unblock: When the event for which a thread is blocked occurs, the thread is moved to the Ready queue. • Finish: When a thread completes, its register context and stacks are deallocated

7 User-Level and Kernel-Level Threads
There are two broad categories of thread implementation: user- level threads (ULTs) and kernel-level threads (KLTs). In a ULT facility, all of the work of thread management is done by the application and the kernel is not aware of the existence of threads. In a pure KLT facility, all of the work of thread management is done by the kernel. There is no thread management code in the application level, simply an application programming interface (API) to the kernel thread facility. Windows is an example of this approach.

8 User and Kernel threads

9 Spawning A New Process Spawn refers to a function that loads and Executes a new child process. The current process may or may not continue to execute asynchronously. Creating a new sub process requires enough memory in which both the child process and the current program can execute. Fork-exec is a commonly used technique in Unix whereby an executing process spawns a new program. Fork() is the name of the System call that the Parent process uses to "divide" itself ("fork" into two identical processes). After calling fork(), the created Child process is an exact copy of the parent except for the return value. This is probably of limited use, so the child usually transfers to another program using the system call exec().

10 The fork system call spawns new process which is a copy of parent process from where it was invoked
A newly created process inherits its parents execution environment

11 Parent And Child Process
The process that invoked fork is the parent process and the newly-created process is the child process. Every process (except process 0) has one parent process, but can have many child processes. A parent will typically retrieve its child's exit status by calling the wait System call. However, if a parent does not do so, the child process becomes a Zombie process.

12 Assigning a task to child processes
The exec command comes in several flavors.its used to assign task to child We can choose exec command to execute an identified executable defined using relative or absolute path. It may be executed with or without inherited executed environment. Various form of exec System calls are as follows

13 The pid and uid of child process are carried over to implanted process and data/code segment obtain new info through it. Usually child process inherits open file descriptor from parent and implanted process have restriction based on file access control

14 need for communication between processes

15 Mode of communication between processes
1)Pipe 2)Shared Files/Shared Pointers 3)Using Memory Locations 4)Message based Communication 5)Using Signaling as a IPC

16 Pipes Pipes as like used in ls|more direct the output stream of one process to feed the input of another process. So for IPC we need to create Pipe and identify the direction of feeding the pipe.

17 Pipe as a Interprocess Communication
The basic scheme has three parts Spawn a new process, Populate it and wait for synchronization. AS a first step we need to identify 2 executables that need to communicate Consider a case one process get character string as input and other to reverse it then we have 2 processes Then we define Pipe to connect between process and facillate the communication. i.e. one process get input String and write into pipe and other get its input to reverse String from pipe. One can write input from input end and read from output end

18 Pipe as a Interprocess Communication
A pipe Descriptor has an array that stores 2 pointers one for input end & other for output end. Here pp[0] Stores the write end address and pp[1] stores the read end address Essentially for Communication process A Should keep write end open and close read end While Process B should keep Read end open and Write end close

19 1) First we have parent process which declares a pipe in it.
2)Next we spawn 2 child processes. Both would get pipe definition which we have defined I parent. read and write is open at this time for child as well as for parent. 3)One child process say process A close its read end and for process B close its write end. 4) Parent process closes its read and write end. 5)Process A is populated with code to get string & B is populated with Reverse of String

20 Shared files One Very commonly employed strategy for IPC is to share file One process identified as writer process write into file & another identified as reader process read from file. The writer process may continually make changes in a file & the other may read these changes as these happen. Unlike other IPC method doesn’t require special system calls can use standard system calls fork(),execlp(). No other system calls required.

21 Shared files Cons If reader is faster than the writer then this method shall have an error. Similarly if writer is continuously busy in writing then file may grow to unbounded length Both this situation result in errors. This problem of speed of reader and writer is call Reader Writer Problem & can be solved by using Mutual Exclusion of resource sharing.

22 Shared file Pointer Another way to handle files would be to share file pointers instead of file Sharing File pointer with mutual exclusion could be easily done using semaphores Shared file pointer method operates in two steps 1) one process position a file pointer at location in the file. 2)Another process read from this file from the communicated location. If reader attempt to read a file even before the writer has written something on a file we shall have an error So we ensures that readers process sleep for a while so that the writer has written some byte in to the file

23 Shared file Pointer We shall use semaphore to achieve mutual exclusion of access to the file pointer and hence to the file This method can use when two processes are related because shared file pointer must be available to both.

24 Shared Memory Communication (Using memory Locations)
We have one process write into a Memory locations and expect the other process to read from it In this case the memory location is a shared Memory locations. Memory mgt system ensures that every process has a well defined code and data area. For shared memory Communications one process would write into certain commonly accessed area and another process would read subsequently from that area The shared memory method can allow access to a common data area even amongst the processes that are not related. Its important that the shared data integrity may get compromised when an arbitrary sequence of read and write occurs

25 Shared Memory has Following steps of execution
First we have to setup shared memory mechanism in kernel Next identified “safe area” is attached to each of the process. Use this attached shared data space in a consistent manner When finished detach the shared data space from all processes to which it was attached. Delete the information concerning the shared memory from the kernel.

26 Message based IPC communication
In this mechanism one may send a message to another process The receiving process may interpret the message Usually message used to communicate an event. Messages are very general form of communication & can be used to send receive formatted data stream between arbitrary processes. They have type which helps in message interpretation and type may specify appropriate permission for process. Usually at receiver end messages are put in a queue. Messages may also be formatted in their structure and determined by application process.

27 Message based IPC communication
msgget(),msgctl(),msgsnd() sends message using message queue msgrcv()receives a message using message queue Are system calls achieves message transfer msgget() system call have key and flag returns qid i.e. id of queue which is index to kernels message queue data structure table & call returns -1 for error. It is important to have Readers and writers of a message identify the relevant queue for message exchange this is done by associating & using the correct key or qid. Key can be kept relatively private between processes by using makekey() function and can be used for encryption.

28 Message based IPC communication
For simple application its sufficient to use pid of creator process Usually the kernel uses some algorithm to translate the key into qid. The access permissions for IPC are stored in IPC permission structure which is simple table. Entries in kernels message queue are like c structure only resembles tables which have several fields like size of queue, permissions & other information's..

29 Signal as IPC Signal is a mechanism used to communicate asynchronous events. Every time we used to press ^c to abort program similarly unexpected value of pointer may generate segmentation fault or when we resize windows signal is generated. Note that in all examples event happen within the process or process receives event as input. in general process may send a signal to another process and needs respond. A process may expect certain type of signal and make provision of defining set of signal handler.

30 Sources of signal during process execution
From terminal -: consider process is running from terminal and if character ^c is pressed from keyboard it generate a signal & disconnect terminal. From window manager -: Like mouse activity used for resizing. From other subsystems -: This may be from memory or other subsystem. i.e. if memory reference is out of process’s code or data it generate signal for segmentation fault. From kernel -:usage of time in processes can be used to set an alarm used by kernel From processes -:killing of child process or jobwhich may have entered in infinite loop.

31 Process may respond to given signal in foll.ways
Ignore it -: A process may Ignore some kind of signals. Respond it -: Response is encoded for signal handler Reconfigure -: Dynamically reconfigure system services Turn on/off option -: use on /off for signal regeneration Timer information -: used by real time system for time based services. Singnal(sigxxx, sighandler)

32 Thank You


Download ppt "Interprocess Communication"

Similar presentations


Ads by Google