作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 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/
我是一名优秀的程序员,十分优秀!