###### tags: `operating system` `note` `thu`
# Chapter 3: Interprocess Communication
## 1. (Mach) Message passing (p. 141)
## 2. Pipeline (p. 146)
### a. Ordinary(老師上課講的)
### b. Named(老師沒講,可能不會考)
## 3. (POSIX) Shared Memory (p. 138)
### a. 建立shared memory的物件,並設定其存取模式(e.g.開檔flag, 權限)
```c
#include <sys/mman.h>
#include <sys/stat.h>
/*
* shm_open - create/open a shared memory object
*
* @param name: name of the shared memory object
* @param oflag: open flags (O_CREAT, O_EXCL, O_RDWR, etc.)
* @param mode: file permissions (ignored if object already exists)
*
* @return A file descriptor for the shared memory object on success,
* or -1 on failure.
*/
int shm_open(const char *name, int oflag, mode_t mode);
```
- 使用`shm_open()`的System Call來達成。
### b. 設定Shared Memory的大小
```c
#include <unistd.h>
/*
* ftruncate - truncate a file to a specified length
*
* Truncates the file associated with the file descriptor @fd to the
* specified length @length. If the file is extended, the extended
* portion is filled with zeros. If the file is shortened, any data
* beyond the new end-of-file is discarded.
*
* @param fd: file descriptor of the file to truncate
* @param length: new length of the file
* @return 0 on success, or -1 on failure.
*/
int ftruncate(int fd, off_t length);
```
- 使用`ftruncate()`的System Call來達成。
### c. 將建立好的Shared Memory檔案對應到指定的記憶體位址上。
```c
#include <sys/mman.h>
/*
* mmap - map files or devices into memory
*
* Maps the specified file or device into memory, creating a new mapping
* at the specified address @addr (or any available address if @addr is NULL).
* The @length parameter specifies the size of the mapping, and @prot specifies
* the protection flags for the mapping (read, write, execute, or any combination).
* The @flags parameter specifies additional flags for the mapping, such as whether
* it should be shared between processes or private to the calling process.
*
* If @fd is not -1, the mapping is associated with the file or device
* specified by the file descriptor @fd, and @offset specifies the
* offset within the file to begin the mapping. If @fd is -1, the
* mapping is not associated with any file, and is instead an anonymous
* mapping.
*
* @param addr: address of the memory region to map (NULL for any available address)
* @param length: size of the memory region to map
* @param prot: protection flags for the memory region (PROT_READ, PROT_WRITE, etc.)
* @param flags: additional flags for the mapping (MAP_SHARED, MAP_PRIVATE, etc.)
* @param fd: file descriptor of the file to map (ignored for anonymous mappings)
* @param offset: offset within the file to begin the mapping
*
* @return A pointer to the beginning of the mapped region on success,
* or MAP_FAILED on failure.
*/
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
```
- 使用`mmap()`的System Call來達成。
### d. 結束使用後,將shared memory移除。
```c
#include <sys/mman.h>
/*
* shm_unlink() removes the shared memory object specified by name. The
* object will be destroyed once all processes that have the object mapped
* close it.
*
* If no processes have the object mapped when shm_unlink() is called, the
* object is removed immediately and further references to it will fail.
* @param name: The name of the shared memory process.
*
* @return 0 on success, or -1 on error, with errno set appropriately.
*/
int shm_unlink(const char *name);
```
- 使用`shm_unlink()`的System Call來達成。
## 4. Socket (p. 153)
## Homework
### 1. fork() (p. 124, 125)
### 2. exec()
### 3. Shared Memory