信号量数组初始化为NULL,seg错误

Semaphore array initializing to NULL, seg fault

本文关键字:seg 错误 NULL 数组 初始化 信号量      更新时间:2023-10-16

我全局声明了一个信号量数组,如下所示:

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);