Skip to content

Commit

Permalink
Merge pull request #9 from dead10ck/fix-deadlock
Browse files Browse the repository at this point in the history
fix deadlock in shmget
  • Loading branch information
Grimler91 authored Mar 26, 2021
2 parents 76da963 + 12f9edf commit 729bb8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,12 @@ int shmget(key_t key, size_t size, int flags)
path_buffer[path_length] = '\0';
int shmid = atoi(path_buffer);
if (shmid != 0) {
int idx = ashv_read_remote_segment(shmid);
int idx = ashv_find_local_index(shmid);

if (idx == -1) {
idx = ashv_read_remote_segment(shmid);
}

if (idx != -1) {
pthread_mutex_unlock(&mutex);
return shmem[idx].id;
Expand Down
11 changes: 11 additions & 0 deletions test/deadlock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "utils.h"

int main() {
key_t SHMEM_KEY = ftok(".", 23);

int shmid_from_shmget;
if ((shmid_from_shmget = shmget(SHMEM_KEY, 30, IPC_CREAT | 0666)) < 0) error_exit("shmget");
if ((shmid_from_shmget = shmget(SHMEM_KEY, 30, IPC_CREAT | 0666)) < 0) error_exit("shmget");

return 0;
}

0 comments on commit 729bb8c

Please sign in to comment.