c - 互斥体创建创建分段错误
问题描述
我不明白为什么在创建以下互斥锁时会出现分段错误:
#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;
}
任何帮助将不胜感激!
解决方案
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;
}
推荐阅读
- filebeat - Filebeat 将多行 postgres 日志作为一个日志发送到仅设置为此特定源的 filebeat
- amazon-web-services - EMR-5.21.0 上的 Zeppelin 0.8 java.io.InvalidClassException (+ Spark 2.4.0)
- jquery-ui - 如何在 JQ UI Datepicker 中始终在输入字段中显示日期
- python - 在现有文件中添加行
- javascript - 如何在 json-rules-engine 中查找失败的规则
- javascript - 如何将一个数组元素设置为树数组结构的根节点?
- python - plt.ion() 最近改变了行为
- javascript - Angular 7根据传递的数据选择选定的属性选项
- scala - scala - scalatest - 最终特征胜过失败断言?
- angular - latest @angular-cli 不会创建具有最新 angular 的项目