1、创建两个线程,实现将一个文件的内容打印到终端上,类似cat一个文件
一个线程读取文件中的内容
另一个线程将读取到的内容打印到终端上
//临死前也要带着这些头文件走,哐哐报错!!
#include<stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
sem_t sem1,sem2;
char buf[32] = "";
//int flag = 0;//0读1打
int fd;
void* callBack1(void* arg)
{
while(1)
{
sem_wait(&sem1);
printf("%s",buf);
fflush(stdout);
sem_post(&sem2);
};
printf("__%d__",__LINE__);
pthread_exit(NULL);
printf("__%d__",__LINE__);
}
void* callBack2(void* arg)
{
fd = open("mutex_jpg.c",O_RDONLY);
off_t len = lseek(fd,0,SEEK_END);
off_t offset = 0;
for(int i = 0; i < len; i++)
{
sem_wait(&sem2);
lseek(fd,offset,SEEK_SET);
bzero(buf,sizeof(buf));
int res = read(fd,buf,sizeof(buf));
if(0 == res)
break;
offset = lseek(fd,0,SEEK_CUR);
sem_post(&sem1);
}
printf("__%d__",__LINE__);
pthread_exit(NULL);
printf("__%d__",__LINE__);
}
int main(int argc, const char *argv[])
{
sem_init(&sem1,0,1);
sem_init(&sem2,0,0);
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,callBack1,NULL);
pthread_create(&tid2,NULL,callBack2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("exit........\n");
sem_destroy(&sem1);
sem_destroy(&sem2);
close(fd);
return 0;
}
//用条件变量
#include<stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
char buf[32] = "";
int flag = 0;//0读1打
int fd;
void* callBack1(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
pthread_cond_wait(&cond,&mutex);
}
printf("%s",buf);
fflush(stdout);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
};
pthread_exit(NULL);
}
void* callBack2(void* arg)
{
fd = open("mutex_jpg.c",O_RDONLY);
off_t len = lseek(fd,0,SEEK_END);
off_t offset = 0;
for(int i = 0; i < len; i++)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
pthread_cond_wait(&cond,&mutex);
lseek(fd,offset,SEEK_SET);
bzero(buf,sizeof(buf));
int res = read(fd,buf,sizeof(buf));
if(0 == res)
break;
offset = lseek(fd,0,SEEK_CUR);
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//sem_init(&sem1,0,1);
//sem_init(&sem2,0,0);
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,callBack1,NULL);
pthread_create(&tid2,NULL,callBack2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("exit........\n");
//sem_destroy(&sem1);
//sem_destroy(&sem2);
close(fd);
return 0;
}
2、 用条件变量的方式实现:现有ID号为a b c的三个线程,每个线程的任务都是循环打印自己id号,要求打印的顺序为abc
#include<stdio.h>
#include <pthread.h>
#include <unistd.h>
int flag = 0;//0a1b2c
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t conda = PTHREAD_COND_INITIALIZER;
pthread_cond_t condb = PTHREAD_COND_INITIALIZER;
pthread_cond_t condc = PTHREAD_COND_INITIALIZER;
void* printf_a(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
pthread_cond_wait(&conda,&mutex);
}
printf("A\n");
flag = 1;
pthread_cond_signal(&condb);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* printf_b(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
pthread_cond_wait(&condb,&mutex);
}
printf("B\n");
flag = 2;
pthread_cond_signal(&condc);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* printf_c(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 2)
{
pthread_cond_wait(&condc,&mutex);
}
printf("C\n");
flag = 0;
pthread_cond_signal(&conda);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2,tid3;
pthread_create(&tid1,NULL,printf_a,NULL);
pthread_create(&tid2,NULL,printf_b,NULL);
pthread_create(&tid3,NULL,printf_c,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}