sem_init() 导致 SEGV

发布于 2024-12-11 19:06:36 字数 2150 浏览 0 评论 0原文

我有以下代码,它被 SEGV 信号杀死。使用调试器显示它被 main() 中的第一个 sem_init() 杀死。如果我注释掉第一个 sem_init() ,第二个会导致同样的问题。我尝试找出导致此系统调用引发 SEGV 的原因。 else 没有运行,因此错误是在它返回值之前发生的。 任何帮助将不胜感激, 谢谢。

我删除了出现此问题之前未运行的其余代码。

#define PORTNUM 7000
#define NUM_OF_THREADS 5
#define oops(msg) { perror(msg); exit(1);}
#define FCFS 0
#define SJF 1;

void bindAndListen();
void acceptConnection(int socket_file_descriptor);
void* dispatchJobs(void*);
void* replyToClient(void* pos);

//holds ids of worker threads
pthread_t threads[NUM_OF_THREADS];

//mutex variable for sleep_signal_cond
pthread_mutex_t sleep_signal_mutex[NUM_OF_THREADS];
//holds the condition variables to signal when the thread should be unblocked
pthread_cond_t sleep_signal_cond[NUM_OF_THREADS];

//mutex for accessing sleeping_thread_list
pthread_mutex_t sleeping_threads_mutex = PTHREAD_MUTEX_INITIALIZER;
//list of which threads are sleeping so they can be signaled and given a job
std::vector<bool> *sleeping_threads_list = new std::vector<bool>();

//number of threads ready for jobs
sem_t* available_threads;
sem_t* waiting_jobs;


//holds requests waiting to be given to one of the threads for execution
std::vector<std::vector<int> >* jobs = new std::vector<std::vector<int> >();

pthread_mutex_t jobs_mutex = PTHREAD_MUTEX_INITIALIZER;




int main (int argc, char * const argv[]) {

//holds id for thread responsible for removing jobs from ready queue and assigning them to worker thread
pthread_t dispatcher_thread;


//initializes semaphores
    if(sem_init(available_threads, 0, NUM_OF_THREADS) != 0){          //this is the line causing the SEGV
        oops("Error Initializing Semaphore");
    }

    if(sem_init(waiting_jobs, 0, 0) !=0){
        oops("Error Initializing Semaphore");
    }


//initializes condition variables and guarding mutexes
for(int i=0; i<NUM_OF_THREADS; i++){
    pthread_cond_init(&sleep_signal_cond[i], NULL);
    pthread_mutex_init(&sleep_signal_mutex[i], NULL);
}




if(pthread_create(&dispatcher_thread, NULL, dispatchJobs, (void*)NULL) !=0){
    oops("Error Creating Distributer Thread");

I have the following code and it is being killed by a SEGV signal. Using the debugger shows that it is being killed by the first sem_init() in main(). If I comment out the first sem_init() the second causes the same problem. I have tried figuring out what would cause this sys call to cause a SEGV. The else is not being run, so the error is happening before it can return a value.
Any help would be greatly appreciated,
Thank you.

I removed the rest of the code that isnt being run before this problem occurs.

#define PORTNUM 7000
#define NUM_OF_THREADS 5
#define oops(msg) { perror(msg); exit(1);}
#define FCFS 0
#define SJF 1;

void bindAndListen();
void acceptConnection(int socket_file_descriptor);
void* dispatchJobs(void*);
void* replyToClient(void* pos);

//holds ids of worker threads
pthread_t threads[NUM_OF_THREADS];

//mutex variable for sleep_signal_cond
pthread_mutex_t sleep_signal_mutex[NUM_OF_THREADS];
//holds the condition variables to signal when the thread should be unblocked
pthread_cond_t sleep_signal_cond[NUM_OF_THREADS];

//mutex for accessing sleeping_thread_list
pthread_mutex_t sleeping_threads_mutex = PTHREAD_MUTEX_INITIALIZER;
//list of which threads are sleeping so they can be signaled and given a job
std::vector<bool> *sleeping_threads_list = new std::vector<bool>();

//number of threads ready for jobs
sem_t* available_threads;
sem_t* waiting_jobs;


//holds requests waiting to be given to one of the threads for execution
std::vector<std::vector<int> >* jobs = new std::vector<std::vector<int> >();

pthread_mutex_t jobs_mutex = PTHREAD_MUTEX_INITIALIZER;




int main (int argc, char * const argv[]) {

//holds id for thread responsible for removing jobs from ready queue and assigning them to worker thread
pthread_t dispatcher_thread;


//initializes semaphores
    if(sem_init(available_threads, 0, NUM_OF_THREADS) != 0){          //this is the line causing the SEGV
        oops("Error Initializing Semaphore");
    }

    if(sem_init(waiting_jobs, 0, 0) !=0){
        oops("Error Initializing Semaphore");
    }


//initializes condition variables and guarding mutexes
for(int i=0; i<NUM_OF_THREADS; i++){
    pthread_cond_init(&sleep_signal_cond[i], NULL);
    pthread_mutex_init(&sleep_signal_mutex[i], NULL);
}




if(pthread_create(&dispatcher_thread, NULL, dispatchJobs, (void*)NULL) !=0){
    oops("Error Creating Distributer Thread");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-12-18 19:06:36

您声明指向信号量的指针:

sem_t* available_threads;
sem_t* waiting_jobs;

但从未初始化内存。 sem_init 函数并不期望分配内存,只是为了初始化现有的内存块。要么分配一些内存并将这些指针分配给它,要么将信号量声明为 sem_t 并将地址传递给 sem_init 。

You declare pointers to your semaphores:

sem_t* available_threads;
sem_t* waiting_jobs;

but never initialize the memory. The sem_init function is not expecting to allocate memory, just to initialize an existing blob of memory. Either allocate some memory and assign these pointers to it, or declare the semaphores as sem_t and pass the address to sem_init.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文