LU6 Advanced IPC Flashcards

1
Q

What does IPC stand for?

A

Inter-Process Communication

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three primary mechanisms for Advanced IPC?

A
  • Message Passing
  • Semaphores: a counter used to provide access to a shared data object
    for multiple processes.
  • Shared Memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name one function related to Message Queues?

A

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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What function is used to obtain a message queue identifier?

A

msgget()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If key represents an existing queue, what should permflags be in msgget?

int msgget(key_t key, int permflags)

A

0 (zero)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of the IPC_CREAT flag in msgget?

A

Create a message queue if does not exist

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if you use IPC_CREAT and IPC_EXCL in msgget, but the queue already exists?

A

msgget() fails and returns -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain what a ‘message queue’ is in the context of IPC.

A

A linked list of messages stored within the kernel identified by a message queue identifier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Briefly describe the role of ‘message passing’ in IPC.

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of msgsnd()?

A

Send a message to the message queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the parameters of msgsnd()?

A
  • mqid (queue ID)
  • *message (pointer to a struct message)
  • size (message size)
  • flags (IPC_NOWAIT)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is contained in a mymesg struct? (message structure)

A

long mtype (message type) and char mtext[512] (message data)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the significance of the mtype field in a message structure?

A

It categorizes messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In msgsnd(), what does flags control?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens in msgsnd() if IPC_NOWAIT is set and the message cannot be sent?

A

The function returns immediately with -1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of msgrcv()?

A

Receive a message from the message queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the parameters of msgrcv()?

A
  • 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does a msg_type of 0 mean when using msgrcv()?

A

Receive the oldest message (FIFO).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If msg_type is positive, what happens in msgrcv()?

A

Only messages with a matching mtype value are received.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How can you receive the lowest mtype in a message queue with msgrcv()?

A

Use a negative value for msg_type, representing the absolute value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Explain the MSG_NOERROR flag in msgrcv().

A

Truncates the message if it’s longer than the specified size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is msgctl() used for?

A

Message control operations (get info, change limits, delete queue).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Name three purposes of msgctl().

A

[ GCD ]

  • Get info
  • Change limits (max bytes allowed in queue)
  • Delete queue.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are the arguments for msgctl()?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does the `cmd` argument do in `msgctl()`?
