LU6 Advanced IPC Flashcards
What does IPC stand for?
Inter-Process Communication
What are the three primary mechanisms for Advanced IPC?
- Message Passing
-
Semaphores: a counter used to provide access to a shared data object
for multiple processes. - Shared Memory
Name one function related to Message Queues?
msgget(), msgsnd(), msgrcv(), msgctl()
- msgget()
- Creates a new message queue or gets a handle to an existing queue.
- msgsnd()
- Sends a message to a specific message queue.
- msgrcv()
- Receives a message from a specific message queue.
- msgctl()
- Performs control operations on a message queue (e.g., delete the queue, get queue status, set queue attributes).
What function is used to obtain a message queue identifier?
msgget()
If key
represents an existing queue, what should permflags be in msgget?
int msgget(key_t key, int permflags)
0 (zero)
What is the purpose of the IPC_CREAT flag in msgget?
Create a message queue if does not exist
What happens if you use IPC_CREAT and IPC_EXCL in msgget, but the queue already exists?
msgget() fails and returns -1
Explain what a ‘message queue’ is in the context of IPC.
A linked list of messages stored within the kernel identified by a message queue identifier.
Briefly describe the role of ‘message passing’ in IPC.
- Processes communicate by exchanging messages through message queues.
- Message passing can be synchronous (blocking) and asynchronous (non-blocking).
- Asynchronous message passing allow IPC without shared memory. This can prevent race condition, since processes don’t directly access each other’s memory.
What is the purpose of msgsnd()
?
Send a message to the message queue
What are the parameters of msgsnd()
?
-
mqid
(queue ID) -
*message
(pointer to a struct message) -
size
(message size) -
flags
(IPC_NOWAIT)
What is contained in a mymesg
struct? (message structure)
long mtype (message type) and char mtext[512] (message data)
What is the significance of the mtype
field in a message structure?
It categorizes messages.
In msgsnd(), what does flags
control?
Behaviour of the msgsnd()
function.
flags can modify how the message is placed in the queue. For instance, IPC_NOWAIT
allows the sending process to continue even if the message queue is full, resulting in an error (EAGAIN) instead of blocking.
What happens in msgsnd()
if IPC_NOWAIT is set and the message cannot be sent?
The function returns immediately with -1.
What is the purpose of msgrcv()
?
Receive a message from the message queue.
What are the parameters of msgrcv()
?
-
mqid
(queue ID) -
*message
(pointer to receive buffer) -
size
(max message size) msg_type
-
flags
(IPC_NOWAIT / MSG_NOERROR)
MSG_NOERROR: message is truncated if longer than the length defined in
the 3rd argument (size)
What does a msg_type
of 0 mean when using msgrcv()
?
Receive the oldest message (FIFO).
If msg_type
is positive, what happens in msgrcv()
?
Only messages with a matching mtype
value are received.
How can you receive the lowest mtype
in a message queue with msgrcv()
?
Use a negative value for msg_type
, representing the absolute value.
Explain the MSG_NOERROR flag in msgrcv()
.
Truncates the message if it’s longer than the specified size.
What is msgctl()
used for?
Message control operations (get info, change limits, delete queue).
Name three purposes of msgctl()
.
[ GCD ]
- Get info
- Change limits (max bytes allowed in queue)
- Delete queue.
What are the arguments for msgctl()
?
mqid, cmd, struct msqid_ds *buf
-
msqid
identifies the queue to operate on. -
cmd
specifies what operation to perform (get status, set attributes, or remove). (IPC_STAT, IPC_SET, IPC_RMID) -
buf
provides a way to exchange information between the program and the kernel about the message queue’s status and attributes.