Top Banner
IPC Programming
25

IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*

Dec 13, 2015

Download

Documents

Paula McDaniel
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

IPC Programming

Page 2: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Process Model

• Processes can be organized into a parent-child hierarchy.

• Consider the following example code:

/*-------------------------------------------------------------*//* Ex1.c - Infinite loop. Compile using: cc -o forever Ex1.c *//*-------------------------------------------------------------*/#define TRUE 1

main(){ while(TRUE) /* do nothing */ ;}

Page 3: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Process Model• Compile using: cc -o forever Ex1.c• Execute from the Korn shell (ksh) using: forever &• Then, the parent process (ksh) creates a child process

(forever):

(ksh) | V (forever)

• This is the process model. In UNIX, a child is created by issuing a fork() system call.

Page 4: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Typical Uses of Fork

• A server forks off a slave process to do the work so that it can continue to process new requests. (Example: inetd and ftp)

(ftp) --.1.---> (inetd) <---.4.--- (ftp)

| |

| 2.

| |

| V

+---.3.------- (ftpd)

Page 5: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Typical Uses of Fork• As shown above:

1.An ftp request is sent to the server (inetd). 2.The server forks off a child process to do the work

(ftpd). 3.All subsequent communication is between the

slave (ftpd) and the client (ftp). 4.Another client (ftp) can make a request and that

request can be processed at the same time as the ongoing request.

• A process forks off a child to execute a command so that the process can return to its original state after the command is executed. (Example: shell programming (Ex4.c)).

Page 6: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Interprocess Communication

1. Pipes - provide a one-way flow of data. (A notable exception is SysVr4 which provides a two-way flow of data.).

2. Signals - provide a method to interrupt processes based on an external event, such as a timeout.

3. Shared Memory - provides shared data.4. Semaphores - provides a way to synchronize

events and process execution.

Page 7: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Signals• To provide reliable network communication, we must have

the ability to timeout and retransmit messages that get lost. To implement timeouts, signals can be used. Signals can be sent using kill(), either from the command-line (see kill(1)), or from within a process (see kill(2)).

• To see the relevant manual entries, type: man 1 kill, or man 2 kill.

• To print the entries: man 2 kill | col -b | lpr –P P1• etc./*-------------------------------------------------------------*//* Ex2.c - Process blocks for 60 seconds. *//*-------------------------------------------------------------*/main(){

printf("Sleeping for 1 minute \n");

sleep(60);

printf("All done \n");}

Page 8: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Signals• To simulate the action of Ex2.c, we can use a signal

handler:

/*-------------------------------------------------------------*//* Ex3.c - Process blocks for 60 seconds (unless interrupted). *//*-------------------------------------------------------------*/#include <signal.h>

void sighandler(int); /* Signal handling function prototype */

main(){

printf("Sleeping for 1 minute \n");

signal(SIGALRM,sighandler);alarm(60); /* Send process a SIGALRM signal in 60 */pause(); /* Block until any signal is received */

printf("All done \n");}

Page 9: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Signals

void sighandler(int signo){

printf("Caught signal %d \n",signo);return;

}

• The statement signal(SIGALRM,sighandler); informs the operating system that the sighandler function should be executed if a SIGALRM=14 signal is received for this process. Alarm(60) informs the operating system to send a SIGALRM signal to this process in 60 seconds. Finally, pause() simply blocks until any signal is received.

Page 10: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Pipes

• A typical example is what happens when you execute the command: $ ls | more

• In this case:A.the shell (ksh) creates a pipe,B.the shell forks off a child process to execute the

"ls" code, andC.the shell forks off another child to execute the

"more" code.

Page 11: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

How to create pipes in the code?

• Prototype: int pipe (int * p);• Example: int p[2];

if (pipe(p) == -1) { an error occurred, so exit };• Essentially, three values are returned from the pipe system

call:1. the value returned by the function is -1 if an error occured,2. the value returned in p[0] is the descriptor opened for

reading, and3. the value returned in p[1] is the descriptor opened for

writing. (An exception to this rule is UNIX SysVr4 in which the

descriptors denote ends of the pipe and can be used for both reading and writing; that is, bidirectional communication.)

Page 12: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Examples: Ex7.c and Ex8.c

• In Ex7.c, the parent creates a pipe, and forks off a child.• The child writes to the pipe (write(p[1],&x,sizeof x);), and the

parent reads from the pipe (read(p[0],&x,sizeof x);).• The descriptors are integers, p[0]=3 and p[1]=4 in the

example.• Whenever a process is executed, three descriptors are

automatically assigned to the process: 0 for standard input (stdin), 1 for standard output (stdout), and 2 for standard error (stderr). In UNIX, a user can redirect stdin, stdout and stderr. For example: $ ls > out causes standard out to be redirected to the file "out". That is, my directory listing is put in the file "out".

Page 13: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Pipes

• Pipes provide a stream of bytes with NO message boundaries.

• For example, the child could write 8 bytes and the parent could read 4 bytes 2 times, etc. This is illustrated by Ex8.c.

• Limitations: The pipe must be created before forking off the children because the children must inherit the descriptors to be used in the communication. This is one advantage of using "named pipes" instead of pipes.