Specifies the control operation to be performed.
26
What is the use of the IPC_STAT command in `msgctl()`?
Get information and save to `msq_stat`.
27
What does the IPC_SET command do in `msgctl()`?
Set some of the member from msq_stat structure
28
What is the purpose of IPC_RMID in `msgctl()`?
Remove the message queue and set msq_stat to NULL.
29
Describe the relationship between `msgctl()` and the `msqid_ds` structure.
The `msgctl()` function uses the `msqid_ds` structure to **store or modify** information about the message queue.
30
What is included inside the `msqid_ds` struct?
[ PPTS ] * Ownership permissions * PIDs * Timestamps * statistics (number of messages, max bytes)
31
What are **semaphores** used for?
Semaphores is a **counter** Allow processes to access to a **shared data object** one at a time (synchronization). | Think of a restaurant with limited tables. ## Footnote Semaphore is simply a variable used to solve the **Critical Section** Problem and to achieve **process synchronization** in the multiprocessing environment.
32
Are semaphores a form of IPC similar to pipes or message queues?
No, semaphores are not a form of IPC but used for **resource management/locking**.
33
What are the two operations you can perform on semaphores?
* Wait (`p`/proberen) * Signal (`v`/verhogen)
34
Explain what the 'wait' operation on a semaphore does.
Decrements the semaphore value (if positive) or waits until it's positive. | Think of a restaurant with limited tables. ## Footnote Semaphore is simply a variable used to solve the **Critical Section** Problem and to achieve **process synchronization** in the multiprocessing environment.
35
What function is typically used to obtain a semaphore set identifier?
semget()
36
In the context of semaphores, what does the word 'proberen' (p-operation) mean?
* to **decrease** semaphore value (if positive) * to **wait** (blocked if )
37
Explain the purpose of the **signal** (`v`) operation in semaphore usage.
Increment for semaphore value (to proceed)
38
What condition must be met for a process to successfully acquire a shared resource protected by a semaphore?
The semaphore value must be positive.
39
Name one function **related** to Semaphores.
* `semget()` * `semop()` -> op=operation * `semctl()`
40
Describe what 'Shared Memory' is in the context of IPC.
A segment of physical memory shared between two or more processes.
41
What does `shmget()` do?
Return a **shared memory segment ID** after created a segment successfully.
42
What are the arguments for `shmget()`?
key, size, permflags
43
What is the purpose of the `key` argument in `shmget()`?
Identifies the shared memory segment to the system.
44
What does the `size` argument in `shmget()` specify?
Required **minimum** size (in bytes) of the memory segment.
45
What are three operations in Shared Memory?
shmget(), shmat(), shmdt(), shmctl() * `shmget()` * `shmat()` – **Attach** memory segment from physical memory to logical data space * `shmdt()` – **Detach** memory segment from the logical address space * `shmctl()` – Similar to msgctl()
46
Explain `shmat()`.
**Attach** memory segment from **physical** memory to **logical** data space
47
Explain `shmdt()`.
**Detach** the shared memory segment from the *process's address space*.
48
49
What are arguments for shmdt?
*addr : Pointer to memory
50
In `shmat()`, what happens when the `*daddr` argument is NULL?
The system chooses the address.
51
What is the purpose of the `shmflags` argument in `shmat()`?
Defines the shmat() behaviour
52
What does SHM_RDONLY signify when passed as shmflag?
Shared memory segment is read-only.
53
What does SHM_RND signify when passed as shmflag?
Round `daddr` to a page boundary. ``` void *shmat(int shmid, const void *daddr, int shmflags) ``` ## Footnote Requests that the shared memory segment be attached at an address that is a multiple of the page size, ensuring page alignment for efficiency and system compatibility.
54
What does `shmctl()` do?
Performs control operations on a shared memory segment (similar to msgctl()).
55
What are the possible return value of shmdt if failure?
-1
56
How many processes can be involved in IPC via shared memory?
Two or more.
57
What function should you call after finishing with a shared memory segment in a process?
shmdt()
58
True/False: Shared Memory is a direct communication method.
True
59
True/False: Semaphores are a resource management technique, NOT an IPC method.
True
60
True/False: Message Passing guarantees immediate message delivery.
False
61
What is a potential race condition issue in shared memory?
Processes accessing and modifying data simultaneously.
62
What would be a good reason to use shared memory over message passing?
Faster data transfer for large amounts of data.
63
If shmget fails, what is the typical return value?
-1
64
Can a message queue be accessed by processes running under different user accounts?
Yes, if permissions are set accordingly.
65
How do processes typically synchronize access to shared memory?
Using synchronization primitives like semaphores or mutexes.
66
If a process dies while holding a semaphore, what problems can that cause?
Deadlock.
67
What is the purpose of *buf in msgctl() when cmd is IPC_STAT?
Address of the struct where info about the queue is stored.
68
What header file(s) are required for msgctl() function?
* * *
69
If `msgctl(msqid, IPC_RMID, NULL)` is successful, what happens to the message queue?
The message queue is destroyed/removed.
70
What is one benefit of using IPC mechanisms over simple global variables for inter-process communication?
Protection from accidental access by other processes.
71
The 'p' or wait operation (proberen) on a semaphore can also be referred to as a ______ operation?
Decrement
72
The 'v' or signal operation (verhogen) on a semaphore can also be referred to as a ______ operation?
Increment
73
Which IPC is ideal when exchanging small, discrete packets of information?
Message passing
74
What happens if msg_type is set to -1 in `msgrcv()`?
Recieve the first message ## Footnote * `msg_type = 0`: message read is **FIFO** * `msg_type > 0`: message with **equal** value to mytype is received * `msg_type < 0`: message with **lowest** mtype number that is **less than or equal** to msg_type’s absolute value is received
75
What is a common technique to avoid race conditions in Shared Memory?
Using a Mutex or Semaphore
76
Which shared memory operations allows detaching the address space?
shmdt
77
What happens when msgctl is successful?
0
78
Which function allows modifying semaphores's operation?
semop()
79
Fill in the blanks: A semaphore is a _______ that provides access to a shared data object?
Counter
80
Which shared memory operations will get error if insufficient permissions?
shmat()
81
True or False: Message Passing is fast
**False** (shared memory is faster) ## Footnote **Shared memory**: Processes can direct access (kernel not involved) **Message passing**: involves system calls to send and receive (data copying) (need kernel)
82
If the semaphore has a negative value, what will happen when a process calls wait or P function?
The process will be blocked
83
If two processes use the **same Key**, what is the result?
They will be using the **same** memory **segment**
84
True or False: A shared memory is usually associated with a Semaphore?
True
85
Which flag in `shmat()` will result in the process accessing the Shared Memory as **read only**?
`SHM_RDONLY`
86
Shared Memory has race condition, how do we solve this problem?
Semaphore
87
Fill in the blanks, _______ can only ever change sem at any one time.
One process
88
Which parameter can you use in msgrcv() to read the oldest message regardless of type?
msg_type = 0
89
What will the system do if the semaphore is 0?
The process will sleep until the semaphore is bigger than 0.
90
How does **message queue** provide a way to communicate data between processes that may be running on **different machines** in a distributed system?
By **abstracting network details**, offering a simple send/receive message interface (higher-level).
91
Can shared memory be used for inter-process communication between processes written in different programming languages?
It depends
92
How does shared memory contribute to the performance optimization in concurrent programming?
Allowing processes to have **direct access** to the shared memory, **reducing the overhead of copying data between processes**.
93
Can processes from different user accounts access the same shared memory segment?
If it has correct permission settings
94
How can the size of a message queue affect the overall performance of the system?
A **larger** queue can buffer **more messages**, improving *throughput* during peak loads. However, **excessively large** queues consume more memory and can **increase latency** if messages are waiting too long. A **smaller** queue may lead to **message loss** or **blocking** if producers outpace consumers. Thus, queue size must be tuned for optimal performance.
95
When can msgrcv() block
When there are no messages in the queue and IPC_NOWAIT
96
Assume you are making a multithreaded program, should you be using IPC methods instead of normal function call to send data?
No
97
If size is 0 when creating the Shared Memory, what does that mean?
It is an existing Shared Memory
98
Can you have multiple message with different types in the queue?
Yes
99
Can two unrelated processes communicate via shared memory?
Yes
100
How to avoid deadlock when processes waiting for semaphore?
Implement a timeout
101
If shmdt() return -1, what does it mean
Failure
102
Which function is used to receive messages from a message queue?
msgrcv()
103
Which shared memory operations will allow deleting a shared memory?
shmctl()