互斥量创建会导致分段错误
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;
}
如有任何帮助,我们将不胜感激!
由于未为 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;
}
我不明白为什么在创建以下互斥体时会出现分段错误:
#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;
}