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


so working on program allow me open single instances of files using multiple processes without deadlock occurring. key function program below. decides if program can execute file based on adjacency graph. part seems work fine.

the trouble running seems mutex locking. how program executes decides 1 process need run before other processes able open files without deadlock (this fine , expected behavior). once first process finishes hangs , believe have narrowed down pthread_mutex_lock call @ top part of code. yet can see unlock quite confused.

could have fact mutex being used in shared memory block? can't imagine cause problems , there no other way me really. tried same thing using semaphore no luck. appreciated.

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

it turns out sem_init(&semname, 1, 1) had 0 second argument blocking interprocess communication.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -