Loading…
Transcript

Pipes

FIFO

Message Queues

Sockets

Forms

half duplex pipes

FIFOs

full duplex pipes

named full duplex pipes

Message Queues

Semaphores

Shared Memory

Sockets

STREAMS

UNIX System IPC

- half duplex

- only common ancestors

Limitations:

#include <unistd.h>

int pipe(int fd[2]);

Creating pipe

Picture

Parent-Child

FILE *popen(const char *cmd,char *type)

popen and plcose

int pclose(FIlE *fp)

filters - program that reads from stdin and writes to stdout

coprocess- filters become co-process if same program generates filter's input and reads filter's output

Creating FIFO:

mkfifo(char *path,mode_t mode)

open- O_BLOCK (default)

O_NONBLOCK

No lseek on FIFO !!

Duplicating output streams

Client/Server Rendezvous points

unrelated process - FIFO

IPC structure (message queues, semaphores and shared memory)commonalities

internally identified using Identifiers, a non negative number. It increases monotonically and finally wraps around

external name for IDs

needs to specified whenever an IPC structure is created

data type - key_t

1. Server create IPC structure with key

as IPC_PRIVATE

Store it in a file for client to access

Drawback: Need file API

2. Client and server agree on a key by defining in common header

"Already created error" - Server to handle

3. Client and server agree on projectid and pathname

key_t ftok(char *path, int id);

single message call to send message

reliable, flow controlled, record oriented, arbitrary access

- system wide and no reference count

- not known by file names

- no multiplexed IO

XSI IPC

struct msqid_ds

{

msg_perm;

msg_qnum;

msg_qbytes;

msg_lspid;

msg_lrpid;

msg_stime;

msg_rtime;

msg_ctime;

}

#include<sys/msg.h>

int msgget(key_t key,int flag)

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

cmd - IPC_STAT, IPC_SET, IPC_RMID

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

ssize_t msgrcv(int msqid, void *ptr, size_t nb, long type, int flag);

Message Queue

Semaphore

It is not an IPC

It is a counter used to control access to shared resource

Procedure:

1. test semaphore value

2. If value>0 grant access, value--

3. If value=0 sleep until value >0

4. job done, value++

Complicated XSI semaphores

#include<sys/sem.h>

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

int semctl(int semid, int semnum, int cmd, ...);

flags- IPC_STAT, IPC_SET, IPC_RMID

GETVAL, SETVAL,

GETPID, SETPID

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

semop - can be >0, <0,=0

- fastest IPC

- needs synchronization

- provided by Semaphores

allows 2 or more processes to share a given region of memory

struct shmid_ds

{

shm_perm;

shm_segsz;

shm_lpid;

shm_cpid;

shm_nattch;

shm_atime;

shm_dtime;

shm_ctime;

}

Shared Memory

#include<sys/shm.h>

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

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

cmd- IPC_STAT, IPC_SET, IPC_RMID, SHM_LOCK, SHM_UNLOCK

void *shmat(int shmid,const void *addr, int flag)

int shmdt(void *addr)

-using pipes:parent-client, child-server

two pipes required

-using FIFO : server -well known FIFO

client specific FIFO

- using messagq queue:

shared message queue

exclusive message queue

ecurity

Client-server properties

Streams between parent and child

use API: s_pipe(int fd[2]).

Naming Stream pipes:

int fattach(int fd, const char *path)

STREAMS

Functions for unique connections:

1. serv_listen()

2. serv_accept()

3. cli_conn()

Passing File Descriptors

-one process opens file and passes descriptors to another

send_fd(int fd, int fd_to_send)

send_err(int fd, int status , char *erro)

recv_fd(int fd, functionpointer)

open server - server dedicated for opening of file or device and handing over file descriptor to client

Advantages

- less IPC data exchanged between client and server

-easy to modify as change at single place

- security

Protocol:

- client sends request for a file or device

- server returns with file descriptor or error

1. not a single value, rather set of values

2. creation and initialization seperate

3. can lead to garbages

struct sem_ds

{

sem_perm;

sem_nsems;

sem_otime;

sem_ctime;

}

binary semaphore

Client

Server

related process pipes