线程fork进程
来源:互联网 发布:poc软件对讲 编辑:程序博客网 时间:2024/05/22 14:08
一个多线程进程的某个线程fork出一个子进程,那么子进程完整复制调用fork的线程,且子进程不会拥有原进程那么多的线程,这里出现一个潜在的死锁条件,就是子进程继承了父进程的互斥锁但是并不知道该互斥锁的状态,若该互斥锁以上锁且子进程再次加锁该互斥量,此时子进程将死锁。
#include <pthread.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <wait.h>pthread_mutex_t mutex;void* another( void* arg ){ printf( "in child thread, lock the mutex\n" ); pthread_mutex_lock( &mutex ); sleep( 5 ); pthread_mutex_unlock( &mutex );}void prepare(){ pthread_mutex_lock( &mutex );}void infork(){ pthread_mutex_unlock( &mutex );}int main(){ pthread_mutex_init( &mutex, NULL ); pthread_t id; pthread_create( &id, NULL, another, NULL ); //pthread_atfork( prepare, infork, infork );//pthread_atfork()可以确保fork后父进程和子进程都拥有一个清楚的锁状态,prepare将父进程所有互斥锁锁住,infork释放prepare锁住的互斥锁,调用这行时子进程就不会死锁了 sleep( 1 ); int pid = fork(); if( pid < 0 ) { pthread_join( id, NULL ); pthread_mutex_destroy( &mutex ); return 1; } else if( pid == 0 ) { printf( "I anm in the child, want to get the lock\n" ); pthread_mutex_lock( &mutex ); printf( "I can not run to here, oop...\n" ); pthread_mutex_unlock( &mutex ); exit( 0 ); } else { pthread_mutex_unlock( &mutex ); wait( NULL ); } pthread_join( id, NULL ); pthread_mutex_destroy( &mutex ); return 0;}
- fork 进程 pthread_create线程
- 线程fork进程
- Linux中进程、线程和fork()
- linux之多线程fork:进程通信
- Ubantu丨线程与进程fork()
- 进程fork
- 进程fork
- 进程fork();
- fork进程
- 1进程与线程以及Linux中fork()的运用
- fork-fork总共几个进程
- 嵌入式开发第24天(线程参数,进程,线程,fork,exit,waitpid,exec)
- fork() 进程学习
- 创建进程fork
- 多进程总结-fork
- 创建进程库函数fork
- fork() 创建新进程
- 环境变量,fork,守护者进程
- windows server 2008 r2的FTP配置和访问
- 幂集的递归回溯求解
- 1043. Is It a Binary Search Tree (25)
- 网络编程之 Socket的模式(一) --- “阻塞/非阻塞” 与 “同步/异步”
- java学习控制流语句
- 线程fork进程
- 干掉系统bug思路
- linux 内存分页机制
- MinGW 介绍
- 集合上二元关系性质的判定
- 计算机经典书籍电子书合集(适合计算机学生学习以及程序员笔试、面试)
- 【VS2010】如何更改添加包含目录(附加依赖项等)
- libGDX开发教程(一)--Libgdx基础入门
- Ubuntu1204和Ubuntu1010安装minicom