一个多线程进程的某个线程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;
}