1.用文件工o以读写的方式打开一个文件
2.利用fork ()函数创建子进程
3.父进程循环从键盘输入数据写入到文件,子进程利用usleep (200)休眠200us,然后循环从文件中读取数据打印到屏幕上。,如果父进程输入"quit",父子进程结束
提示
(1)用文件IO,以读写的方式打开文件.
(2)利用fork创建子进程。父进程利用fegets从键盘输入数据,写到文件中,书写完毕后用lseek()定位到头部。若是输入“quit”结束循环.
(3)子进程循环调用read函数,读取数据。若是读到的数据是0,说明读到文件尾部,调用continue继续读。否则若是读取数据。正常数据。若是读到"quit",结束读取循环,书写完毕后,调用lseek()定位到头部。若是用户输入“quit",结束输入循环。
程序:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
void write_fd(int fd)
{
char buf[100]={0};
int n=0;
while(1)
{
memset(buf,0,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
n=write(fd,buf,strlen(buf));
lseek(fd,-n,SEEK_CUR);
if(strncmp(buf,"quit",4)==0)//
break;
}
}
void