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

时间:2016-10-30 03:00:26

标签: c++ arrays segmentation-fault semaphore

我在全局声明了一个信号量数组:

sem_t *exiting_on[11];

并尝试在main中初始化它,如:

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

1 个答案:

答案 0 :(得分: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);