我需要使用pthread C库制作一个程序来处理线程。该任务的文本说:有一个走廊,该走廊有一个用于两个方向的单车道,并且有两种类型的员工将越过走廊到达走廊的另一侧。有类型1的员工从左到右,类型2的员工从右到左。在这条走廊中,有一个服务员在没人通过它时清洁它。清洁走廊时,双方的员工都等待走廊结束,否则,如果走廊被占用,员工表示无法清洁并入睡2秒钟。 当我编译该程序时,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;
}
答案 0 :(得分:1)
pthread_t emp1, emp2, attendant;
用相同的名称遮盖函数,因此pthread_create(&idt,NULL,attendant,NULL);
将变量attendant
的未初始化值传递给pthread_create
作为函数指针而不是函数{{1 }}。
删除变量,根本就没有使用它们。
您还忘记了void* attendant(void* arg)
。
这将使您的程序编译:https://ideone.com/3CIb4J
此外,您的#include <unistd.h>
函数不等待线程完成,您应该使用main
等待线程。