Advanced Programming in UNIX Environment Episode 65
发布日期:2021-10-07 23:47:41 浏览次数:4 分类:技术文章

本文共 3464 字,大约阅读时间需要 11 分钟。

Threads and fork

When a thread calls fork, a copy of the entire process address space is made for the child. Recall the discussion of copy-on-write in Section 8.3. The child is an entirely different process from the parent, and as long as neither one makes changes to its memory contents, copies of the memory pages can be shared between parent and child.

#include 
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));

The parent and the child end up unlocking duplicate locks stored in different memory locations, as if the following sequence of events occurred:

1.The parent acquired all its locks.

2.The child acquired all its locks.
3.The parent released its locks.
4.The child released its locks.

We can call pthread_atfork multiple times to install more than one set of fork handlers.

For example, assume that module A calls functions from module B and that each module has its own set of locks. If the locking hierarchy is A before B, module B must install its fork handlers before module A. When the parent calls fork, the following steps are taken, assuming that the child process runs before the parent:

1.The prepare fork handler from module A is called to acquire all of module A’s locks.

2.The prepare fork handler from module B is called to acquire all of module B’s locks.
3.A child process is created.
4.The child fork handler from module B is called to release all of module B’s locks in the child process.
5.The child fork handler from module A is called to release all of module A’s locks in the child process.
6.The fork function returns to the child.
7.The parent fork handler from module B is called to release all of module B’s locks in the parent process.
8.The parent fork handler from module A is called to release all of module A’s locks in the parent process.
9.The fork function returns to the parent.

#include "apue.h"#include 
pthread_mutex_t lock1=PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t lock2=PTHREAD_MUTEX_INITIALIZER;void prepare(void){ int err; printf("preparing locks...\n"); if((err=pthread_mutex_lock(&lock1))!=0) err_count(err,"can't lock lock1 in prepare handler"); if((err=pthread_mutex_lock(&lock2))!=0) err_count(err,"can't lock lock2 in prepare handler");}vid parent(void){ int err; printf("parent unlocking locks...\n"); if((err=pthread_mutex_unlock(&lock1))!=0) err_count(err,"can't unlock lock1 in parent handler"); if((err=pthread_mutex_unlock(&lock2))!=0) err_count(err,"can't unlock lock2 in parent handler");}void child(void){ int err; printf("child unlocking locks...\n"); if((err=pthread_mutex_unlock(&lock1))!=0) err_count(err,"can't unlock lock1 in child handler"); if((err=pthread_mutex_unlock(&lock2))!=0) err_count(err,"can't unlock lock2 in child handler");}void *thr_fn(void *arg){ printf("thread started...\n"); pause(); return 0;}int main(void){ int err; pid_t pid; pthread_t tid; if((err=pthread_atfork(prepare, parent,child))!=0) err_exit(err, "can't install fork handlers"); if((err=pthread_creaete(&tid, NULL,thr_fn,0))!=0) err_exit(err,"can't create thread"); sleep(2); printf("parent about to fork...\n"); if((pid=fork())<0) err_quit("fork failed"); else if(pid==0) printf("child returned from fork\n"); else printf("parent return from fork\n"); return 0;}

pthread_atfork example

转载地址:https://blog.csdn.net/myfather103/article/details/79984133 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Advanced Programming in UNIX Environment Episode 66
下一篇:Advanced Programming in UNIX Environment Episode 64

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月22日 11时01分19秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Vue条件渲染---vue工作笔记0008 2019-04-26
Vue事件处理_vue的事件处理超级方便_功能强大---vue工作笔记0011 2019-04-26
Vue表单数据自动收集---vue工作笔记0012 2019-04-26
Vue生命周期---vue工作笔记0013 2019-04-26
ES6-ES11新特性_ECMAScript_简单介绍---JavaScript_ECMAScript工作笔记001 2019-04-26
ES6-ES11新特性_ECMAScript相关名词介绍_---JavaScript_ECMAScript工作笔记002 2019-04-26
ES6新特性_let变量声明以及声明特性---JavaScript_ECMAScript_ES6-ES11新特性工作笔记003 2019-04-26
Sharding-Sphere,Sharding-JDBC_介绍_Sharding-Sphere,Sharding-JDBC分布式_分库分表工作笔记001 2019-04-26
Sharding-Sphere,Sharding-JDBC_分库分表介绍_Sharding-Sphere,Sharding-JDBC分布式_分库分表工作笔记002 2019-04-26
C++_类和对象_对象特性_构造函数的分类以及调用---C++语言工作笔记041 2019-04-26
C++_类和对象_对象特性_拷贝构造函数调用时机---C++语言工作笔记042 2019-04-26
C++_类和对象_对象特性_构造函数调用规则---C++语言工作笔记043 2019-04-26
C++_类和对象_对象特性_深拷贝与浅拷贝---C++语言工作笔记044 2019-04-26
AndroidStudio_java.util.ConcurrentModificationException---Android原生开发工作笔记237 2019-04-26
AndroidStudio_android中实现对properties文件的读写操作_不把properties文件放在assets文件夹中_支持读写---Android原生开发工作笔记238 2019-04-26
弹框没反应使用Looper解决_the caller should invoke Looper.prepare() and Looper.loop()---Android原生开发工作笔记239 2019-04-26
Command line is too long. Shorten command line for Application---微服务升级_SpringCloud Alibaba工作笔记0067 2019-04-26
AndroidStudio_android实现双击_3击_监听实现---Android原生开发工作笔记240 2019-04-26
C++_类和对象_对象特性_初始化列表---C++语言工作笔记045 2019-04-26
AndroidStudio安卓原生开发_UI高级_DrawerLayout_侧滑菜单控件---Android原生开发工作笔记120 2019-04-26