Linux System Programming (2)

1. parent and child to communicate using file

To open a file, then fork out the child, the parent and child processes have the file descriptor. The parent process to the file written word, and then close the file descriptor. Then A second later after the child woke up, the child first file pointer to the initial location of the file, and then read the file and print output, and finally close the file descriptor.

schematic diagram:

Results of the:

2. The parent process fork process example of three sub

Mistakes:

 

Results of the:

Explanation: The first two sub-processes are normal exit, the last paragraph is a sub-process error has occurred, then the system will send a signal to the child process to kill the child process 11. Therefore, the results above.

3. waitpid function

        

NOTE: If after the child process is created, by some other process operations are sent to the group, then the parent process and the child process is not in the same set of process parameters needed pid <0, the sub-process pid add a minus sign. (Pid parameter is greater than 0 and equal to 1 in both the more commonly used)

Example: pid parameter is -1, nonblocking.

 

Explanation: When wpid equal to 0, indicating that the child process is running, then continue.

4. ipc concept

The duct related concepts

 

6. pipe using the function

Return Value: 0 if successful, -1 on failure.

Example:

Results of the:

7. The process of communication between blood fork position

fork position should be created after the pipe, so that the parent and child processes to use the same pipeline.

8. Use the match between parent and child communication practice ideas

ps aux default is written to the terminal, so we need to use dup2, so after a file descriptor redirection, ps aux will be written to the pipe, "write" the end. Similarly, grep bash default is read from the terminal, so we need to use dup2, so after a file descriptor redirection, grep bash pipeline will be "read" from the end to read.

The inner pipe is achieved queue, so the data can only be read once. When the child process writing data to the pipeline, in order to prevent the parent read, where the child process is also read, to close the read end of the pipe in the child, so only the child process writing, the parent process read; when the parent the process of reading the data, in order to prevent the parent process can also write from the pipeline, the child also wrote circumstances, to close the write end of the pipe in the parent process, so that only the child process writing, the parent process read.

9. parent and child communication code that implements ps aux | grep bash

       

 

Results of the:

注:1. 上面是父进程写,子进程读。如果我们还需要进行父进程读,子进程写的话,那么我们就需要再创建一根管道。

       2. 如果写操作的进程不关闭读端,读操作的进程不关闭写端,那么会造成阻塞,grep读不到数据一直在那里等,如下:

10. 兄弟进程间通信

父进程中需要关闭管道的读写两端。子进程1中需要关闭管道的读端,子进程2中需要关闭管道的写端。

例:

 

  

  

执行结果:

11. 管道的读写行为

12. 验证管道缓冲区大小

例:

执行结果:

13. 设置管道的非阻塞属性

上面的三行紫色代码即可完成设置管道的非阻塞属性。

14. fifo的创建

fifo文件大小为0,里面不存数据,数据存在内核的缓冲区里面。只要有fifo文件,就会和内核缓冲区做一个映射,这是操作系统行为。

其中pathname是fifo文件的名字,mode是设置文件权限的参数。最终真正的权限是我们设置的参数再与umask取反的按位与:

15. fifo进行没有血缘关系的进程间通信

先创建fifo文件,名字是myfifo,然后执行a.c的进程和执行b.c的进程之间进行通信:

管道默认有阻塞属性,所以当写进程还没有写数据时,读进程会阻塞在那里等待。当管道中有数据了之后,读进程就开始读。

16. mmap函数参数讲解

(1)mmap函数

   

           

  

注:映射区的大小为4k的整数倍,所以如果参数设置为100字节,会发现映射区大小为4k。

(2)munmap函数

17. 使用mmap读取磁盘文件内容

 

Guess you like

Origin blog.csdn.net/mengyujia1234/article/details/90897172