在sem_wait(&sem)和sem_post(&sem)之间加上printf("\n"),报段错误 (核心已转储)
#include
#include
#include
int num;
sem_t sem[4];
void* pthread_main1()
{
for(int i=0;i_wait(&sem[0]);
printf("I ");
sem_post(&sem[1]);
}
}
void* pthread_main2()
{
for(int i=0;i_wait(&sem[1]);
printf("Love ");
sem_post(&sem[2]);
}
}
void* pthread_main3()
{
for(int i=0;i_wait(&sem[2]);
printf("Jinan ");
sem_post(&sem[3]);
}
}
void* pthread_main4()
{
for(int i=0;i_wait(&sem[3]);
printf("University! \n ");
sem_post(&sem[0]);
}
}
void init_sems()
{
for (int i = 0; i < 4; i++)
{
if(i==0)
{
sem_init(sem+i,0,1);
}
else
{
sem_init(sem+i,0,0);
}
}
}
int main()
{
int i=0;
printf("please enter the count:");
scanf("%d",&num);
init_sems();
pthread_t pid[4];
pthread_create((void*)&(pid[0]),NULL,(void*)pthread_main1,NULL);
pthread_create((void*)&(pid[1]),NULL,(void*)pthread_main2,NULL);
pthread_create((void*)&(pid[1]),NULL,(void*)pthread_main3,NULL);
pthread_create((void*)&(pid[1]),NULL,(void*)pthread_main4,NULL);
for(i=0;i<4;i++)
{
pthread_join(pid[i],NULL);
}
sem_destroy(&sem[0]);
sem_destroy(&sem[1]);
sem_destroy(&sem[2]);
sem_destroy(&sem[3]);
return 0;
}
错误发生在pthread_main4,我想换行打印,但直接报错