#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <semaphore.h>
sem_t sem1,sem2;
pthread_t tid1,tid2;
char buf[100]={0};
char *p;
void *fun1(void *arg)
{
FILE *fp1 = fopen("./1.txt","r");
if(fp1 == NULL)
{
perror("fopen");
return NULL;
}
while(1)
{
sem_wait(&sem2);
p = fgets(buf,sizeof(buf),fp1);
if(p == NULL)
{
perror("fgets");
break;
}
sem_post(&sem1);
}
}
void *fun2(void *arg)
{
FILE *fp2 = fopen("./2.txt","a");
if(fp2 == NULL)
{
perror("fopen");
return NULL;
}
while(1)
{
sem_wait(&sem1);
fputs(buf,fp2);
fflush(fp2);
sem_post(&sem2);
}
}
int main(int argc
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交