美文网首页
Android 进程间使用管道通信

Android 进程间使用管道通信

作者: Nothing_655f | 来源:发表于2020-12-25 16:29 被阅读0次

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: <http://www.gnu.org/software/coreutils/>
       General help using GNU software: <http://www.gnu.org/gethelp/>
       Report mkfifo translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2013 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       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.

相关文章

网友评论

      本文标题:Android 进程间使用管道通信

      本文链接:https://www.haomeiwen.com/subject/umhinktx.html