android管道使用方法,Android 进程间使用管道通信

Android 进程间使用管道通信

实现比较简单,直接上代码就可以理解了

#define PATH_CMD_FIFO "/data/misc/my_fifo"

/* read pipe from path of PATH_CMD_FIFO

* if read data from pipe, and then send data to uart port

*/

static void * read_fifo_thread(void *arg)

{

int ret = 0;

int fd = 0;

char buf[512] = {0};

ALOGD("%s Enter\n", __FUNCTION__);

if (mkfifo(PATH_CMD_FIFO, O_CREAT | O_EXCL | 0777) < 0

&& errno != EEXIST) {

ALOGE("%s creat fifo server error\n", __FUNCTION__);

goto err_ret;

}

fd = open(PATH_CMD_FIFO, O_CREAT | O_RDONLY | O_NONBLOCK, 0);

if (fd < 0) {

ALOGE("%s open fd error=%s\n", __FUNCTION__, strerror(errno));

goto err_ret;

}

ALOGD("%s fd:%d\n", __FUNCTION__, fd);

while (!stop_flag) {

memset(buf, 0x0, sizeof(buf));

ret = read(fd, buf, sizeof(buf));

if (ret > 0) {

// Do something

}

usleep(1000*1000);

}

err_ret:

unlink(PATH_CMD_FIFO);

pthread_exit(NULL);

}

1、注意 mkfifo 的时候的权限问题,我这边用的是 0777,根据对应的情况而定

2、检查是否生成了管道文件

可以看到在 /data/misc/my_fifo 有这个文件生成

3、读写调试

echo "hello world" > /data/misc/my_fifo

上面的线程中就可以读到这个信息

5、使用mkfifo bin调试

另外调试过程中可以直接在板子上使用mkfifo 命令

NAME

mkfifo - make FIFOs (named pipes)

SYNOPSIS

mkfifo [OPTION]... NAME...

DESCRIPTION

Create named pipes (FIFOs) with the given NAMEs.

Mandatory arguments to long options are mandatory for short options too.

-m, --mode=MODE

set file permission bits to MODE, not a=rw - umask

-Z, --context=CTX

set the SELinux security context of each NAME to CTX

--help display this help and exit

--version

output version information and exit

AUTHOR

Written by David MacKenzie.

REPORTING BUGS

Report mkfifo bugs to bug-coreutils@gnu.org

GNU coreutils home page:

General help using GNU software:

Report mkfifo translation bugs to

COPYRIGHT

Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later .

This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

mkfifo(3)

The full documentation for mkfifo is maintained as a Texinfo manual. If the info and mkfifo programs are properly installed at your

site, the command

info coreutils 'mkfifo invocation'

should give you access to the complete manual.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值