fifo_write和fifo_read

fifo_write

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:
Funcion List: 
*****************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <memory.h>
#include <stdlib.h>

#define FIFOSERVER "/tmp/fifoserver"

int main()
{
	int fd;
	char w_buf[100];
	int real_wnum;

	memset(w_buf,0,100);

	if(mkfifo(FIFOSERVER,O_CREAT) < 0)
	{
		perror("mkfifo error!");
		exit(-1);
	}

	    fd = open(FIFOSERVER,O_WRONLY | O_NONBLOCK);
        //fd = open(FIFOSERVER, O_WRONLY);

		if(-1 == fd)
		{
			perror("open error!");
			exit(-1);
		}

		printf("please input message:\n");
		scanf("%s",w_buf);

		real_wnum = write(fd,w_buf,100);

		if(-1 == real_wnum)
		{
			perror("write error!");
			exit(-1);
		}

		close(fd);

    return 0;
}

fifo_write

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:
Funcion List: 
*****************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <memory.h>

#define FIFOSERVER "/tmp/fifoserver"

int main(int argc,char ** argv)
{
	char r_buf[100];
	int fd;
	int r_size;
	int ret_size;

	r_size = atoi(argv[1]);

	printf("require real read bytes is %d\n",r_size);

	memset(r_buf,0,100);

	fd = open(FIFOSERVER,O_RDONLY);
	//fd = open(FIFOSERVER,O_RDONLY | O_NONBLOCK);
	
	if(-1 == fd)
	{
		perror("open error!");
		exit(-1);
	}

    ret_size = read(fd,r_buf,r_size);

	if(-1 == ret_size)
	{
		perror("read error!");
		exit(-1);
	}
    
	printf("real read %d bytes ,is %s\n",ret_size,r_buf);

	unlink(FIFOSERVER);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值