Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 互斥锁死锁(pthread_Mutex)_C_Unix_Pthreads_Mutex_Deadlock - Fatal编程技术网

C 互斥锁死锁(pthread_Mutex)

C 互斥锁死锁(pthread_Mutex),c,unix,pthreads,mutex,deadlock,C,Unix,Pthreads,Mutex,Deadlock,大家晚上好,我不明白为什么在这段代码中会出现死锁。 我特别定义了互斥体: mutex3:=0已锁定 mutex2:=1未锁定 我有两个过程:父亲和儿子。每个线程都有N个线程,我通过参数传递它们。孩子的线程与父亲的线程发生冲突,这是一个典型的资源生产者/消费者问题 提前谢谢 我想问题在这里: void * func_padre(void * n) { while(1) { pthread_mutex_lock(&mutex2); printf

大家晚上好,我不明白为什么在这段代码中会出现死锁。 我特别定义了互斥体:

mutex3:=0已锁定 mutex2:=1未锁定 我有两个过程:父亲和儿子。每个线程都有N个线程,我通过参数传递它们。孩子的线程与父亲的线程发生冲突,这是一个典型的资源生产者/消费者问题

提前谢谢

我想问题在这里:

void * func_padre(void * n)
{
    while(1)
    {
        pthread_mutex_lock(&mutex2);
        printf("Scrivi messaggio: ");
        fflush(stdout);
        scanf("%[^\n]",(char *)addr_mem);
        getchar();
        pthread_mutex_unlock(&mutex3);

    }
}

void * func_figlio(void * n)
{
    while(1)
    {   
        pthread_mutex_lock(&mutex3);
        write(fd,addr_mem,4096);
        lseek(fd,0,SEEK_SET);
        memset(addr_mem,0,4096);
        pthread_mutex_unlock(&mutex2);
    }
}
代码:


您的问题不是死锁,而是错误代码。线程无法解锁未锁定的互斥锁。您的while循环会解锁它们不持有的所有互斥锁,也不会解锁它们持有的互斥锁。

建议:

使全局变量在一组有限的值之间循环,例如:0,1,0

使用互斥来阻止其他线程的进程,即

When a call (in thread) returns from locking the mutex, 
then check 
    if the value in the global variable is for 'this' thread.
    then  
        process the thread activities,  
        increment the global variable,
    endif 
endif
unlock the mutex
call nanosleep() to delay for a while to allow other threads to run

如果我想将这样的情况与互斥体同步,我应该怎么做?我是被迫使用信号量系统V吗?不,只是修复错误。关于:pthread_mutex_t mutex2,mutex3;代码应将其分为两条语句。其中,互斥体的每个声明都有一个初始值设定项,类似于:pthread_mutex_t mutex2=pthread_mutex_初始值设定项;OT:关于:pthread_create&tid,NULL,func_padre,NULL;始终检查返回值。0以外的任何值都表示函数失败。当它失败时,调用'perror pthread_create failed;OT:紧接着:pid=分叉;应该是:if pid<0{perror fork失败;exit-exit_失败;}OT:about:printfPOCHI参数\n;福卢斯斯特杜特;1应该向stderr输出一条错误消息,而不是stdout 2应该输出一条用法消息,告诉用户他们应该做什么。类似于:fprintf stderr,用法:%s\n,argv[0];关于:fd=openargv[1],O|u CREAT | O|u RDWR,0666;1 O_CREAT表示该文件一定不存在,因此O_RDWR不是一个好的选择,因为无法从一个不包含任何内容的文件中读取。2始终检查返回值是否大于等于0。如果小于0,则调用:Perr或open失败;其次是:退出失败;
When a call (in thread) returns from locking the mutex, 
then check 
    if the value in the global variable is for 'this' thread.
    then  
        process the thread activities,  
        increment the global variable,
    endif 
endif
unlock the mutex
call nanosleep() to delay for a while to allow other threads to run