簡體   English   中英

互斥體創建創建分段錯誤

[英]Mutex creation creates a segmentation fault

我不明白為什么在創建以下互斥鎖時會出現分段錯誤:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <semaphore.h> // include POSIX semaphores
#include <fcntl.h>



struct semaphoreStruct{
  sem_t *mutex;
};




struct semaphoreStruct *semaphore_list;

int main(){

  sem_unlink("MUTEX");
  semaphore_list->mutex = sem_open("MUTEX", O_CREAT|O_EXCL,0700,1);
  return 0;
}

任何幫助將不勝感激!

出現分段錯誤,因為 memory 未分配給struct semaphoreStruct *semaphore_list指針。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <semaphore.h> // include POSIX semaphores
#include <fcntl.h>

struct semaphoreStruct{
  sem_t *mutex;
};

struct semaphoreStruct *semaphore_list;

int main(){
  semaphore_list = malloc(sizeof(struct semaphoreStruct)); //allocate the memory for the struct
  sem_unlink("MUTEX");
  semaphore_list->mutex = sem_open("MUTEX", O_CREAT|O_EXCL,0700,1);
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM