Upload
arun-kumar
View
219
Download
0
Embed Size (px)
Citation preview
8/2/2019 Unix Inter Process Communication
1/28
Unix
Inter Process Communication
8/2/2019 Unix Inter Process Communication
2/28
Types of IPC Pipes
Named Pipes
Messages
Shared Memory
Semaphores
Sockets
8/2/2019 Unix Inter Process Communication
3/28
Pipes Allow data transfer in a FIFO manner
Allow synchronization of process execution
Use file system for data storage
Can be created using pipe() system call
Pipe manipulation is done using regular
system calls for files e.g. read(), write() Only related processes can access a pipe
8/2/2019 Unix Inter Process Communication
4/28
Pipes
Syntax
pipe (file_descriptors)
where file_descriptors is a pointer to an array of 2 integers
which are the file descriptors for reading and writing the
pipe
8/2/2019 Unix Inter Process Communication
5/28
Named Pipes Similar to pipes
Have directory entries
open system call used to open named pipes
mknod system call to create named pipes
Unrelated processes can also communicate
using named pipes
8/2/2019 Unix Inter Process Communication
6/28
MESSAGES Allow processes to send data streams to
arbitrary processes
Four system calls cover all the operations msgget
msgsend
msgrec
msgctl
8/2/2019 Unix Inter Process Communication
7/28
msgget
Returns the message queue identifier assocaited with the key
msgget(key, flag)
key is the name of the message queue
flag is the modes which can be passed
8/2/2019 Unix Inter Process Communication
8/28
msgsnd
msgsnd (id, message, size, flag)
Sends message to the queue associated with the message queue identifier specified
by id
message is of the form
struct msgbuf
{
long mtype;
char mtext[];
}
Flags can be
IPC_NOWAIT- Return -1 if the message cant be stored
MSG_NOERROR- Message bigger than size is shortened with no error returned
8/2/2019 Unix Inter Process Communication
9/28
msgrcvmsgsnd (id, message, size, type, flag)
Reads message from the queue associated with the message
queue identifier specified by id
message is of the form
struct msgbuf
{
long mtype;
char mtext[];
}
8/2/2019 Unix Inter Process Communication
10/28
msgctlMsgctl ( id, command, buffer )
Provides a variety of message control functions as specified bycommand
Commands can be
IPC_STAT store information about queue in the buffer
IPC_SET Change the information in the buffer
IPC_RMID remove the message queue identifier specified
by id
8/2/2019 Unix Inter Process Communication
11/28
SHARED MEMORY Allows processes to share part of their virtual
address space
System calls similar to the ones in messages
8/2/2019 Unix Inter Process Communication
12/28
shmgetint shmget ( key, size, flag )
key is the name of the shared memorysize is the size of shared memory segment in bytes
flag is a combination of
IPC_CREAT
IPC_EXCL
mode
8/2/2019 Unix Inter Process Communication
13/28
shmatInserts a shared memory segment into a process address space
char *shmat ( id, at_address, flag )
id is the shared memory segment identifier
size is the address in process address space where the shared
memory segment has to be attached
flag is a combination of
SHM_RDONLY read only
0 read,write
8/2/2019 Unix Inter Process Communication
14/28
shmdtRemoves a shared memory segment into a process address space
int shmdt ( dt_address )
dt_address is the address in the process address space of a
shared memory segment to be detached
8/2/2019 Unix Inter Process Communication
15/28
shmctlshmctl ( id, cmd, buf)
Provides shared memory control operations as specified by thecmd
cmd IPC_STAT
IPC_SET
IPC_RMID
SHM_LOCK
SHM_UNLOCK
8/2/2019 Unix Inter Process Communication
16/28
SEMAPHORE
Non-negative integer count used tocoordinate access to the resources
Initial semaphore count set to the number offree resources
Count decremented or incremented by thethreads or processes as and when theyacquire or free resources.
Threads block at count zero till it becomespositive
8/2/2019 Unix Inter Process Communication
17/28
semget
semget ( key, num, flag )
Returns or creates the semaphore identifier associated with the
keykey is the name of the semaphore set
number defines the number of semaphores in the set
flag is a combination of
IPC_CREAT
IPC_EXCL
8/2/2019 Unix Inter Process Communication
18/28
semop
semop ( id, operations, number )
Performs operations on the semaphore
id is the semaphore identifier
operations is the array of semaphore operation structure
operation consists of
- semaphore number
-operation
-flags
number is the number of entries in operations
8/2/2019 Unix Inter Process Communication
19/28
semctl
semctl ( id, semnum, cmd, ... )
Performs operations on the semaphore as specified by the cmd
id is the semaphore identifier
Fourth argument is optional, depending upon the operation
requested.
If required, then its a buffer.
cmd GETVAL, SETVAL, GETPID, GETNCNT, GETZCNT
8/2/2019 Unix Inter Process Communication
20/28
SOCKET
Creates an endpoint for communication and returns a descriptor
int socket ( domain, type, protocol )
domain specifies communication domain
type type of communication over socket (virtual circuit or
datagram)
protocol protocol to control the communication
8/2/2019 Unix Inter Process Communication
21/28
Other Socket System Calls
8/2/2019 Unix Inter Process Communication
22/28
Close
Closes the communication endpoint
close ( sd )
sd is the socket descriptor
8/2/2019 Unix Inter Process Communication
23/28
Bind
Assigns a name to an unnamed socket
int bind ( sd, sockname, namelength)
sd is the socket descriptor
sockname is the name of the socket
namelength is the length of the name
8/2/2019 Unix Inter Process Communication
24/28
Connect
Makes a connection to an existing socket
int connect ( sd, sockaddr, namelength)
sd is the socket descriptor
sockaddr is the name of the target socket
8/2/2019 Unix Inter Process Communication
25/28
Accept
Receives incoming requests for a connection
accept ( fd, addr, addrlen )
addr user data array which kernel fills with the address of the
connecting client
8/2/2019 Unix Inter Process Communication
26/28
send
Transmits data over a connected socket
send ( sd, msg, length, flag)
msg pointer to data being sent
length length of the data
8/2/2019 Unix Inter Process Communication
27/28
recv
Receives data over a connected socket
recv ( sd, buf, length, flag)
bufpointer to data being sent
length length of the data
8/2/2019 Unix Inter Process Communication
28/28
shutdown
Closes a socket connection
shutdown ( sd, mode )
mode specifies which side no longer permits data transmission