SlideShare a Scribd company logo
SYSTEMS PROGRAMMING
LECTURE 7
INTERPROCESS COMMUNCATION
INTERPROCESS COMMUNICATION
• IPC allows different processes to communicate
between themselves
• So far, processes could communicate using fork/child
inheritance, passing arguments in exec calls, through
the file system and using signals
• There are more structures which allows proceses to
communicate more efficiently
• IPC structures provide different flavors of
communication
• Some can only be used between realated processes
(fork/child). Unnamed structures
INTERPROCESS COMMUNICATION
• Named structures can be used by anyone having
access rights
• System V IPC structures follow the same access
protocol, but some extra form of initial
communication is necessary for the processes to
use them
• Different implementations might support one type
of IPC and not another
• Some structures are handled differently between
implementations
PIPES
• Oldest and most widely implemented form of IPC
• Data can flow in one direction (half duplex)
• Pipes must be used in related processes since their
identifier is the file descriptor
• pipe creates a pipe and places 2 file descriptors
inside fd. fd[0] is opened for reading and fd[1]
for writing
• Pipes work in a FIFO fashion
#include <unistd.h>
int pipe(int fd]);
Return 0 if OK, 1 on error
PIPES
• fstat return st_mode of FIFO which can be tested
by the S_ISFIFO macro
• We use normal read, write and close operations
to access pipes
• If we read from a pipe whose write end has been
closed, read returns 0, showing an end of file
• If we write to a pipe whose read end has been closed,
write returns -1 with errono EPIPE, and SIGPIPE
is generated
• The constant PIPE_BUF gives the maximum amount of
bytes that can be written in one go without interleaving
between different writers
PIPES
• Within one process, a pipe is useless
• We normally fork a child process and then close
one side of the parent and close the other side
in the child
FIFOS
• FIFOs are just like normal files but they behave like
pipes with a pathname
• FIFOs are full duplex and more than one process
can open a FIFO
• Other unrelated processes can access FIFOs using
normal open, read, write and close system
calls
• To remove a FIFO file, call unlink
• Normal access file permissions apply. The stat
function returns the type as FIFO like pipes (in ls
marked with a ‘p’)
FIFOS
• If we write to a FIFO which no process has opened
for reading, SIGPIPE is generated
• When no writer exists, a read returns 0 for EOF
• A FIFO can be created with mkfifo
• mode in mkfifo is the same as mode in open for
file access permissions
#include <sys/stat.h>
int mkfifo(const char* path, mode_t mode);
Return 0 if OK, 1 on error
FIFOS
• When we open a FIFO, the O_NONBLOCK flag
affects what happens:
• If not specified, an open for read-only blocks
until a two way communication exists
• If specified an open for read-only returns
immediately. An open for write-only returns with
error -1 and errno ENXIO unless another process
has opened the FIFO for reading
• If we write to a FIFO that no process has opened
for reading the signal SIGPIPE is generated
XSI IPC
• The three types of IPC referred to as XSI IPC (message
queues, semaphores and shared memory) have many
similar features
• All these structures use the same access protocol
• Each IPC has a key associated with it
• This key is global to the whole system (kernel)
• Two processes using the same key will be given access
to the same IPC structure
• In supplying the key an identifier is returned, which is
then used to operate in the IPC. This identifier is unique
to the whole system too
XSI IPC
• Keys and identifiers will run out in the system if no
IPC structures are removed
• A special key exists: IPC_PRIVATE which is
guaranteed to be an unused key
• The identifier or key must somehow be passed
between different processes using the same IPC
structure
• Keys or identifiers can be passed between
processes using the filesystem, command line values,
an agreed upon header, parent/child inheritance or
using ftok.
XSI IPC
• ftok converts an agreed upon pathname and
project ID into an IPC key
• The general format to obtain an identifier is
Xget(key, flag)
• If a key is passed the second process will have to ‘re-
create’ the IPC structure, if the identifier is passed,
second process will access the IPC using this identifier
#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok(char *pathname, char projID);
XSI IPC
• A new IPC structure will be created if the key is
equal to IPC_PRIVATE or a new key is given
and IPC_CREAT is specified in flag (in
Xget)
• Using IPC_EXCL with IPC_CREAT in flag
will produce and error with errno set to EEXIST
if an IPC already exists with the same key
• When creating an IPC, access permissions are
given in flag of Xget
XSI IPC
Permission Message
Queues
Semaphore Shared
Memory
user-read
user-write
MSG_R
MSG_W
SEM_R
SEM_A
SHM_R
SHM_W
group-read
group-write
MSG_R >> 3
MSG_W >> 3
SEM_R >> 3
SEM_A >> 3
SHM_R >> 3
SHM_W >> 3
other-red
other-write
MSG_R >> 6
MSG_W >> 6
SEM_R >> 6
SEM_A >> 6
SHM_R >> 6
SHM_W >> 6
MESSAGE QUEUES
• This is a linked list of messaged stored within the kernel
• msgget opens an existing queue, or creates a new
one, and returns the IPC queue identifier
• msgctl is used to remove the structure created by the
same effective user. Removal is immediate and anyone
using the structure will get an error EIDRM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t, int flag)
Returns message queue ID, -1 on error
int msgctl(int msgid, IPC_RMID, NULL)
Returns -1 on error
MESSAGE QUEUES
• Each queue has the structure above associated with it
• It defines the current status of the queue
struct msqid_ds {
struct ipc_perm msg_perm;
msgqnum_t msg_qnum;
msglen_t msg_qbytes;
pid_t msg_lspid;
pid_t msg_lrpid;
time_t msg_stime;
time_t msg_rtime;
time_t msg_ctime;
. . .
}
MESSAGE QUEUES
• Some limits exist for message queues:
• MSGMAX: largest message allowed in bytes
• MSGMNB: max size of a queue in bytes
• MSGMNI: max number of queues, system wide
• MSGTQL: max number of messages, system wide
• Each message consists of a positive long value
specifying the message type followed by data
• Messages are always places at the end of the
queue
MESSAGE QUEUES
• The cmd argument specifies the command to be
performed on the queue:
• IPC_STAT: Fetch msqid_ds structure of the queue
• IPC_SET: Set some structure values to queue
• IPC_RMID: Remove message queue from the system
and any data still on the queue
#include <sys/msg.h>
int msgctl(int msgid, int cmd, struct,
msqid_ds *buf)
Returns -1 on error
MESSAGE QUEUES
• The ptr argument points to a long integer that contains
the positive integer message type and is immediately
followed by the data. We can use a struct like:
#include <sys/msg.h>
int msgsnd(int msqid, const void* ptr,
size_t nbytes, int flag);
Returns -1 on error
struct mymesg {
long mtype; // Positive message type
char mtext[512]; // Message data
}
MESSAGE QUEUES
• A flag of IPC_NOWAIT can be specified
• If message queue is full causes msgsnd to return
immediately with EAGAIN
• If not specified, call is blocked until there is room,
queue is removed or signal is caught
• Since a reference count is not maintained with each
message queue the removal of a queue simply
generates errors on the next queue operation
• When msgsnd returns successfully, the msqid_ds
structure is updated
MESSAGE QUEUES
• If returned message is larger than nbytes and
MSG_NOERROR is set in flag, the message is
truncated (without warning)
• Otherwise E2BIG is returned
• type argument lets us specify which message we
want
#include <sys/msg.h>
int msgrcv(int msqid, void* ptr,
size_t nbytes, int flag);
Returns -1 on error
MESSAGE QUEUES
• type == 0: the first message on the queue is
returned
• type > 0: first message on the queue whose
message type equals type is returned
• type < 0: first message whose message type is
the lowest value less than or equal to abs(type)
is returned
• A nonzero type is used to read the messages in an
order other than first in, first out
• IPC_NOWAIT will make the operation non-
blocking, ENOSMG if message type doesn’t exist
POSIX MESSAGE QUEUES
• Much nicer interface
• Queues are reference counted (only removed
when all processes are detached from it)
• Visible on a pseudo-filesystem
• Notion of message priority
• Message notification system, where processes
can register to receive notifications when a new
message arrived on an empty queue (action is
signal or start new thread)
SEMAPHORES
• Counter used to provide access to shared data for
multiple processes
• When a semaphore is positive a process decrements
the semaphore by 1 signifying it has used up one
resource
• When the semaphore is 0 it means the resource is
not available and the process blocks until the
resource is available
• Testing and decrementing is done atomically to
guarantee functionality
SEMAPHORES
• Semaphores are created in sets with each set holding
one or more semaphores
• The are limits affecting semaphores:
• SEMVMX: max value of semaphore
• SEMMNI: max number of system-wide semaphore
sets
• SEMMNS: max number of system-wide semaphores
• SEMMSL: max number of semaphores per set
• SEMOPM: max number of operations per semop
call
SEMAPHORES
• XSI semaphores are a complicated implementation of
semaphores:
• The use of semaphore sets instead of single
independent ones
• The creation and initialisation of semaphores are
independent (not performed atomically)
• We have to worry about a program that
terminates without releasing its allocated
semaphores
• The kernel maintains a similar structure to msqid_ds
for each semaphore and semaphore set
SEMAPHORES
struct semi_ds {
struct ipc_perm sem_perm;
unsigned short sem_nsems;
timet_t sem_otime; // last semop time
timet_t sem_ctime // last-change time
. . .
}
struct {
unsigned short semval; // Semaphore value
pid_t sempid; // pid of last operation
unsigned short semcnt; // #procs semval>curval
unsigned short semzcnt; // #rpocs semval==0
}
SEMAPHORES
• Obtain a semaphore ID with specified number of
semaphores in set
• If referencing an existing set, nsems can be 0
• semctl is a catchall for various semaphore
implementations
#include <sys/sem.h>
int semget(key_t, int nsems, int flags);
Returns ID is OK, 1 on error
int semctl(int semid, int semnum, int cmd,
... /*union semun arg */);
SEMAPHORES
• The cmd argument specifies one of the following ten
commands to be performed on the specified set
• The five command that refer to one particular
semaphore use semnum to specify one member of
the set
• The value of semnum is between 0 and nsems-1
union semun {
int val; // SETVAL
struct semid_ds *buf; // IPC_STAT/IPC_SET
unsigned short *array; // GETALL/SETALL
}
SEMAPHORES
• IPC_STAT: Fetch semid_ds structure for set
• IPC_SET: Set structure fields
• IPC_RMID: Remove semaphore from the system.
Removal is immediate. Any other process using the
semaphore will get an error on next attempted
operation
• GETVAL: Return value of semval for semnum
• SETVAL: Set value of semval for semnum
• GETPID: Return value of sempid for semnum
SEMAPHORES
• GETCNT: Return value of semncnt for semnum
• GETZCBT: Get value of semzcnt for semnum
• GETALL: Fetch all semaphore values in the set
• SETALL Set all semaphore values in set
• For all GET commands other than GETALL, the
function returns the corresponding value
• For the remaining commands, the return value is 0
• The function semop atomically performs an array
of operation on a semaphore set
SEMAPHORES
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
int semop(int semid, struct sembuf
semoparray[], size_t nops);
Return 0 if OK, 1 on error
struct sembuf {
unsigned short sem_num; // Member # in set
short sem_op; // Operation
short sem_flg; // IPC_NOWAIT/SEM_UNDO
}
SEMAPHORES
• nops arguments specifies the number of operation
(elements) in the array
• If sem_op is:
• Positive: semaphore value is added by the value
of sem_op (release)
• 0: block until value becomes equal to 0
• Negative: if semaphore value is greater than or
equal to abs(sem_op), semaphore is
subtracted. Otherwise block until we can do this
(wait)
SEMAPHORES
• All blocking operations will fail if semaphore is
removed (ERMID) or signal is caught (EINTR)
• If flag is set to IPC_NOWAIT, semop never
blocks and returns with errno EAGAIN when an
operation was not successful
• Typical semaphore usage:
Access semaphore
semop() with sem_op = -1
Use resource
semop() with sem_op = 1
Access semaphore
semop() with sem_op = -1
Use resource
semop() with sem_op = 1
Create semaphore
Set semaphore value to 1
SHARED MEMORY
• This is an area of memory that can be used by one or
more processes
• One has to be careful in synchronising access to a given
region by several processes
• SHMMAX: max number of bytes in shared memory
segment
• SHMMIN: min number of byes in shared memory
segment
• SHMMNI: max number of system-wide shared memory
segments
• SHMSEG: max number of shared memory segments per
process
SHARED MEMORY
• Size is the minimum size of the shared memory
segment desired (generally rounded up to page
size)
• If a new segment is being created we must specify
size, client can specify a size of 0
• When a new segment is created the contents of the
segment are initialised with zeros
#include <sys/shm.h>
int shmget(key_t key, int size, int flag);
Return shm ID or -1 on error
SHARED MEMORY
• cmd specifies one of the following commands:
• IPC_STAT: Fetch shmid_ds structure for set
• IPC_SET: Set structure fields
• IPC_RMID: Remove shared memory segment
from system. Attachment count is maintained for
shared memory segments, segment is not removed
until last process detaches it
#include <sys/shm.h>
int shmctl(int shimd, int cmd,
struct shmid_ds *buf)
Return 0 if OK, -1 on error
SHARED MEMORY
• Two additional commands are provided for Linux
and Solaris implementations:
• SHM_LOCK: lock the shared memory segment
in memory. Can be executed only by superuser
• SHM_UNLOCK: Unlock shared memory
segment. Can be executed only by superuser
• Once a shared memory segment is created a
process attaches it to its address space by using
shmat
SHARED MEMORY
• Address in the calling process at which the segment
is attached depends on the addr argument and
whether SHM_RND is specified in flag
• If addr is 0, segment is attached at the first
available address selected by the kernel
(recommended)
#include <sys/shm.h>
void *shmat(int shmid, const void *addr,
int flag);
Return pointer to shared memory
segment if OK, 1 on error
SHARED MEMORY
• If addr is nonzero and SHM_RND is not
specified, segment is attached at the address
given by addr
• If addr is nonzero and SHM_RND is specified,
segment is attached at the address given by
(addr – (addr % SHMLBA). SHMLBA is
the low boundary address multiple and is
always a power of 2. This rounds the address
down to the next multiple of SHMLBA
• It is unadvisable to specify and address
SHARED MEMORY
• When we’re done with a shared memory
segment we use shmdt to detach it
• The addr argument is the value that was
returned by a previous call to shmat
• If successful, shmd will decrement the attachment
counter in the associated shmid_ds structure
#include <sys/shm.h>
void *shmdt(void *addr);
Return 0 if OK, 1 on error

More Related Content

What's hot

Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
YourHelper1
 
IPC mechanisms in windows
IPC mechanisms in windowsIPC mechanisms in windows
IPC mechanisms in windows
Vinoth Raj
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/O
YourHelper1
 
Lecture 9 -_pthreads-linux_threads
Lecture 9 -_pthreads-linux_threadsLecture 9 -_pthreads-linux_threads
Lecture 9 -_pthreads-linux_threads
Prashant Pawar
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
YourHelper1
 
Process, Threads, Symmetric Multiprocessing and Microkernels in Operating System
Process, Threads, Symmetric Multiprocessing and Microkernels in Operating SystemProcess, Threads, Symmetric Multiprocessing and Microkernels in Operating System
Process, Threads, Symmetric Multiprocessing and Microkernels in Operating SystemLieYah Daliah
 
Pthread
PthreadPthread
Pthread
Gopi Saiteja
 
127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux
Sam Bowne
 
NTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC PresentationNTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC Presentation
Muhamad Hesham
 
Pipes in Windows and Linux.
Pipes in Windows and Linux.Pipes in Windows and Linux.
Pipes in Windows and Linux.
Junaid Lodhi
 
CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 8: Windows overflows (Part 1)CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 8: Windows overflows (Part 1)
Sam Bowne
 
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: ShellcodeCNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: Shellcode
Sam Bowne
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
QUONTRASOLUTIONS
 
Linux Programming
Linux ProgrammingLinux Programming
Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
Viên Mai
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
Amir Payberah
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
Tushar B Kute
 

What's hot (20)

Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
IPC mechanisms in windows
IPC mechanisms in windowsIPC mechanisms in windows
IPC mechanisms in windows
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/O
 
Lecture 9 -_pthreads-linux_threads
Lecture 9 -_pthreads-linux_threadsLecture 9 -_pthreads-linux_threads
Lecture 9 -_pthreads-linux_threads
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
 
Process, Threads, Symmetric Multiprocessing and Microkernels in Operating System
Process, Threads, Symmetric Multiprocessing and Microkernels in Operating SystemProcess, Threads, Symmetric Multiprocessing and Microkernels in Operating System
Process, Threads, Symmetric Multiprocessing and Microkernels in Operating System
 
Pthread
PthreadPthread
Pthread
 
Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux
 
NTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC PresentationNTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC Presentation
 
Pipes in Windows and Linux.
Pipes in Windows and Linux.Pipes in Windows and Linux.
Pipes in Windows and Linux.
 
CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 8: Windows overflows (Part 1)CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 8: Windows overflows (Part 1)
 
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: ShellcodeCNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: Shellcode
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
Unit 7
Unit 7Unit 7
Unit 7
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
 
Unix kernal
Unix kernalUnix kernal
Unix kernal
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 

Viewers also liked

Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
HelpWithAssignment.com
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
HelpWithAssignment.com
 
Factorial Experiments
Factorial ExperimentsFactorial Experiments
Factorial Experiments
HelpWithAssignment.com
 
Network Programming Assignment Help
Network Programming Assignment HelpNetwork Programming Assignment Help
Network Programming Assignment Help
HelpWithAssignment.com
 
Game theory Bayesian Games at HelpWithAssignment.com
Game theory Bayesian Games at HelpWithAssignment.comGame theory Bayesian Games at HelpWithAssignment.com
Game theory Bayesian Games at HelpWithAssignment.comHelpWithAssignment.com
 
Tips for writing a good biography
Tips for writing a good biographyTips for writing a good biography
Tips for writing a good biography
HelpWithAssignment.com
 
Get 24/7 Reliable Engineering Assignment Help, 100% error free, money back g...
Get 24/7 Reliable Engineering  Assignment Help, 100% error free, money back g...Get 24/7 Reliable Engineering  Assignment Help, 100% error free, money back g...
Get 24/7 Reliable Engineering Assignment Help, 100% error free, money back g...
HelpWithAssignment.com
 
Cash Dividend Assignment Help
Cash Dividend Assignment Help Cash Dividend Assignment Help
Cash Dividend Assignment Help
HelpWithAssignment.com
 
Manufacturing Process Selection and Design
Manufacturing Process Selection and DesignManufacturing Process Selection and Design
Manufacturing Process Selection and Design
HelpWithAssignment.com
 
Scoping
ScopingScoping
Scope
ScopeScope
System Programming Assignment Help- Signals
System Programming Assignment Help- SignalsSystem Programming Assignment Help- Signals
System Programming Assignment Help- Signals
HelpWithAssignment.com
 
Customer relationship management
Customer relationship managementCustomer relationship management
Customer relationship management
HelpWithAssignment.com
 

Viewers also liked (14)

Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
 
Factorial Experiments
Factorial ExperimentsFactorial Experiments
Factorial Experiments
 
Network Programming Assignment Help
Network Programming Assignment HelpNetwork Programming Assignment Help
Network Programming Assignment Help
 
Game theory Bayesian Games at HelpWithAssignment.com
Game theory Bayesian Games at HelpWithAssignment.comGame theory Bayesian Games at HelpWithAssignment.com
Game theory Bayesian Games at HelpWithAssignment.com
 
Tips for writing a good biography
Tips for writing a good biographyTips for writing a good biography
Tips for writing a good biography
 
Get 24/7 Reliable Engineering Assignment Help, 100% error free, money back g...
Get 24/7 Reliable Engineering  Assignment Help, 100% error free, money back g...Get 24/7 Reliable Engineering  Assignment Help, 100% error free, money back g...
Get 24/7 Reliable Engineering Assignment Help, 100% error free, money back g...
 
Cash Dividend Assignment Help
Cash Dividend Assignment Help Cash Dividend Assignment Help
Cash Dividend Assignment Help
 
Manufacturing Process Selection and Design
Manufacturing Process Selection and DesignManufacturing Process Selection and Design
Manufacturing Process Selection and Design
 
Scoping
ScopingScoping
Scoping
 
Scope
ScopeScope
Scope
 
System Programming Assignment Help- Signals
System Programming Assignment Help- SignalsSystem Programming Assignment Help- Signals
System Programming Assignment Help- Signals
 
Customer relationship management
Customer relationship managementCustomer relationship management
Customer relationship management
 

Similar to System Programming - Interprocess communication

ipc.pptx
ipc.pptxipc.pptx
ipc.pptx
SuhanB
 
IPC
IPCIPC
Inter Process Communication - IPC
Inter Process Communication - IPCInter Process Communication - IPC
Inter Process Communication - IPC
Pravjot Singh
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows VistaTrinh Phuc Tho
 
Introduction to System Calls
Introduction to System CallsIntroduction to System Calls
Introduction to System Calls
Vandana Salve
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 
MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
SRINIVAS KOLAPARTHI
 
Ch03 processes
Ch03 processesCh03 processes
Ch03 processes
Nazir Ahmed
 
Linux Inter Process Communication
Linux Inter Process CommunicationLinux Inter Process Communication
Linux Inter Process Communication
Abhishek Sagar
 
Elixir
ElixirElixir
Operating Systems & Applications
Operating Systems & ApplicationsOperating Systems & Applications
Operating Systems & Applications
Maulen Bale
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CanSecWest
 
operating system calls input and output by (rohit malav)
operating system calls input and output by (rohit malav)operating system calls input and output by (rohit malav)
operating system calls input and output by (rohit malav)
Rohit malav
 
Usp message queues
Usp message queuesUsp message queues
Usp message queues
RohitK71
 
Chapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux KernelChapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux Kernel
Dr.Ashvini Chaudhari Bhongade
 
Process & Mutlithreading
Process & MutlithreadingProcess & Mutlithreading
Process & Mutlithreading
Rahul Jamwal
 

Similar to System Programming - Interprocess communication (20)

ipc.pptx
ipc.pptxipc.pptx
ipc.pptx
 
IPC
IPCIPC
IPC
 
IPC
IPCIPC
IPC
 
Inter Process Communication - IPC
Inter Process Communication - IPCInter Process Communication - IPC
Inter Process Communication - IPC
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Introduction to System Calls
Introduction to System CallsIntroduction to System Calls
Introduction to System Calls
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
 
4 threads
4 threads4 threads
4 threads
 
Ch03 processes
Ch03 processesCh03 processes
Ch03 processes
 
Linux Inter Process Communication
Linux Inter Process CommunicationLinux Inter Process Communication
Linux Inter Process Communication
 
Elixir
ElixirElixir
Elixir
 
Threads
ThreadsThreads
Threads
 
Operating Systems & Applications
Operating Systems & ApplicationsOperating Systems & Applications
Operating Systems & Applications
 
Os concepts
Os conceptsOs concepts
Os concepts
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
 
operating system calls input and output by (rohit malav)
operating system calls input and output by (rohit malav)operating system calls input and output by (rohit malav)
operating system calls input and output by (rohit malav)
 
Usp message queues
Usp message queuesUsp message queues
Usp message queues
 
Chapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux KernelChapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux Kernel
 
Process & Mutlithreading
Process & MutlithreadingProcess & Mutlithreading
Process & Mutlithreading
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 

System Programming - Interprocess communication

  • 2. INTERPROCESS COMMUNICATION • IPC allows different processes to communicate between themselves • So far, processes could communicate using fork/child inheritance, passing arguments in exec calls, through the file system and using signals • There are more structures which allows proceses to communicate more efficiently • IPC structures provide different flavors of communication • Some can only be used between realated processes (fork/child). Unnamed structures
  • 3. INTERPROCESS COMMUNICATION • Named structures can be used by anyone having access rights • System V IPC structures follow the same access protocol, but some extra form of initial communication is necessary for the processes to use them • Different implementations might support one type of IPC and not another • Some structures are handled differently between implementations
  • 4. PIPES • Oldest and most widely implemented form of IPC • Data can flow in one direction (half duplex) • Pipes must be used in related processes since their identifier is the file descriptor • pipe creates a pipe and places 2 file descriptors inside fd. fd[0] is opened for reading and fd[1] for writing • Pipes work in a FIFO fashion #include <unistd.h> int pipe(int fd]); Return 0 if OK, 1 on error
  • 5. PIPES • fstat return st_mode of FIFO which can be tested by the S_ISFIFO macro • We use normal read, write and close operations to access pipes • If we read from a pipe whose write end has been closed, read returns 0, showing an end of file • If we write to a pipe whose read end has been closed, write returns -1 with errono EPIPE, and SIGPIPE is generated • The constant PIPE_BUF gives the maximum amount of bytes that can be written in one go without interleaving between different writers
  • 6. PIPES • Within one process, a pipe is useless • We normally fork a child process and then close one side of the parent and close the other side in the child
  • 7. FIFOS • FIFOs are just like normal files but they behave like pipes with a pathname • FIFOs are full duplex and more than one process can open a FIFO • Other unrelated processes can access FIFOs using normal open, read, write and close system calls • To remove a FIFO file, call unlink • Normal access file permissions apply. The stat function returns the type as FIFO like pipes (in ls marked with a ‘p’)
  • 8. FIFOS • If we write to a FIFO which no process has opened for reading, SIGPIPE is generated • When no writer exists, a read returns 0 for EOF • A FIFO can be created with mkfifo • mode in mkfifo is the same as mode in open for file access permissions #include <sys/stat.h> int mkfifo(const char* path, mode_t mode); Return 0 if OK, 1 on error
  • 9. FIFOS • When we open a FIFO, the O_NONBLOCK flag affects what happens: • If not specified, an open for read-only blocks until a two way communication exists • If specified an open for read-only returns immediately. An open for write-only returns with error -1 and errno ENXIO unless another process has opened the FIFO for reading • If we write to a FIFO that no process has opened for reading the signal SIGPIPE is generated
  • 10. XSI IPC • The three types of IPC referred to as XSI IPC (message queues, semaphores and shared memory) have many similar features • All these structures use the same access protocol • Each IPC has a key associated with it • This key is global to the whole system (kernel) • Two processes using the same key will be given access to the same IPC structure • In supplying the key an identifier is returned, which is then used to operate in the IPC. This identifier is unique to the whole system too
  • 11. XSI IPC • Keys and identifiers will run out in the system if no IPC structures are removed • A special key exists: IPC_PRIVATE which is guaranteed to be an unused key • The identifier or key must somehow be passed between different processes using the same IPC structure • Keys or identifiers can be passed between processes using the filesystem, command line values, an agreed upon header, parent/child inheritance or using ftok.
  • 12. XSI IPC • ftok converts an agreed upon pathname and project ID into an IPC key • The general format to obtain an identifier is Xget(key, flag) • If a key is passed the second process will have to ‘re- create’ the IPC structure, if the identifier is passed, second process will access the IPC using this identifier #include <sys/types.h> #include <sys/ipc.h> key_t ftok(char *pathname, char projID);
  • 13. XSI IPC • A new IPC structure will be created if the key is equal to IPC_PRIVATE or a new key is given and IPC_CREAT is specified in flag (in Xget) • Using IPC_EXCL with IPC_CREAT in flag will produce and error with errno set to EEXIST if an IPC already exists with the same key • When creating an IPC, access permissions are given in flag of Xget
  • 14. XSI IPC Permission Message Queues Semaphore Shared Memory user-read user-write MSG_R MSG_W SEM_R SEM_A SHM_R SHM_W group-read group-write MSG_R >> 3 MSG_W >> 3 SEM_R >> 3 SEM_A >> 3 SHM_R >> 3 SHM_W >> 3 other-red other-write MSG_R >> 6 MSG_W >> 6 SEM_R >> 6 SEM_A >> 6 SHM_R >> 6 SHM_W >> 6
  • 15. MESSAGE QUEUES • This is a linked list of messaged stored within the kernel • msgget opens an existing queue, or creates a new one, and returns the IPC queue identifier • msgctl is used to remove the structure created by the same effective user. Removal is immediate and anyone using the structure will get an error EIDRM #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgget(key_t, int flag) Returns message queue ID, -1 on error int msgctl(int msgid, IPC_RMID, NULL) Returns -1 on error
  • 16. MESSAGE QUEUES • Each queue has the structure above associated with it • It defines the current status of the queue struct msqid_ds { struct ipc_perm msg_perm; msgqnum_t msg_qnum; msglen_t msg_qbytes; pid_t msg_lspid; pid_t msg_lrpid; time_t msg_stime; time_t msg_rtime; time_t msg_ctime; . . . }
  • 17. MESSAGE QUEUES • Some limits exist for message queues: • MSGMAX: largest message allowed in bytes • MSGMNB: max size of a queue in bytes • MSGMNI: max number of queues, system wide • MSGTQL: max number of messages, system wide • Each message consists of a positive long value specifying the message type followed by data • Messages are always places at the end of the queue
  • 18. MESSAGE QUEUES • The cmd argument specifies the command to be performed on the queue: • IPC_STAT: Fetch msqid_ds structure of the queue • IPC_SET: Set some structure values to queue • IPC_RMID: Remove message queue from the system and any data still on the queue #include <sys/msg.h> int msgctl(int msgid, int cmd, struct, msqid_ds *buf) Returns -1 on error
  • 19. MESSAGE QUEUES • The ptr argument points to a long integer that contains the positive integer message type and is immediately followed by the data. We can use a struct like: #include <sys/msg.h> int msgsnd(int msqid, const void* ptr, size_t nbytes, int flag); Returns -1 on error struct mymesg { long mtype; // Positive message type char mtext[512]; // Message data }
  • 20. MESSAGE QUEUES • A flag of IPC_NOWAIT can be specified • If message queue is full causes msgsnd to return immediately with EAGAIN • If not specified, call is blocked until there is room, queue is removed or signal is caught • Since a reference count is not maintained with each message queue the removal of a queue simply generates errors on the next queue operation • When msgsnd returns successfully, the msqid_ds structure is updated
  • 21. MESSAGE QUEUES • If returned message is larger than nbytes and MSG_NOERROR is set in flag, the message is truncated (without warning) • Otherwise E2BIG is returned • type argument lets us specify which message we want #include <sys/msg.h> int msgrcv(int msqid, void* ptr, size_t nbytes, int flag); Returns -1 on error
  • 22. MESSAGE QUEUES • type == 0: the first message on the queue is returned • type > 0: first message on the queue whose message type equals type is returned • type < 0: first message whose message type is the lowest value less than or equal to abs(type) is returned • A nonzero type is used to read the messages in an order other than first in, first out • IPC_NOWAIT will make the operation non- blocking, ENOSMG if message type doesn’t exist
  • 23. POSIX MESSAGE QUEUES • Much nicer interface • Queues are reference counted (only removed when all processes are detached from it) • Visible on a pseudo-filesystem • Notion of message priority • Message notification system, where processes can register to receive notifications when a new message arrived on an empty queue (action is signal or start new thread)
  • 24. SEMAPHORES • Counter used to provide access to shared data for multiple processes • When a semaphore is positive a process decrements the semaphore by 1 signifying it has used up one resource • When the semaphore is 0 it means the resource is not available and the process blocks until the resource is available • Testing and decrementing is done atomically to guarantee functionality
  • 25. SEMAPHORES • Semaphores are created in sets with each set holding one or more semaphores • The are limits affecting semaphores: • SEMVMX: max value of semaphore • SEMMNI: max number of system-wide semaphore sets • SEMMNS: max number of system-wide semaphores • SEMMSL: max number of semaphores per set • SEMOPM: max number of operations per semop call
  • 26. SEMAPHORES • XSI semaphores are a complicated implementation of semaphores: • The use of semaphore sets instead of single independent ones • The creation and initialisation of semaphores are independent (not performed atomically) • We have to worry about a program that terminates without releasing its allocated semaphores • The kernel maintains a similar structure to msqid_ds for each semaphore and semaphore set
  • 27. SEMAPHORES struct semi_ds { struct ipc_perm sem_perm; unsigned short sem_nsems; timet_t sem_otime; // last semop time timet_t sem_ctime // last-change time . . . } struct { unsigned short semval; // Semaphore value pid_t sempid; // pid of last operation unsigned short semcnt; // #procs semval>curval unsigned short semzcnt; // #rpocs semval==0 }
  • 28. SEMAPHORES • Obtain a semaphore ID with specified number of semaphores in set • If referencing an existing set, nsems can be 0 • semctl is a catchall for various semaphore implementations #include <sys/sem.h> int semget(key_t, int nsems, int flags); Returns ID is OK, 1 on error int semctl(int semid, int semnum, int cmd, ... /*union semun arg */);
  • 29. SEMAPHORES • The cmd argument specifies one of the following ten commands to be performed on the specified set • The five command that refer to one particular semaphore use semnum to specify one member of the set • The value of semnum is between 0 and nsems-1 union semun { int val; // SETVAL struct semid_ds *buf; // IPC_STAT/IPC_SET unsigned short *array; // GETALL/SETALL }
  • 30. SEMAPHORES • IPC_STAT: Fetch semid_ds structure for set • IPC_SET: Set structure fields • IPC_RMID: Remove semaphore from the system. Removal is immediate. Any other process using the semaphore will get an error on next attempted operation • GETVAL: Return value of semval for semnum • SETVAL: Set value of semval for semnum • GETPID: Return value of sempid for semnum
  • 31. SEMAPHORES • GETCNT: Return value of semncnt for semnum • GETZCBT: Get value of semzcnt for semnum • GETALL: Fetch all semaphore values in the set • SETALL Set all semaphore values in set • For all GET commands other than GETALL, the function returns the corresponding value • For the remaining commands, the return value is 0 • The function semop atomically performs an array of operation on a semaphore set
  • 32. SEMAPHORES #include <sys/types.h> #include <sys/sem.h> #include <sys/ipc.h> int semop(int semid, struct sembuf semoparray[], size_t nops); Return 0 if OK, 1 on error struct sembuf { unsigned short sem_num; // Member # in set short sem_op; // Operation short sem_flg; // IPC_NOWAIT/SEM_UNDO }
  • 33. SEMAPHORES • nops arguments specifies the number of operation (elements) in the array • If sem_op is: • Positive: semaphore value is added by the value of sem_op (release) • 0: block until value becomes equal to 0 • Negative: if semaphore value is greater than or equal to abs(sem_op), semaphore is subtracted. Otherwise block until we can do this (wait)
  • 34. SEMAPHORES • All blocking operations will fail if semaphore is removed (ERMID) or signal is caught (EINTR) • If flag is set to IPC_NOWAIT, semop never blocks and returns with errno EAGAIN when an operation was not successful • Typical semaphore usage: Access semaphore semop() with sem_op = -1 Use resource semop() with sem_op = 1 Access semaphore semop() with sem_op = -1 Use resource semop() with sem_op = 1 Create semaphore Set semaphore value to 1
  • 35. SHARED MEMORY • This is an area of memory that can be used by one or more processes • One has to be careful in synchronising access to a given region by several processes • SHMMAX: max number of bytes in shared memory segment • SHMMIN: min number of byes in shared memory segment • SHMMNI: max number of system-wide shared memory segments • SHMSEG: max number of shared memory segments per process
  • 36. SHARED MEMORY • Size is the minimum size of the shared memory segment desired (generally rounded up to page size) • If a new segment is being created we must specify size, client can specify a size of 0 • When a new segment is created the contents of the segment are initialised with zeros #include <sys/shm.h> int shmget(key_t key, int size, int flag); Return shm ID or -1 on error
  • 37. SHARED MEMORY • cmd specifies one of the following commands: • IPC_STAT: Fetch shmid_ds structure for set • IPC_SET: Set structure fields • IPC_RMID: Remove shared memory segment from system. Attachment count is maintained for shared memory segments, segment is not removed until last process detaches it #include <sys/shm.h> int shmctl(int shimd, int cmd, struct shmid_ds *buf) Return 0 if OK, -1 on error
  • 38. SHARED MEMORY • Two additional commands are provided for Linux and Solaris implementations: • SHM_LOCK: lock the shared memory segment in memory. Can be executed only by superuser • SHM_UNLOCK: Unlock shared memory segment. Can be executed only by superuser • Once a shared memory segment is created a process attaches it to its address space by using shmat
  • 39. SHARED MEMORY • Address in the calling process at which the segment is attached depends on the addr argument and whether SHM_RND is specified in flag • If addr is 0, segment is attached at the first available address selected by the kernel (recommended) #include <sys/shm.h> void *shmat(int shmid, const void *addr, int flag); Return pointer to shared memory segment if OK, 1 on error
  • 40. SHARED MEMORY • If addr is nonzero and SHM_RND is not specified, segment is attached at the address given by addr • If addr is nonzero and SHM_RND is specified, segment is attached at the address given by (addr – (addr % SHMLBA). SHMLBA is the low boundary address multiple and is always a power of 2. This rounds the address down to the next multiple of SHMLBA • It is unadvisable to specify and address
  • 41. SHARED MEMORY • When we’re done with a shared memory segment we use shmdt to detach it • The addr argument is the value that was returned by a previous call to shmat • If successful, shmd will decrement the attachment counter in the associated shmid_ds structure #include <sys/shm.h> void *shmdt(void *addr); Return 0 if OK, 1 on error