简体   繁体   English

互斥锁死锁(pthread_mutex)

[英]Mutex Deadlock (pthread_mutex)

Good evening guys, I can't understand why in this code I get a deadlock. 晚上好,我不明白为什么在这段代码中我陷入僵局。 I have defined mutexes in particular: 我特别定义了互斥锁:

  • mutex3: = 0 (LOCKED) 互斥锁3:= 0(锁定)
  • mutex2: = 1 (UNLOCKED) 互斥锁2:= 1(未锁定)

I have 2 processes: Father and son. 我有两个过程:父子。 Each has N threads that I pass by argument. 每个都有我通过参数传递的N个线程。 The child's threads contend with the father's threads a "resource" (typical producer / consumer problem). 孩子的线程与父亲的线程争用“资源”(典型的生产者/消费者问题)。

Thanks in advance! 提前致谢!

I suppose the problem is here: 我想问题就在这里:

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);
    }
}

CODE: 码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

void * addr_mem;
pthread_mutex_t mutex2,mutex3;
int fd;
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);
    }
}

int main(int argc, char*argv[])
{
        if(argc<2)
        {
            printf("POCHI PARAMETRI\n");
            fflush(stdout);
            exit(-1);
        }

        int val=strtol(argv[2],0,10);

        fd = open(argv[1],O_CREAT|O_RDWR,0666);
        lseek(fd,0,SEEK_SET);//USELESS

        ///SHARED MEMORY
        int id_mem;


        id_mem = shmget(6543,4096,IPC_CREAT|0666);
        addr_mem = shmat(id_mem,NULL,0);
        /////////////////////

        /// MUTEX
        pthread_mutex_init(&mutex2,NULL);
        pthread_mutex_init(&mutex3,NULL);
        pthread_mutex_lock(&mutex3);
        /////////////////////
        pthread_t tid;
        int i=0;

        int pid;
        pid = fork();
        if(pid==0)
        {
            //CHILD
            for(i=0; i<val; i++)
                pthread_create(&tid,NULL,func_figlio,NULL);

            while(1)
            {   
                pthread_mutex_lock(&mutex3);
                write(fd,addr_mem,4096);
                memset(addr_mem,0,4096);
                pthread_mutex_unlock(&mutex2);
            }
        }
        else
        {
            //FATHER
            for(i=0; i<val; i++)
                pthread_create(&tid,NULL,func_padre,NULL);

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

                }
        }
}

Your problem is not a deadlock, it's just buggy code. 您的问题不是僵局,而只是错误代码。 A thread cannot unlock a mutex it did not lock. 线程无法解锁它未锁定的互斥锁。 Your while loops all unlock mutexes they do not hold and never unlock the mutexes they do hold. while循环全部解锁他们不持有互斥体和解锁从来没有他们持有的互斥。

suggest: 建议:

have global variable that is cycled through a limited set of values, say: 0, 1, 0 ... 具有通过一组有限的值循环的全局变量,例如:0、1、0 ...

Use the mutex to stop other threads from progressing, IE 使用互斥量阻止其他线程继续运行,即IE

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM