简体   繁体   中英

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)
  • mutex2: = 1 (UNLOCKED)

I have 2 processes: Father and son. Each has N threads that I pass by argument. 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.

suggest:

have global variable that is cycled through a limited set of values, say: 0, 1, 0 ...

Use the mutex to stop other threads from progressing, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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