用信号量的方式,创建两个线程 A B
1. A线程读取文件中的内容
2. B线程打印A读取到的内容到终端,
3. 全部打印完毕后,结束进程;
4. 现象类似cat一个文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
struct msg
{
int fd1;
off_t size;
}info;
sem_t sem;
char str[500]="";
void *task1(void *arg)
{
if(sem_wait(&sem))//P操作
{
perror("sem_wait");
return NULL;
}
/**********临界区************/
lseek(info.fd1,0,SEEK_SET);
for(int i=0;i<info.size;i++)
{
ssize_t res=read(info.fd1,&str[i],1);//线程1读取文件内容并存到数组str中
if(res<0)
{
perror("read");
return NULL;
}
}
/**********临界区************/
if(sem_post(&sem))//V操作
{
perror("sem_post");
return NULL;
}
pthread_exit(NULL);//退出线程
}
void *task2(void *arg)
{
while(1)
{
if(strlen(str))//当字符数组str被赋值后再执行打印操作
{
if(sem_wait(&sem))//P操作
{
perror("sem_wait");
return NULL;
}
/**********临界区************/
for(int i=0;i<info.size;i++)
{
printf("%c",str[i]);//线程2打印
}
/**********临界区************/
if(sem_post(&sem))//V操作
{
perror("sem_post");
return NULL;
}
pthread_exit(NULL);//退出线程
}
}
}
int main(int argc, const char *argv[])
{
info.fd1=open(argv[1],O_RDONLY);//打开文件
if(info.fd1<0)
{
perror("open");
return -1;
}
info.size=lseek(info.fd1,0,SEEK_END);//求出文件大小
if(sem_init(&sem,0,1))//创建并初始化信号量
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,task1,NULL))//创建线程1
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,task2,NULL))//创建线程2
{
perror("pthread_create");
return -1;
}
pthread_join(tid1,NULL);//阻塞等待线程1退出
pthread_join(tid2,NULL);//阻塞等待线程2退出
sem_destroy(&sem);//摧毁信号量
close(info.fd1);
return 0;
}