两个线程 A、B,要求A线程读取文件中的数据,B线程将读取到的数据打印到终端上
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
char str[128] = "";
pthread_cond_t cond;
pthread_mutex_t mutex;
int flag=0;
//信号量 信号灯
sem_t sem1, sem2;
void* callBack_read(void* arg)
{
FILE* fp = fopen("./fork.c", "r");
if(NULL == fp)
{
perror("fopen");
pthread_exit(NULL);
}
//循环读取文件中的数据,直到文件读取完毕
while(1)
{
//P sem1
// sem_wait(&sem1);
pthread_mutex_lock(&mutex);
if(0!=flag)
{
pthread_cond_wait(&cond, &mutex);
}
if(fgets(str, sizeof(str), fp) == NULL)
{
//V sem_post
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
// sem_post(&sem2);
break;
}
flag=1;
//V sem2
//sem_post(&sem2);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
fclose(fp);
}
void* callBack_write(void* arg)
{
while(1)
{
//P sem2
//sem_wait(&sem2);
pthread_mutex_lock(&mutex);
if(1!=flag)
{
pthread_cond_wait(&cond, &mutex);
}
if(strlen(str) == 0)
break;
printf("%s", str);
fflush(stdout);
bzero(str, sizeof(str));
flag=0;
//V sem1
// sem_post(&sem1);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, const char *argv[])
{
//创建两个信号量
/* if(sem_init(&sem1, 0, 1) < 0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem2, 0, 0) < 0)
{
perror("sem_init");
return -1;
}*/
//创建条件变量
if(pthread_cond_init(&cond, NULL)<0)
{
perror("pthread_cond_init");
return -1;
}
pthread_mutex_init(&mutex, NULL);
//创建两个线程
pthread_t tid1, tid2;
//一个线程读取文件
if(pthread_create(&tid1, NULL, callBack_read, NULL) != 0)
{
perror("pthread_create");
return -1;
}
//另外一个线程打印读取到的数据
if(pthread_create(&tid2, NULL, callBack_write, NULL) != 0)
{
perror("pthread_create");
return -1;
}
//阻塞等待
pthread_join(tid1, NULL);
// pthread_detach(tid2);
pthread_join(tid2, NULL);
//信号量的销毁
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推;
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t cond1, cond2;
int flag=0;
void *callbach_a(void *arg)
{
int i=0;
while(i<10)
{
pthread_mutex_lock(&mutex);
if(0!=flag)
{
pthread_cond_wait(&cond1, &mutex);
}
printf("A");
i++;
flag=1;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *callbach_b(void *arg)
{
int i=0;
while(i<10)
{
pthread_mutex_lock(&mutex);
if(1!=flag)
{
pthread_cond_wait(&cond1, &mutex);
}
printf("B");
i++;
flag=2;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *callbach_c(void *arg)
{
int i=0;
while(i<10)
{
pthread_mutex_lock(&mutex);
if(2!=flag)
{
pthread_cond_wait(&cond2, &mutex);
}
printf("C\n");
i++;
flag=0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex, NULL);
if(pthread_cond_init(&cond1, NULL)<0)
{
perror("pthread_cond_init");
return -1;
}
if(pthread_cond_init(&cond2, NULL)<0)
{
perror("pthread__cond_init");
return -1;
}
pthread_t a, b, c;
if(pthread_create(&a, NULL, callbach_a, NULL)<0)
{
perror("pthread_creat");
return -1;
}
if(pthread_create(&b, NULL, callbach_b, NULL)<0)
{
perror("pthread_creat");
return -1;
}
if(pthread_create(&c, NULL, callbach_c, NULL)<0)
{
perror("pthread_creat");
return -1;
}
pthread_join(a,NULL);
pthread_join(b,NULL);
pthread_join(c,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
return 0;
}