Page 14: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Quiz

• Problem 1. How many process are created when the following code is executed? What happens?

$ ls | lp -dms222 - 2 processes are created...

$ ls > more - 1 process is created...

• Problem 2. What happens when the following code is executed? What is printed? x=3.

int p[2]; int x=3; pipe(p);

write(p[1],&x,sizeof(int));x=4;read(p[0],&x,sizeof(int));

printf("x = %d \n",x);

Page 15: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Signals and Timeouts

• To provide reliable network communication, we must have the ability to timeout and retransmit messages that get lost. To implement timeouts, signals can be used. Signals can be sent using kill(), either from the command-line (see kill(1)), or from within a process (see kill(2)).

• To see the relevant manual entries, type: man 1 kill, or man 2 kill.

• To print the entries: man 2 kill | col -b | lp –dP1• Example 5 (Ex5.c) shows how signals and alarms can

be used to implement a timeout.

Page 16: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Signals and Timeouts

• The statement signal(SIGALRM,sighandler); informs the operating system that the sighandler function should be executed if a SIGALRM=14 signal is received for this process.

• alarm(seconds) informs the operating system to send a SIGALRM signal to this process in a fixed number of seconds.

• Finally, alarm(0) turns the alarm off.• In the scanf() is interrupted by a signal, then it

returns an error (-1) and sets errrno=EINTR. Thus, we check to see if the read operation was interrupted. If so, it is re-executed in the while loop.

Page 17: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Semaphores• Semaphores can be used to synchronize processes (or

events within processes) and to control access to shared resources (such as shared memory).

• Def: A semaphore is a structure (record) that consists of 2 parts:

struct semaphore {int count;

queue Q; }

an integer counter - count, and a queue - Q which is used to hold processes that are blocked

on this semaphore.

Page 18: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Semaphores

• There are 2 semaphore operations: sem_wait() and sem_signal().

semaphore S;

sem_wait(S): if (S.count > 0) then S.count = S.count - 1; else block the process in S.Q;

sem_signal(S): if (S.Q is non-empty) then wakeup a process in S.Q;

else S.count = S.count + 1;

Page 19: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Examples of Semaphores

• Mutual Exclusion: Several processes want to have exclusive access to a shared resource.

semaphore S = 1;

All processes execute the following code to access the resource:

sem_wait(S);

(critical section - access the resource)

sem_signal(S);

Page 20: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Examples of Semaphores• Process Synchronization: Process A must finish executing

before process B begins executing. S = 0A --------------> BNote: the semaphore represents the constraint A before B.semaphore S = 0;Process A:

----------do work ...sem_signal(S);Process B:----------sem_wait(S);do work ...

• In this case, B must be sure that A is finished before starting to work.

Page 21: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Implementation• In the actual implementation, we need 4 system calls:

1. semid = sem_create(key, initial_value); - to create and initialize a semaphore.

2. sem_wait(semid);3. sem_signal(semid);4. sem_rm(semid); - to remove a semaphore from the IPC

table.• If the process bombs, we need to manually remove the

entries from the ipc table: use "ipcs" to view the ipc table, and "ipcrm -s <semid>" to remove an entry.

• Ex9.c, the parent creates a semaphore and initializes it to 1. Then, two child processes are created. They use the semaphore to ensure mutually exclusive access to a shared file called "balance". (see Mutual Exclusion above).

Page 22: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Shared Memory• Allows processes to have access to the same address space.• Access MUST be synchronized; for example, by using

semaphores.• Comparison with Pipes: Consider an ftp "get" operation. Pipes require the data to be

moved 4 times, whereas shared memory only requires the data to be moved 2 times. Pipes impose synchronization automatically because a process will be blocked if there is nothing to be read. However, processes using shared memory MUST be synchronized by using other means, such as semaphores.

• Shared Memory Operations:1. int shminit(key, size) - initialize a shared memory segment.2. char *shmat(shmid,0,0) - attach to a shared memory

segment.3. void shmkill(shmid) - remove shared memory segment.

Page 23: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

IPC Table Clean-Up

• Example 10.• Entries in the IPC table can be removed by

using: $ ipcs $ ipcrm -m <shmid> $ ipcrm -s <semid>

Page 24: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Quiz

• Problem 1. What happens? What will be printed if the error is removed?

• 5 will be printed if the erroneous line is removed.

int shmid; char *shmaddr; int *xp;

shmid = shminit (1234, 8); shmaddr = shmat (shmid, 0, 0); *xp = 3; this line is bad news and will cause a segmentation fault because xp doesn't point to memory allocated to this process. xp = (int *) (shmaddr + 4); *xp = 5; printf("%d \n",*(shmaddr+4)); shmkill(shmid);

Page 25: IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Quiz• Problem 2. Ditto from above. 5 will be printed. Note that

shmaddr[1] is shorthand for *(shmaddr + 1).

#include <stdio.h>#include "shared_mem.h"main(){

int shmid; int *shmaddr;

int *xp;

shmid = shminit (1234, 8); shmaddr = (int *) shmat (shmid, 0, 0); xp = shmaddr + 1; *xp = 5; printf("%d \n",shmaddr[1]); shmkill(shmid);}