int flag=0;
void* RUN(void* arg)
{
while(1)
{
while(1)
{
if(flag!=0)
{
break;
}
}
flag=0;
char buf2[128]={0};
int fd=open("./zuoye.txt",O_RDONLY,0666);
int retval1=read(fd,buf2,128);
printf("子线程读出%s\n",buf2);
close(fd);
sleep(1);
flag=1;
}
}
int main(int argc, const char *argv[])
{
flag=1;
pthread_t thread;
if(pthread_create(&thread,NULL,RUN,NULL)!=0)
{
perror("pthread_create");
return 1;
}
while(1)
{
while(1)
{
if(flag!=0)
{
break;
}
}
flag=0;
char buf[128]={"aasa"};
int fd=open("./zuoye.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
int retval1=write(fd,buf,128);
printf("主线程写入%s\n",buf);
close(fd);
sleep(1);
flag=1;
}
return 0;
}
0510作业
于 2024-05-10 19:21:25 首次发布