owned this note
owned this note
Published
Linked with GitHub
# IPC-PIPE
###### tags: <`twlkh`>, <`yenWu`>, <`IPC`>
閱讀書籍:`Linux `
# Inter Process Communication 簡介
IPC 在現今 muti-threads, muti-processes 裡扮演了很重要的腳色,如果無法通訊可能就等於無法合作
## IPC 分類

>主要分為 通訊 和 同步
* 通訊
>- [ ] 一語以蔽之就是"溝通",當然溝通的方法有很多種,ex 寄信(資料傳遞)、留言板(共享空間)
> 通訊也分為兩種 傳遞資料 和 共用空間
* 資料傳遞
> 傳遞資料也分兩種=.=,
* 共享空間
> 訊息交換
* 同步
# Pipe
### 自言自語-pipe
> [練習程式碼](https://github.com/c14006078/PracticeMakePerfect/blob/master/pipe/f_pipe.c)[name=Yen-Kuan Wu]

----
### 父子行程溝通-pipe
> [練習程式碼](https://github.com/c14006078/PracticeMakePerfect/blob/master/pipe/fork_pipe.c)[name=Yen-Kuan Wu]

----

---
shell 的 pipe
`ls | grep myfile`
解釋圖
`ls -> stdout`
`stdin -> grep myfile -> myfile`
`ls -> stdout "pipe to" stdin -> grep myfile -> stdout`
# Linux kernel 2.4.37
[include/linux/fs/pipe_fs_i.h](http://lxr.free-electrons.com/source/include/linux/pipe_fs_i.h?v=2.4.37)
[fs/pipe.c](http://lxr.free-electrons.com/source/fs/pipe.c?v=2.4.37)
fs/fifo.c
# fd in process

# Pipe location in process

## Pipe in Linux kernel
```clike=
const struct file_operations read_pipefifo_fops = {
.llseek = no_llseek,
.read = do_sync_read,
.aio_read = pipe_read,
.write = bad_write,
.poll = pipe_poll,
.unlocked_ioctl = pipe_ioctl,
.open = pipe_read_open,
.release = pipe_read_release,
.fasync = pipe_read_fasync,
}
```
# pipefs
* Every thing is a file, so is `pipe`
* Every file belongs to one filesytem, so does `pipe`
* `pipefs` maintain all pipes
PipeFS
>PipeFS is a unique virtual filesystem. This filesystem is mounted inside the kernel rather than in the userspace. While most filesystems are mounted under "/", PipeFS is mounted on "pipe:", making PipeFS its own root (yes, a second root filesystem). This filesystem is one superblock and cannot exceed that amount system-wide. The entry point of this filesystem/second-root is the system-call "pipe()". Unlike the other virtual/pseudo filesystems, this one cannot be viewed.
>Many of you may be wondering what purpose this PipeFS filesystem serves. Unix pipes (simply called pipes) use this filesystem. When a pipe is used (like this - ls | less), the pipe() system-call makes a new pipe object on this filesystem. Without this filesystem, pipes cannot be made. Also, threads and forks communicate together via pipes. Without PipeFS, processes could not fork and threads could not communicate. Network pipes also rely on this virtual/pseudo filesystem.
## kernel/fs/pipe.c
# FIFO
* "FIFO" is also called "name pipe"
# Solaris CH14
