4月·15的作业

write_fifo.c

 1	#include <stdio.h>
     2	#include <string.h>
     3	#include <stdlib.h>
     4	#include <syspes.h>
     5	#include <sys/stat.h>
     6	#include <errno.h>
     7	#include <fcntl.h>
     8	#include <unistd.h>
     9	int main(int argc, const char *argv[])
    10	{
    11		int fd;
    12		char buf[1024] = "";
    13		if(argc != 2)
    14		{
    15			fprintf(stderr,"usage:%s\n",argv[0]);
    16			return -1;
    17		}
    18		fd = open(argv[1],O_WRONLY);
    19		if(fd < 0)
    20		{
    21	
    22			perror("fail to open");
    23			return -1;
    24		}
    25		while(1)
    26		{
    27			
    28			fgets(buf,sizeof(buf),stdin);
    29			write(fd,buf,strlen(buf));
    30			if(!strncmp(buf,"quit",4))
    31			{
    32				exit(0);
    33			}
    34		}
    35		
    36		return 0;
    37	}

read_fifo.c

  1	
     2	#include <stdio.h>
     3	#include <string.h>
     4	#include <stdlib.h>
     5	#include <syspes.h>
     6	#include <sys/stat.h>
     7	#include <errno.h>
     8	#include <fcntl.h>
     9	#include <unistd.h>
    10	
    11	int main(int argc, const char *argv[])
    12	{
    13		char buf[1024];
    14		int fd;
    15		if(argc != 2)	
    16		{
    17			fprintf(stderr,"usage:%s fifo",argv[0]);
    18			return -1;
    19		
    20		}
    21		fd = open(argv[1],O_RDONLY);
    22		if(fd < 0)
    23		{
    24			perror("fail to open");
    25			return -1;
    26		}
    27		while(1)
    28		{
    29			memset(buf,0,sizeof(buf));
    30			read(fd,buf,sizeof(buf));
    31			printf("read>>%s",buf);
    32			if(!strncmp(buf,"quit",4))
    33			{
    34				exit(0);
    35			}
    36		}
    37		return 0;
    38	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值