只有一个进程通过我的互斥锁,其他进程挂起

Only one process is passing through my mutex lock and the others are hanging

所以我正在开发一个程序,它允许我使用多个进程打开单个文件实例而不会发生死锁。所以我的程序的关键功能如下。它基本上决定了程序是否可以基于邻接图执行文件。所以那部分似乎工作正常。

我 运行 遇到的麻烦似乎是我的互斥锁定。所以基本上程序的执行方式是它决定一个进程需要 运行 完全,然后其他进程才能打开文件而不会出现死锁(这是很好的预期行为)。但是一旦第一个进程完成,它就会挂起,我相信我已经将它缩小到代码顶部的 pthread_mutex_lock 调用。然而正如你所见,我解锁了它,所以我很困惑。

这可能与共享内存块中使用我的互斥锁有关吗?我无法想象它会引起问题,而且我也没有其他方法可以真正做到这一点。我也尝试过使用信号量做同样的事情,但没有成功。任何帮助将不胜感激。

FILE *openFile(char *path, char *mode) {

int segId = shmget(systemKey, size, S_IRUSR | S_IWUSR | IPC_CREAT);
memStruct* ourMem = (memStruct*)shmat(segId, NULL, 0);

pthread_mutex_lock(&ourMem->openMutex);//~~~~~~~~~~

int hasCycle = containsCycle(ourMem, path);


if(hasCycle == 0) {
    FILE* fileToReturn = fopen(path, mode);
    int positionOfFile = getFilePosition(path, ourMem);
    int positionOfProcess = getProcessPosition(getpid(), ourMem);

    ourMem->adjMatrix[positionOfFile][positionOfProcess] = 2;
    ourMem->Available[positionOfFile] = 0;
    ourMem->fileArray[positionOfFile] = fileToReturn;

    pthread_mutex_unlock(&ourMem->openMutex);//~~~~~~~~~~

    return fileToReturn;
}
else {
    while(1) {
        hasCycle = containsCycle(ourMem, path);

            if(hasCycle == 0) {
                    FILE* fileToReturn = fopen(path, mode);
                    int positionOfFile = getFilePosition(path, ourMem);
                    int positionOfProcess = getProcessPosition(getpid(), ourMem);

                    ourMem->adjMatrix[positionOfFile][positionOfProcess] = 2;
                    ourMem->Available[positionOfFile] = 0;
                    ourMem->fileArray[positionOfFile] = fileToReturn;

                    pthread_mutex_unlock(&ourMem->openMutex);//~~~~~~~~~~

                    return fileToReturn;
        }
    }
}



pthread_mutex_unlock(&ourMem->openMutex);//~~~~~~~~~~


return NULL;
}

结果是 sem_init(&semName, 1, 1) 我有一个 0 作为阻止进程间通信的第二个参数。