gpt4 book ai didi

c - 段错误: 11 in a program of multithread in C using pthread library

转载 作者:行者123 更新时间:2023-11-30 19:02:14 27 4
gpt4 key购买 nike

我需要使用 pthread C 库编写一个程序来处理线程。任务文本显示:有一条走廊,有两个方向的单车道,有两种类型的员工将穿过走廊到达走廊的另一侧。有从左到右的类型 1 员工和从右到左的类型 2 员工。这条走廊里有一个服务员,在无人经过的时候负责打扫这条走廊。打扫走廊时,双方员工都要等待结束,否则,如果走廊被占用,员工说无法打扫,2秒后就去 sleep 。当我编译这个程序时,GCC 编译器返回 5 警告,但没有错误。当我运行该程序时,立即出现段错误:11。

这是更新的程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>


//global variables
int c1,c2; //counters for imp1, imp2
pthread_mutex_t m1, m2;
sem_t corridor;
double clean = 0.5;

//implementation thread attendant
void* attendant(void* arg){
sleep(1); //rest 1 seconds
if (c1==0 && c2==0){
sem_wait(&corridor);
printf("I'm starting to clean up\n");
sleep(clean);
printf ("I finished cleaning\n");
sem_post(&corridor);
}else{
printf ("I can't clean, the corridor is busy\n");
}
return NULL;
}


//thread employee type 1
void* emp1(void *arg){
printf("I'm the number %d of em1\n", c1);
pthread_mutex_lock(&m1); //beginning critical section
c1++; //it increases to signal the presence of a thread of the same type that wants to enter the corridor
if (c1 == 1){ //the thread is the only one in the corridor. Can pass
printf ("I am the first of my group emp1\n");
sem_wait(&corridor); //takes possession of the corridor
}
pthread_mutex_unlock(&m1); //allows other threads of the same type to pass in the corridor since it was the first in his group. End of critical section

// invents "passage" function

pthread_mutex_lock(&m1); //beginning of the critical section. Once crossed the corridor, the variable c1 is modified. A mutex is used to avoid inconsistency
c1--;
if(c1 == 0){
printf ("I am the last of my group emp1\n");
sem_post(&corridor);
} //if c1 == 0, it is the last thread imp1 and releases the corridor
pthread_mutex_unlock(&m1); //end critical section
return NULL;
}


//thread employee type 2
void* emp2(void *arg){
printf("I'm the number %d of emp2\n", c2);
pthread_mutex_lock(&m2); //beginning critical section
c2++; //it increases to signal the presence of a thread of the same type that wants to enter the corridor
if (c2 == 1){ // the thread is the only one in the corridor. Can pass
printf ("I am the first of my group emp2\n");
sem_wait(&corridor); //takes possession of the corridor
}
pthread_mutex_unlock(&m2); //allows other threads of the same type to pass in the corridor since it was the first in his group. End of critical sectionritica

// invents "passage" function

pthread_mutex_lock(&m2); //beginning of the critical section. Once crossed the corridor, the variable c1 is modified. A mutex is used to avoid inconsistency
c2--;
if(c2 == 0){
printf ("I am the last of my group emp2\n");
sem_post(&corridor);
}//if c1 == 0, it is the last thread imp1 and releases the corridor
pthread_mutex_unlock(&m2); //end critical section
return NULL;
}


int main(int argc, char const *argv[]) {
//pthread_t emp1, emp2, attendant;
pthread_t idt;
int r; //var random to create thread emp1 or emp2
int i; //index

//variable initialization
c1 = c2 = 0;
pthread_mutex_init(&m1, NULL);
pthread_mutex_init(&m2, NULL);
sem_init(&corridor,0,1);

pthread_create(&idt,NULL,attendant,NULL);
while(i<40){
sleep(1);
r = rand()%2;
if(r==0){
printf("Employee creation 1\n");
pthread_create(&idt,NULL,emp1,NULL);
}else{
printf("Employee creation 2\n");
pthread_create(&idt,NULL,emp2,NULL);
}
i++;
pthread_join(idt, NULL);
}
return 0;
}

最佳答案

pthread_t emp1, emp2, attendant;隐藏具有相同名称的函数,因此 pthread_create(&idt,NULL,attendant,NULL);传递变量 attendant 的未初始化值至pthread_create作为函数指针而不是函数 void* attendant(void* arg) .

删除变量,您根本没有使用它们。

你还忘了#include <unistd.h> .

这将使您的程序编译:https://ideone.com/3CIb4J

还有你的main函数不等待线程完成,您应该使用 pthread_join等待线程。

关于c - 段错误: 11 in a program of multithread in C using pthread library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56271599/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com