Linux System Programming (4)

1. Daemon concept

2. Create a formal process daemon

PGID which is the process group ID, SID is the session ID.

Example: Create a daemon

3. daemon exercises _ code implementation

Note: The callback function parameters to set permissions for open function is on or O_APPEND, which is an additional meaning behind the write function to write the file when it will start writing from the end.

Results of the:

4. The concept of threads

Note: the main thread and the child thread stacks do not share. A process has five threads, then the average stack area was divided into five parts.

5. pthread_create thread creation function

Example:

The results: the first out of the main thread, the child thread is forcibly ended, so there is no output sub-thread ID:

The main thread sleeps 2 seconds and then quit:

Results of the:

Note: To add parameters -lpthread gcc, specify the name of the library.

6. The cycle creates multiple child threads and Notes

Example: loop to create five sub-thread and the child thread in print is the first of several sub-sub-thread and thread ID.

Results of the:

You can see the results in print is the first of several child threads wrong. The reason is: i is a local variable, the main thread's stack, i will address as a parameter passed to the child thread, it is possible to create the current sub-thread did not grab CPU, but the main thread to continue downward, so i becomes large, when the sub-thread that just grabbed the CPU and then, taken out from the address value i is larger, so not right.

So we make improvements, you can get the correct result:

Results of the:

7. thread function to print an error message

If the return value ret is not equal to 0, indicating a mistake, error information can be printed strerror function.

8. pthread_exit function using

pthread_exit function allows the calling thread exits, and no effect on other threads. pthread_exit function has a parameter passed in retval, address error messages can be passed. Do not want an incoming error message, you can pass NULL. retval pointer must point to the global or heap space, or if you point to the local variables defined in the thread, then (in the thread's stack space) when the thread exits, local variables will disappear. exit function is a function to exit the process.

Example: exit the main thread, the thread does not affect the pair

Results of the:

Example: Call the exit function, the process exits, all threads of the process will have been forced to quit.

Results of the:

9. Use sub-thread resource recovery pthread_join

例:主线程阻塞等待子线程退出,并打印子线程退出状态。

执行结果:

可以发现打印出来的是随机值,这是因为number是子线程中定义的局部变量,存在子线程的栈中,所以子线程退出后,这个变量也就不存在了,因此打印出的是随机值。

把number换成全局变量就对了:

执行结果:

10. 线程相关函数介绍

注:在执行到系统调用的位置处,线程被杀死。如果里面没有write,printf这种系统调用,线程就不能被杀死。

11. 设置分离属性

例:在创建线程时就设置他的分离属性

12. 线程同步的概念

例:在主线程中创建两个子线程,在两个子线程中分别对同一个全局变量number做加法操作。执行完毕后可以发现,结果不正确,比预期的结果要小。

代码:

执行结果:可以发现小于20000

原因:

13. 线程同步的思想

14. 互斥锁相关的函数

(1)互斥锁类型

(2)互斥锁的使用步骤

(3)互斥锁相关函数

注:初始化互斥锁的函数的第二个参数传NULL。

15. 使用互斥锁实现线程同步

执行结果:

 

Guess you like

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