Android之BitTube

概述

在Android的Sensor框架代码里看到BitTube,这里对其进行学习整理.
BitTube是用来处理进程间通讯的机制,和管道类似,主要是对socketpair的封装.先了解一下socketpair

socketpair

socketpair用来创建一对未命名、互相连接的套接字,套接字的一端可以进行读和写的操作,用来实现全双工的通讯.
函数原型:int socketpair(int domain, int type, int protocol, int sv[2]);
参数说明
1.domain在linux平台上只能使用AF_UNIX
2.type表示socket的类型,目前支持如下类型

/**
 * enum sock_type - Socket types
 * @SOCK_STREAM: stream (connection) socket
 * @SOCK_DGRAM: datagram (conn.less) socket
 * @SOCK_RAW: raw socket
 * @SOCK_RDM: reliably-delivered message
 * @SOCK_SEQPACKET: sequential packet socket
 * @SOCK_DCCP: Datagram Congestion Control Protocol socket
 * @SOCK_PACKET: linux specific way of getting packets at the dev level.
 *                For writing rarp and other similar things on the user level.
 *
 * When adding some new socket type please
 * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
 * overrides this enum for binary compat reasons.
 */

3.protocol 目前必须为0
4.sv[2] 代表两个互相连接的套接字.

通过socketpair实现不同进程之间的通讯极其简单,代码如下

//test in ubuntu 14.04 by helenxr
//step1:gcc socketpair.c
//step2:./a.out

/*test result like below:
create socketpair[3,4]
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:paren
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值