UNIT8_5

download UNIT8_5

of 60

Transcript of UNIT8_5

  • 8/13/2019 UNIT8_5

    1/60

    UNIT 8

    INTER PROCESS

    COMMUNICATION

  • 8/13/2019 UNIT8_5

    2/60

    The only way for the process to exchange information is bypassing open files across

    1)fork.2)Exec.3)filesystem.

    Different types of inter process communication are1) Pipes (half duplex).

    2) FIFOs (named pipes).3) Stream pipes (full duplex).4) Named stream pipes.5) Message queues.

    6) Semaphores.7) Shared memory.8) Sockets.9) Streams.(1-7) are restricted to ipc b/w process on same host

  • 8/13/2019 UNIT8_5

    3/60

    PIPESPipes are the oldest form of UNIX ipc and are provided by

    all unix systems.

    Pipes have two limitations.

    1) They are half duplex. Data flows only in one direction.

    2) They can be used only between process that have a

    common ancestor.

    Pipe creation

    Whenever a process calls fork, a pipe is created between

    parent and child.

    (fork creates a child process, a parent process will not dieuntil a child is terminated, we can kill the parentprocess abruptly)

  • 8/13/2019 UNIT8_5

    4/60

    A pipe is created by calling the pipe function.

    #includeint pipe(int filedes[2]);

    Returns

    0 if ok.

    -1on error.

    Two file descriptors are returned through the filedes

    argument.

    Filedes[0]:- open for reading

    Filedes[1]:- open for writing.

    **The output of filedes[1] is the input for filedes[0].

  • 8/13/2019 UNIT8_5

    5/60

    Two ends of a pipe connected in a single process.

    Fd[0] Fd[1]

  • 8/13/2019 UNIT8_5

    6/60

    Showing data flowing through thekernel

    Read writeFd[0] Fd[1]

    pipe

    User process

    kernel

  • 8/13/2019 UNIT8_5

    7/60

    Half duplex pipe after fork

    Read write

    Fd[0] Fd[1]

    parent child

    Read write

    Fd[0] Fd[1]

    pipe

    fork

  • 8/13/2019 UNIT8_5

    8/60

    Read write

    Fd[0] Fd[1]

    parent child

    Read write

    Fd[0] Fd[1]

    pipe

    fork

  • 8/13/2019 UNIT8_5

    9/60

    Fd[1] Fd[0]

    Pipe from parent to child

  • 8/13/2019 UNIT8_5

    10/60

    When one end of a pipe is closed, thefollowing rules apply.

    Reading from a pipe whose Write endclosed, after all the data has been read,read returns 0 to indicate the end of file.

    If we write to a pipe whose read end hasbeen closed, the signal sigpipeisgenerated. If we ignore or catch the

    signal, the return value for write will beset to errno EPIPE.

  • 8/13/2019 UNIT8_5

    11/60

    (implementation of above diagram)#includeInt main(void){Int n,fd[2];Pid_t pid;

    Char line[MAXLINE];If (pipe(fd)

  • 8/13/2019 UNIT8_5

    12/60

    Popen() and pclose()Popen() pclose() functions handle the following operations which are done by

    pipe().

    1) Creation of pipe.2) The fork of a child.

    3) Closing the unused ends of the pipe.

    4) Execing the shell to execute the command.

    5) Waiting for the command to terminate.

    Syntax:-

    #include

    FILE *popen( const char *cmdstring, const char *type);

    Returns

    File pointer if ok

    -1on error

    Int pclose(FILE *fp);

    Returns

    Termination status of cmdstring if ok

    -1on error

  • 8/13/2019 UNIT8_5

    13/60

    If type is r the file pointer is connected to the standard o/p of cmdstring.

    If type is w the file pointer is connected to the standard i/p of cmdstring.

    fp Stdout

    parent child

    Result of fp=popen(ls *.c,r);

    Stdin

    fp

    parent child

    Result of fp=popen(ls *.c,w);

  • 8/13/2019 UNIT8_5

    14/60

    Stdout

    User at a terminal

    parent Filter program

    Popen pipe

    User inputprompt

    Transforming input using popen()

    stddin

    stdout

  • 8/13/2019 UNIT8_5

    15/60

    FIFO1) FIFOS are also called named pipes.

    2) FIFOS are used between unrelated processto exchange data,

    whereas pipes are used between related process.3) FIFO is a type of a file.

    4) st_mode of stat()system call tells, whether the file is a fifo ornot. Use S_ISFIFOmacro in open(), in mode argument to test thefile is FIFO or not.

    5) If file type is FIFO, in $lsl,the first column will have p i.e. it is afifo file.

    Creating a FIFO.

    Creating a FIFO is similar to creating a file. (unix system V.4)

    #include#include

    int mkfifo (const char *pathname, mode_t mode);

    Returns

    0if ok

    -1on error

  • 8/13/2019 UNIT8_5

    16/60

    Note:-Open is used to create an ordinary file. Mkfifo() is used to create a special file

    FIFO.

    UNIX system V.3 uses mknod() to create FIFO files.

    int mknod (const char *pathname, int mode);

    Returns

    0if ok-1on error.

    Arguments:-1) The pathnameargument is the name of the FIFO file to be created.

    2) The mode argument specifies the access permissionfor user, group andothers, to be assigned to the file.

    3) The permissions are set in the same way as open(), but instead ofspecifying O_RDONLY|O_RDWR|O_WRONLY the symbolic constantS_ISFIFO is used. S_IRWXU,S_IRWXO,S_IRGRP etc. are used

  • 8/13/2019 UNIT8_5

    17/60

    Opening closing, reading, writing FIFOS1) Once we have created a FIFO using mkfifo()we open it using

    open().

    2) Normal file I/O functions open(), close(), read(), write(), unlink()etc all work with FIFOs.

    EXAMPLE:-

    1) To create a FIFO file called FIFO5with access permission of readwrite execute for everyone.

    mkfifo( FIFO5, S_ISFIFO|S_IRWXU|S_IRWXG|S_IRWXO);

    2) Create a FIFO in /tmp/fifo1 with rwx permission to user and writepermission to group.

    mkfifo( /tmp/fifo1,S_ISFIFO|S_IRWXU|S_IWGRP);

  • 8/13/2019 UNIT8_5

    18/60

    O_NONBLOCKflag is specified/ unspecified in open() function

    Not specified

    1) Opens for read only blocks, until some other process opens FIFOfor writing.

    2) Opens for write only blocks, until some other process opens FIFOfor reading.

    Specified

    1) Open for read only returns immediately.

    2) Open for write only returns an error with errno of ENXIO, if noother process has the FIFO open for reading.

  • 8/13/2019 UNIT8_5

    19/60

    Open for reading:- If we write to a FIFO that has no process open for readingthe

    signal SIGPIPE is generated.

    When the last writer for a FIFO closesthe FIFO, an end of the file

    is generated for the reader of the FIFO.

    We can have multiple writersfor a FIFO.

    PIPE_BUF specifies the max amount of datathat can beautomatically written to a FIFO.

    USES:-1) FIFOS are used by shell commands to pass data from one shell

    pipeline to another, without creating intermediate temporary files.

    2) FIFOS are used in a client server applicationto pass data betweenthe clients and server.

    E l f FIFOS

  • 8/13/2019 UNIT8_5

    20/60

    Examples of FIFOSUsing FIFOS to duplicate o/p streams.$mkfifo fifo1$prog3

  • 8/13/2019 UNIT8_5

    21/60

    PROG1

    PROG3FIFO1

    tee

    PROG2

    i/p file

    Using a FIFO and tee to send a stream to two different processes

  • 8/13/2019 UNIT8_5

    22/60

    Client server example using FIFO

    SERVER

    WELL KNOWN FIFO

    CLIENTCLIENT

    WRITE WRITE

    READ REQUEST

    FIG:- Client sending requests to a server using a FIFO

  • 8/13/2019 UNIT8_5

    23/60

    Readreplies

    SERVER

    WELL KNOWN FIFO

    CLIENTCLIENT

    WRITE WRITE

    READ REQUEST

    FIFO client

    specificFIFO client

    specific

    Write req Write req

    Readreplies

    Fig:-client server communication using FIFOS

  • 8/13/2019 UNIT8_5

    24/60

    Program for creation of fifo

  • 8/13/2019 UNIT8_5

    25/60

    Message queues Message queue are linked list of messages stored within the kernel

    and identified by message queue identifier.

    A new message queue is created, or an existing message queue isopened by msgget.

    New messages are addedto the end of a queue by msgsnd.

    Every message has a positive long integer type field, a non-negativelength, and the actual data bytes all of which are specified tomsgsnd when the message is added to the queue.

    Messages are fetchedfrom a message queue by msgrcv.

    We dont have to fetch the messages in first in first out order.

    Instead we can fetch the messages on their type field.

    kernel

  • 8/13/2019 UNIT8_5

    26/60

    kernel

    Msg_perm

    structure

    Msg_first

    Msg_last

    Msg_ctime

    Struct msqid_ds

    link

    Type=100

    Length=1

    data

    link

    Type=300 Type=200

    Length=1Length=1

    link

    data data

    Fig:- msg Q structure in kernel

  • 8/13/2019 UNIT8_5

    27/60

    Each message queue has the following data structure associated withit. This data structure defines the current status of the queue

    struct msqid_ds

    {

    struct ipc_perm msg_perm; /* permission structure */

    struct msg *msg_first; /* ptr to first msg on msg queue */

    struct msg *msg_last; /* ptr to last msg on msg queue */

    ulong msg_cbytes; /* current no. of bytes on msg queue */ulong msg_qnum; /* no. of msgs on msg queue */

    ulong msg_qbytes; /* Max no. of bytes on msg queue */

    pid_t msg_lspid; /* pid of last msgsnd() */

    pid_t msg_lrpid; /* pid of last msgrcv() */

    time_t msg_stime; /*last msgsnd() time */

    time_t msg_rtime; /*last msgrcv() time */

    time_t msg_ctime; /*last change time */

    };

    Struct ipc perm

  • 8/13/2019 UNIT8_5

    28/60

    Struct ipc_perm

    {

    uid_t uid; /*owners effective userid */

    gid_t gid; /*owners effective group id */

    uid_t cuid; /*creators effective userid */gid_t cgid; /*creators effective group id */

    mode_t mode; /* access modes */

    ulong seq; /*slot usage sequence number */

    key_t key; /*key */};

    To modify user id and group id fields we use msgctl

  • 8/13/2019 UNIT8_5

    29/60

    To modify user id and group id fields we use msgctl .

    The two pointers msg_first and msg_last are worthless to a user process, as thesepoint to where the corresponding messages are stored with in the kernel.

    Message queues are

    1)Connection oriented.

    2)Reliable

    3)Flow control.

    4)Stores as records.

    5)Can assign priorities.

    System limits:-

    Limits are set in the file /etc/conf/cf.d/mtune.

    In system limit file we have max and min values of messages, semaphores etc.

    MSGMAX:- size in bytes of largest message we can send.

    MSGMNB:- MAX sizein bytes ofa particular queue(i.e. sum of allmessages on queue)

    MSGMNI:- Max no. of message qssystem wide.

    MSGQTL:- MAX no. of messagessystem wide.

    OPENING AN EXISITING MESSAGE QUEUE OR CREATING A NEW

  • 8/13/2019 UNIT8_5

    30/60

    OPENING AN EXISITING MESSAGE QUEUE OR CREATING A NEWMESSAGE QUEUE

    Msgget is used to open a message queue (or) create new

    #include

    #include#include

    Int msgget( key_t key, int flag);

    Return

    Msg q id if ok-1on error

    Key:-

    1) Each ipc structurein the kernel is referredby a non-negative integer identifier. Tosend or fetch a message to or from a message queue, we need to know theidentifier.

    2) Ipc identifiers are not small integers.

    3) When a given ipc structure is created or removed, the identifier associated with thatstructure continually increases until it reaches maximum positive value for an integer,

    and then wraps around to 0.

    Slot usage sequence number:-

  • 8/13/2019 UNIT8_5

    31/60

    Slot usage sequence number:The identifier is remembered even after the ipc_structure is deleted, andincreemented each time the structure is used. This is called slot usage sequencenumber.

    Whenever an ipc structure is created, a key must be specified. The data type of thiskey is primitive system/data type key_t, which is often defined as long integer in the

    header . This key is converted into the identifier by the kernel.

    New ipc structure is created if either

    1)Key is IPC_PRIVATE.2)Key is currently associated with an ipc structure of particular type and the IPC_CREATbit of a flag is specified.

  • 8/13/2019 UNIT8_5

    32/60

    INITIALIZATIONS FOR NEW MESSAGE QUEUEWhen a new message Queue is created the following members of the

    msqid_ds structure are initialized.

    1) Mode is specified from corresponding permission bits.

    Permissions message queues

    User read MSG_R

    User write MSG_W

    GROUP READ MSG_R>>3

    GROUP WRITE MSG_W>>3

    OTHER READ MSG_R>>6

    OTHER WRITE MSG_W>>6

    2) msg_qnum, msg_lspid msg_lrpid msg_stime msg_rtime are all set to 0

    3) msg_ctime is set to the current time.

    4) msg_qbytes is set to the system limit.

  • 8/13/2019 UNIT8_5

    33/60

    MsgctlThe msgctl function performs various options on a queue.

    #include#include

    #include

    Int msgctl( int msqid, int cmd, struct msqid_ds *buf);

    Returns

    Oif ok.

    -1on error.

    Arguments

    The cmdargument specifies the command to be performed, on the

    queue specified by msqid.IPC_STAT, IPC_SET, IPC_RMID

    IPC_STAT:-

    Fetch the msqid_ds structure for this message queue storing it in thestructure pointed to by buf.

    IPC SET:

  • 8/13/2019 UNIT8_5

    34/60

    IPC_SET:-

    Sets the following four field

    1) msg_perm.uid

    2) msg_perm.gid

    3) msg_perm.mode

    4) msg_qbytes

    IPC_RMID:-

    Removes the message queue from the system and anydata still on the queue. This removal is immediate.

    IPC_SET & IPC_STAT:-

    This can be executed only by the process whoseEffective user id==msg_perm.uid

    Or by a process with super user privilleges

    Msgsnd:

  • 8/13/2019 UNIT8_5

    35/60

    Msgsnd:-Data is placed onto a message queue by calling msgsnd

    #include

    #include

    #include

    Int msgsnd( int msqid, const void *ptr, size_t nbytes, int flag);

    Returns

    0 if ok

    -1on errorArguments :-

    Ptr points to a long integer that contains the positive integer messagetype and it is immediately followed by the message type.

    Struct my_mesg

    {

    long mtype; /* positive message type */

    Char mtext[512]; /* message data, of length nbytes */

    };

    Ptr is pointer to structure my_mesg.

    Msgrcv:-

  • 8/13/2019 UNIT8_5

    36/60

    Msgrcv:-Messages are retrived from a queue by msgrcv

    #include

    #include

    #include

    Int msgrcv( int msqid, void *ptr,size_t nbytes, long type, int flag);

    Returns

    Size of data portion of messageif ok

    -1on error.msgrcv(msgid,(void *) &some_data, BUFSIZ, msg_to_receive, 0)

    The type argument lets us specify which message we want

    Type==0 the first message on the queue is returned.

    Type>o the first message on queue whosemessage type == type is returned.

    Type

  • 8/13/2019 UNIT8_5

    37/60

    semaphoreSemaphore value is stored in the kernel.

    To obtain a shared resource, a process needs to do thefollowing:

    1. Test the semaphore that controls the resource.2. If the value of the semaphore is positive, the process can use the

    resource. In this case, the process decrements the semaphore valueby 1, indicating that it has used one unit of the resource.

    3. Otherwise, if the value of the semaphore is 0, the process goes tosleep until the semaphore value is greater than 0. When the processwakes up, it returns to step 1.

    4. When a process is done with a shared resource that is controlled by asemaphore, the semaphore value is incremented by 1. If any otherprocesses are asleep, waiting for the semaphore, they are awakened.

  • 8/13/2019 UNIT8_5

    38/60

    semaphore

    Semaphore value is stored in the kernel.Steps to obtain a shared resource by a process.

    1. Test the semaphore that controls the resource.

    2. value of the semaphore is positive. process decrementsthe semaphore value by 1.

    3. value of the semaphore is 0, process goes to sleep.

    until the semaphore value is greater than 0

    4. process is done with a shared resource that iscontrolled by a semaphore, the semaphore value is

    incremented by 1

  • 8/13/2019 UNIT8_5

    39/60

    Process A Process B

    Semaphore0 or 1

    kernel

    Fig:- semaphore value stored in the kernel

    kernel

  • 8/13/2019 UNIT8_5

    40/60

    Fi :- Kernel data structure for sema hore set

    Sem_ctime

    Sem_otime

    Sem_nsems

    Sem_base

    Sem_perm

    strucutre

    semzcnt

    semncnt

    sempid

    semval

    semzcnt

    semcntsempid

    semval

    semid

    Struct semid_ds

    kernel

    binary semaphore: It controls a single resource and its value is

  • 8/13/2019 UNIT8_5

    41/60

    binary semaphore:It controls a single resource, and its value isinitialized to 1.

    In general, however, a semaphore can be initialized to any positive value,

    with the value indicating how many units of the shared resource areavailable for sharing.

    The kernel maintains a semid_ds structure for each semaphore set:

    struct semid_ds {

    struct ipc_perm sem_perm; /* permissions */

    Struct sem *sem_base; /* ptr to first semaphore in the set */

    unsigned short sem_nsems; /* # of semaphores in set */

    time_t sem_otime; /* last-semop() time */time_t sem_ctime; /* last-change time */

    .

    .

    .

    Struct

  • 8/13/2019 UNIT8_5

    42/60

    {

    unsigned short semval; /* semaphore value, always >= 0 */

    pid_t sempid; /* pid for last operation */

    unsigned short semncnt; /* # processes awaiting semval>curval */

    unsigned short semzcnt; /* # processes awaiting semval==0 */

    .

    .

    .

    };

    System limits that effect semophore sets

    SEMVMX: The maximum value of any semaphore

    SEMAEM: The maximum value of any semaphore's adjust-on-exit value

    SEMMNI: The maximum number of semaphore sets, systemwide SEMMNS: The maximum number of semaphores, systemwide

    SEMMSL:The maximum number of semaphores per semaphore set

    SEMMNU: The maximum number of undo structures, systemwide

    SEMUME: The maximum number of undo entries per undo structures

    SEMOPM: The maximum number of operations per semop call

    The first function to call is semgetto obtain a semaphore ID.

  • 8/13/2019 UNIT8_5

    43/60

    #include

    int semget(key_t key, int nsems, int flag);

    Returns:

    semaphore ID if OK,-1 on error

    The modemember of this structure is set to the correspondingpermission bits of f lag.

    sem_otime is set to 0.

    sem_ctime is set to the current time.

    sem_nsems is set to nsems.

    The semctl function is the catchall for various semaphore operations.

  • 8/13/2019 UNIT8_5

    44/60

    #include

    int semctl(int semid, int semnum, int cmd,... /* union semun arg */);

    Returns :

    For all the GET commands other than GETALL the function returns thecorresponding value

    For remaining commands the return value is 0.

    The fourth argument is optional, depending on the command requested.

    The value of semnumis 0 to nsems-1.

    union semun {int val; /* for SETVAL */

    struct semid_ds *buf; /* for IPC_STAT and IPC_SET */

    unsigned short *array; /* for GETALL and SETALL */

    };

    For cmd argument

  • 8/13/2019 UNIT8_5

    45/60

    For cmdargument

    IPC_STAT Fetch the semid_ds structure for this set, storing it in thestructure pointed to by arg.buf.

    GETVAL Return the value of semval for the member semnum.

    SETVAL Set the value of semval for the member semnum. The value isspecified by arg.val.

    GETPID Return the value of sempid for the member semnum.

    GETNCNT Return the value of semncnt for the member semnum.

    IPC_SETSet the

    1) sem_perm.uid,2) sem_perm.gid, and3) sem_perm.mode fields from the structure pointed to by arg.buf

    IPC_RMID:Remove the semaphore set from the system. This removal isimmediate

    The function semop atomically performs an array of operations on a

  • 8/13/2019 UNIT8_5

    46/60

    semaphore set.

    #include

    int semop(int semid, struct sembuf semoparray[], size_t nops);

    Returns: 0 if OK, 1 on error

    The semoparrayargument is a pointer to an array of semaphoreoperations, represented by sembuf structures:

    struct sembuf

    {

    unsigned short sem_num; /* member # in set (0, 1, ..., nsems-1) */

    short sem_op; /* operation (negative, 0, or positive) */short sem_flg; /* IPC_NOWAIT, SEM_UNDO */

    };

    The nopsargument specifies the number of operations (elements) inthe array.

    Sh d M

  • 8/13/2019 UNIT8_5

    47/60

    Shared Memory Shared memory allows two or more processes to share a

    given region of memory

    This is the fastest form of IPC, because the data does notneed to be copied between the client and the server.

    If the server is placing data into a shared memory region,the client shouldn't try to access the data until the server isdone. Often, semaphores are used to synchronize sharedmemory access

    struct shmid_ds {

  • 8/13/2019 UNIT8_5

    48/60

    _ {

    struct ipc_perm shm_perm; /* Permissions */

    size_t shm_segsz; /* size of segment in bytes */

    pid_t shm_lpid; /* pid of last shmop() */pid_t shm_cpid; /* pid of creator */

    shmatt_t shm_nattch; /* number of current attaches */

    time_t shm_atime; /* last-attach time */

    time_t shm_dtime; /* last-detach time */time_t shm_ctime; /* last-change time */

    :

    :

    :

    };

  • 8/13/2019 UNIT8_5

    49/60

    client server

    FIFO, pipe ormessage

    Output file Input file

    Fig:- typical movement of data between client and server

  • 8/13/2019 UNIT8_5

    50/60

    client Shared memory server

    Outputfile

    Inputfile

    Fig: movement of data between client andserver using shared memory

    The first function called is usually shmget to obtain a

  • 8/13/2019 UNIT8_5

    51/60

    The first function called is usually shmget, to obtain ashared memory identifier.

    #include

    int shmget(key_t key, size_t size, int flag);

    Returns:

    shared memory ID if OK,

    -1 on error

    The shmctl function is the catchall for various shared

  • 8/13/2019 UNIT8_5

    52/60

    The shmctlfunction is the catchall for various sharedmemory operations.

    #include

    int shmctl(int shmid, int cmd, struct shmid_ds *buf);

    Returns: 0 if OK, -1 on error The cmd argument specifies one of the following five commands to be

    performed, on the segment specified by shmid.

    IPC_STAT Fetch the shmid_ds structure for this segment, storing it in thestructure pointed to by buf.

    IPC_SETSet the following three fields from the structure pointed to by bufin the shmid_ds structure associated with this shared memory segment:shm_perm.uid, shm_perm.gid, and shm_perm.mode. This commandcan be executed only by a process whose effective user ID equalsshm_perm.cuid or shm_perm.uid or by a process with superuser privileges.

  • 8/13/2019 UNIT8_5

    53/60

    IPC_RMID Remove the shared memory segment set from the system.Since an attachment count is maintained for shared memory segments (theshm_nattch field in the shmid_ds structure), the segment is not removeduntil the last process using the segment terminates or detaches it.

    SHM_LOCKLock the shared memory segment in memory. This commandcan be executed only by the superuser.

    SHM_UNLOCKUnlock the shared memory segment. This command canbe executed only by the superuser.

    System limits that effect shared memory

    SHMMAX: The maximum size in bytes of a shared memory segment.

    SHMMIN: The minimum size in bytes of a shared memory segment

    SHMMNI: The maximum number of shared memory segments,

    systemwide. SHMSEG: The maximum number of shared memory segments, per

    process

    Once a shared memory segment has been created, a process attaches it to itsdd b lli h t

  • 8/13/2019 UNIT8_5

    54/60

    address space by calling shmat.

    #include void *shmat(int shmid, const void *addr, int f lag);

    Returns:pointer to shared memory segment if OK,-1 on error

    The address in the calling process at which the segment is attacheddepends on the addr argument and whether the SHM_RND bit is

    specified in flag. If addr is 0, the segment is attached at the first available address

    selected by the kernel. This is the recommended technique. If addr is nonzeroand SHM_RND is not specified, the segment is

    attached at the address given by addr. If addr is nonzero and SHM_RNDis specified, the segment is

    attached at the address given by (addr - (addr modulus SHMLBA)).

    The SHM_RND command stands for "round." SHMLBA stands for "lowboundary address multiple" and is always a power of 2.

    What the arithmetic does is round the address down to the next multipleof SHMLBA.

    #include

  • 8/13/2019 UNIT8_5

    55/60

    int shmdt(void *addr);

    Returns: 0 if OK, -1 on error

    The addrargument is the value that was returned by a previous call toshmat. If successful, shmdt will decrement the shm_nattch counter in the

    associated shmid_ds structure.

    The addr argument is the value that was returned by a previous call toshmat. If successful, shmdt will decrement the shm_nattch counter in the

    associated shmid_ds structure.

  • 8/13/2019 UNIT8_5

    56/60

    Namespaces

    The set of possible names for a given type of IPC is called its namespace.

    Ipc_type Name space identificationPipe

    FIFO

    Message queue

    Shared memory

    Semaphore

    Socked (unix domain)

    Socket (other domain)

    (no name)

    Pathname

    Key_t key

    Key_t key

    Key_t key

    Pathname

    (domain dependent)

    File descriptor

    File descriptor

    Identifier

    Identifier

    Identifier

    File descriptor

    File descriptor

    The function ftok() is provided by the system V standard C library to

  • 8/13/2019 UNIT8_5

    57/60

    () p y y yconvert the pathname and project identifier into a system V IPC.

    #include

    #include

    Key_t ftok( char *pathname, char proj);Key_t is a 32 bit integer.

    Ftok()Msgget()Semget()Shmget()

    Char *path

    Char proj

    Key_t key Int id

    Fig:-generating IPC id`s using ftok()

    Start here OKCREATE NEW ENTRY RETURN ID

  • 8/13/2019 UNIT8_5

    58/60

    Fig:- Logic for opening or creating an IPC channel.

    Key==IPC_PRIVATE

    DOES KEY ALREADYEXIST

    Are both IPC_CREATand IPC_EXCL set?

    Is the acess

    permission OK?

    System tables full ?

    IPC_CREAT set?

    yes

    no

    yes

    no

    yes

    Ok return id

    no

    yes

    no

    Error return

    Errno=ENOSPC

    Error return

    Errno=ENOENT

    Error return

    Errno=EEXIST

    Error return

    Errno=EACESS

    SHAREDSEMAPHOREMESSAGE

  • 8/13/2019 UNIT8_5

    59/60

    Shmget

    Shmctl

    Shmat

    shmdt

    semget

    Semctl

    semop

    Msgget

    Msgctl

    Msgsnd

    Msgrcv

    Include file

    System call to create or open

    System call to create or open

    System calls for IPC

    operations

    SHAREDMEMORY

    SEMAPHOREMESSAGEQUEUES

    Fig:- summary of System V IPC system calls

    Permission Message Semaphore Shared

  • 8/13/2019 UNIT8_5

    60/60

    Permission Messagequeue

    Semaphore Sharedmemory

    User read

    User write(alter)

    MSG_R

    MSG_W

    SEM_R

    SEM_W

    SHM_R

    SHM_RGroup read

    Group-write(alter)MSG_R>>3

    MSG_W>>3

    SEM_R>>3

    SEM_R>>3

    SHM_R>>3

    SHM_W>>3

    Other read

    Other write( alter)

    MSG_R>>6

    MSG_R>>6

    SEM_R>>6

    SEM_R>>6

    SHM_R>>6

    SHM_W>>6

    FIG:- System V IPC permissions