信号量数组初始化为NULL,seg错误
Semaphore array initializing to NULL, seg fault
我全局声明了一个信号量数组,如下所示:
sem_t *exiting_on[11];
并尝试将其初始化为:
for(int x = 0; x<11; x++)
sem_init(exiting_on[x], 0, 0);
调试已经揭示了完全正常(出现(的for循环中的一些错误,尽管我在搜索时发现的每个例子看起来都很像。也许我遗漏了一些明显的东西,我不确定。我将在下面发布完整的代码(模拟电梯,类似于信号量的理发店问题(,但我还没有设法让它运行,所以它可能充满了其他错误。
欢迎任何建议!这对我来说都是新鲜事,所以我总是想学习。
#include <pthread.h>
#include <iostream>
#include <stdlib.h>
#include <semaphore.h>
using namespace std;
sem_t lobby_people; //current number of people in the "lobby"
sem_t rider; //people on the elevator
sem_t ele_full; //tells the elevator it is full
sem_t arrived; //tells elevator the person got off, prevents closing the door before they leave
sem_t sim_done; //tells main that elevator is done
sem_t initialized; //tells main that person has recorded their pid from guest so that it can be safely incremented
sem_t *exiting_on[11]; //holds persons until they reach their desired floor
int pushed_button[11] = {0}; //craftily allows elevator to know how many people want to go to a floor
int current_floor = 1;
int target_floor =0;
int e_capacity = 7; //craftily allows people to know when to tell elevator to close
//Unimportant printing methods
//
//
void *person(void *pid)
{
int *person_number = static_cast<int*>(pid);
sem_post(&initialized);
target_floor = rand() % 9 + 2;
sem_wait(&rider);
board(*person_number, target_floor);
sem_wait(&lobby_people);
e_capacity--;
pushed_button[target_floor + 1]++;
if(e_capacity==0)
sem_post(&ele_full);
sem_wait(exiting_on[target_floor + 1]); //trying to use the array
get_off(*person_number);
sem_post(&arrived);
pthread_exit(NULL);
}
void *elevator(void *arg)
{
for(int trips = 0; trips < 7; trips++)
{
sem_wait(&ele_full);
close_door();
for(int current_floor = 1; current_floor < 11; current_floor++)
{
if(pushed_button[current_floor] != 0)
{
open_door(current_floor + 1);
while(pushed_button[current_floor] != 0)
{
sem_post(exiting_on[current_floor]); //also trying to use the array
sem_wait(&arrived);
}
close_door();
}
}
open_door(1);
e_capacity = 7;
for(int new_riders = 0; new_riders < 7; new_riders++)
sem_post(&rider);
}
sem_post(&sim_done);
pthread_exit(NULL);
}
int main()
{
//initializing our semaphores
sem_init(&lobby_people, 0, 0);
sem_init(&rider, 0, 7);
sem_init(&ele_full, 0, 0);
sem_init(&arrived, 0, 0);
sem_init(&sim_done, 0, 0);
sem_init(&initialized, 0, 0);
for(int x = 0; x<11; x++) //the likely culprit!
sem_init(exiting_on[x], 0, 0);
pthread_t my_thread;
pthread_create(&my_thread, NULL, &elevator, NULL); //starts elevator thread, which waits for riders
for(int guests = 0; guests < 49; guests++) //generates 49 people threads
{
sem_post(&lobby_people);
pthread_create(&my_thread, NULL, &person, &guests);
sem_wait(&initialized); //waits for the person to copy their value of guests before incrementing it
}
sem_wait(&sim_done); //awaits the end of the elevator's 7th run
complete();
return 0;
}
sem_t *exiting_on[11];
这声明了一个指向sem_t
的指针数组。数组中的值,即个体指针,没有初始化。指针是随机垃圾。
sem_init()
获取一个指向sem_t
结构的有效指针,并对其进行初始化。传递每个未初始化的无效指针是未定义的行为。您应该声明一个sem_t
结构的数组:
sem_t exiting_on[11];
然后初始化每个:
for(int x = 0; x<11; x++)
sem_init(&exiting_on[x], 0, 0);
相关文章:
- C++ - 将元素按升序插入数组的 SEG 错误
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 字符串上的合并排序上的 Seg 错误
- 导致 Seg 错误的 SingleLinkedList 的析构函数
- g++ 9.2.1 (Linux) 会导致 seg 错误,但 Windows 上的代码块不会
- 函数抛出seg错误,我不知道为什么
- Microsoft Visual Studio 2019交叉编译CMake库 - 有效,但存在SEG错误
- 由于相同的文件名/类名存在于不同的SO中而导致的SEG错误
- 尝试访问 3D 矢量中的元素时出现 Seg 错误
- 垫子对象数组太大会导致 Seg 错误
- 为什么push_back成矢量<矢量<int>>导致 seg 错误?
- 在向量中存储字符串存在 SEG 错误
- 更改本地类名时出现 seg 错误
- 使用 ->GetString( " ") 时出现 Seg 错误,它位于单独的类中
- 将字符数组转换为 std::string 以传递到 std::bitset seg 错误
- 字符和 int 连接何时会导致 seg 错误?
- 静态数组溢出堆栈(seg 错误 11)仅在类定义内部时,否则不会..?
- .EOF 在读取文件时导致 Seg 错误:FIXXED
- 在 qt5 中使用 QSharedPointer 时出现 Seg 错误
- 为什么我的 if 语句会出现 seg 错误?