Linux System Programming (3)

1. mmap Notes

If you do, you need to create a separate variable to operate, and finally passed munmap function is still ptr.

 

  

2. Use mmap blood of inter-process communication

Example: The parent process mapping area to write a sentence, this sentence is synchronized to a disk file, the child process mapping area synchronize the contents of a disk file, and then print out the contents of the mapping area.

Note: The memory-mapped area is non-blocking, so the above procedures can not guarantee that the parent process to write in, and then read out the child, we should add sleep in the child process to ensure that the parent process to complete the operation.

3. Create an anonymous mapping area

Create an anonymous mapping area would not open the file, but you need to specify the size of the anonymous mapping area, and then add the flag MAP_ANON, the location of the file descriptor to write to -1.

Example:

4. mmap inter-process communication unrelated

Example: a process to communicate and process b, a read b write. A first run is not output, because the memory mapping area is nothing in and run B, the contents of the output will be a time.

Process a:

Process b:

Introduction The signal

Common signals:

Concept 6. The blocking signal set signal and the pending set

Blocking signal set and pending in the pcb signal set, it can not directly operate.

7. kill functions using

Example: call the kill function to kill the parent process child process.

Results of the:

8. raise and abort function

(1) raise function

Example: a child process to their signal to commit suicide, the father of the child process recycling process and print the child process the signal which is to be killed.

Results of the:

(2) abort function

Example: child process send yourself abnormal termination signal, the father of the child process recycling process and print the child process the signal which is to be killed.

Results of the:

9. alarm function

The first call alarm function returns 0. When the alarm function and then calls the reset timing time is also returns the number of seconds remaining before the reset operation.

Example:

Results of the:

Calculating the number of how many 10. 1s

(1) to the terminal:

With time command to view the program runs with:

Results of the:

You can see that in fact count time spent only 0.06 seconds.

(2) output to a file

After the output to a file you can see, there are a few for the number of 0.728 seconds, you can see the number of open files to more than 600 million.

Analyze the reasons:

To the output terminal, a user area to be frequently switched to the core region, the loss is high; and the output to a file, the number of switches on the lot less, so the loss is low.

Use a timer function 11. setitimer

其中参数which是定时法则,当设置为ITIMER_REAL时,是自然定时法,时间到了后发出信号SIGALRM;当设置为ITIMER_VIRTUAL时,是只算用户区的时间,时间到了后发出信号SIGVTALRM;当设置为ITIMER_PROF时,是只算用户+损耗的时间,时间到了后发出信号SIGPROF。

参数new_value是设置时间的。最后一个参数一般不用,设置为NULL即可。

例:

执行结果:

 

12. 阻塞信号集和未决信号集的状态关系

如果要设置阻塞信号集,那么我们要先创建一个自定义信号集,想要阻塞哪个信号,就把哪个信号对应位置的标志位设为1。将自定义信号集的各个标志位都设置好了之后,然后通过一系列的信号集操作函数,把这个自定义信号集设置给阻塞信号集。

13. 读当前进程的未决信号集

注:sigprocmask函数的how参数取值如下,其中取SIG_BLOCK最常用。oldset参数是传出参数,如不感兴趣设为NULL。

例:读取当前进程的未决信号集

执行结果:

14. 设置信号阻塞

设置SIGINT信号、SIGQUIT信号阻塞,并试图设置SIGKILL信号阻塞(该信号是不能被阻塞的,设置了也没用)。

 

执行结果:一开始都是0,按ctrl+c后,第二位变为1,按ctrl+反斜杠后,第三位变为1。

可以看到第9位是0,所以9号信号是不能被阻塞的。当我们用kill -9杀死该进程后,发现果然被杀死了。

15. signal信号捕捉函数

例:

每当按下ctrl+c时,signal函数会捕捉信号SIGINT,回调myfunc函数。no传入的是信号的编号。

执行结果:

16. sigaction函数的使用

       

注:结构体sigaction中,用第一个函数指针,不用第二个。如果在信号处理函数执行过程中,不临时屏蔽信号,那么将sa_mask信号集重置;如果需要临时屏蔽指定的信号,那么用sigaddset函数设置sa_mask信号集,这样在信号处理函数执行过程中,就会临时屏蔽指定的信号,当信号处理函数执行结束后,进程会处理该信号。sa_flags固定设置为0。

例1:没有临时屏蔽的信号

执行结果:

例2:临时屏蔽信号SIGQUIT

执行结果:

解释:当按下ctrl+c时,sigaction函数会捕捉信号SIGINT,回调myfunc函数,在myfunc函数中,先打印helloword,然后睡3秒,在睡觉期间按ctrl+反斜杠不会立即退出,这是因为已经设置了在回调函数执行过程中临时屏蔽SIGQUIT信号。睡醒后打印wakeup,然后回调函数结束,此时进程才会处理该信号,于是进程退出。

 

Guess you like

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