网名的特殊符号网名大全翅膀号

进程间通信(PIPE调用) - 博客频道 - CSDN.NET
取而代之的专栏
菜鸟开始写博客
分类:LINUX
看过了高级的popen调用之后,我们来了解一下底层的pipe函数。popen函数用过启动一个shell来解释请求的命令。而pipe不需要启动shell来解释。
#include&unistd.h&
int pipe(int file_descriptor[2]);
pipe函数的参数是一个由两个整数类型的文件描述符组成的数据的指针。该函数是数据中填上两个新的文件描述符后返回0.如果失败则返回-1并设置
errno来表明失败的原因。这些错误有
1.EMFILE:进程使用的文件描述符过多
2.ENFILE:系统的文件表已满
3.EFAULT:文件描述符无效
两个返回的文件描述符以一种特殊的方式连接起来。写到file_descriptor[1]的所有数据都可以从file_descriptor[0]读回来。数据基于先进先出的原则
(通常简写为FIFO)进程处理,这意味着如果你把字节1,2,3写入到dile_descriptor[1],从file_descriptor[0]读出来的数据是1,2,3.
#include &unistd.h&
#include &stdlib.h&
#include &stdio.h&
#include &string.h&
int main()
int file_pipes[2];
const char some_data[] = &123&;
char buffer[BUFSIZ + 1];
memset(buffer, '\0', sizeof(buffer));
if (pipe(file_pipes) == 0) {
data_processed = write(file_pipes[1], some_data, strlen(some_data));
printf(&Wrote %d bytes\n&, data_processed);
data_processed = read(file_pipes[0], buffer, BUFSIZ);
printf(&Read %d bytes: %s\n&, data_processed, buffer);
exit(EXIT_SUCCESS);
exit(EXIT_FAILURE);
跨越fork调用的管道
#include &unistd.h&
#include &stdlib.h&
#include &stdio.h&
#include &string.h&
int main()
int file_pipes[2];
const char some_data[] = &123&;
char buffer[BUFSIZ + 1];
pid_t fork_
memset(buffer, '\0', sizeof(buffer));
if (pipe(file_pipes) == 0) {
fork_result = fork();
if (fork_result == -1) {
fprintf(stderr, &Fork failure&);
exit(EXIT_FAILURE);
// We've made sure the fork worked, so if fork_result equals zero, we're in the child process.
if (fork_result == 0) {
data_processed = read(file_pipes[0], buffer, BUFSIZ);
printf(&Read %d bytes: %s\n&, data_processed, buffer);
exit(EXIT_SUCCESS);
// Otherwise, we must be the parent process.
data_processed = write(file_pipes[1], some_data,
strlen(some_data));
printf(&Wrote %d bytes\n&, data_processed);
exit(EXIT_SUCCESS);
父进程与子进程
#include &unistd.h&
#include &stdlib.h&
#include &stdio.h&
#include &string.h&
int main()
int file_pipes[2];
const char some_data[] = &123&;
char buffer[BUFSIZ + 1];
pid_t fork_
memset(buffer, '\0', sizeof(buffer));
if (pipe(file_pipes) == 0) {
fork_result = fork();
if (fork_result == (pid_t)-1) {
fprintf(stderr, &Fork failure&);
exit(EXIT_FAILURE);
if (fork_result == 0) {
sprintf(buffer, &%d&, file_pipes[0]);
(void)execl(&pipe4&, &pipe4&, buffer, (char *)0);
exit(EXIT_FAILURE);
data_processed = write(file_pipes[1], some_data,
strlen(some_data));
printf(&%d - wrote %d bytes\n&, getpid(), data_processed);
exit(EXIT_SUCCESS);
// The 'consumer' program, pipe4.c, that reads the data is much simpler.
#include &unistd.h&
#include &stdlib.h&
#include &stdio.h&
#include &string.h&
int main(int argc, char *argv[])
char buffer[BUFSIZ + 1];
memset(buffer, '\0', sizeof(buffer));
sscanf(argv[1], &%d&, &file_descriptor);
data_processed = read(file_descriptor, buffer, BUFSIZ);
printf(&%d - read %d bytes: %s\n&, getpid(), data_processed, buffer);
exit(EXIT_SUCCESS);
querdaizhi
排名:第14727名
(1)(1)(1)(1)(3)(5)(5)(1)(37)(3)(5)(2)(4)2010年 总版技术专家分年内排行榜第一2009年 总版技术专家分年内排行榜第一
2011年 总版技术专家分年内排行榜第二
本帖子已过去太久远了,不再提供回复功能。1213人阅读
USB的pipe的两端分别指的是USB host端的内存区域,和设备端的endpoint。
pipe分为两类,一类是stream pipe, 另一类是message pipe。
两类的主要区别是,message pipe发送的数据,都是usb协议里规定好的结构,而steam pipe发送的数据则没有什么要求。
还有一个重要的概念就是,message pipe要求IN和OUT,而且必须是一个endpoint支持IN,OUT,也就是要求全双工。所以endpoint 0的control transfer就总是message pipe。但是只能是endpoint 0可以使用control transfer么?其实不是,除了endpoint 0之外,其实还可以有其他endpoint支持全双工,这就根据设备的支持了,一般这么用的比较少。下面介绍control transfer的时候会介绍到。
USB协议规定了4种transfer,分别为control transfer, isochronous transfer, bulk transfer, interrupt transfer.
一个transfer由多个transaction组成,一个transaction由多个packet组成。
control transfer
一个control transfer由SETUP, DATA, STATUS三种transaction组成,其中SETUP是由host -& device, STATUS是由device -& host。DATA是可有可无的,DATA的传输方向也是由SETUP里去指定的。
control trnasfer属于message pipe, 里面传输的数据都是定义好了的结构体,比如set configuration之类。
一个device必须有一个control transfer,也就是使用的endpoint0,但是device不见得只有一个control transfer,也可以有其他的全双工的endpoint去运作附加的control transfer。
SETUP transaction的数据都是USB协议定义好的结构体,这个结构体允许vendor指定自己的专门设备的一些command。DATA transaction如果有的话,就跟在SETUP后面,其也有一个USB定义好的结构体,除非传输的是vedor指定的信息。最后是STATUS transaction,这个也是有USB协议规定好的。
基于control transfer的endpoint决定了最大的packet大小,full-speed的设备为8, 16, 32, 64个bytes, high-speed设备支持64 bytes, low-speed为8bytes.这个包的最大size决定于wMaxPacketSize。这个wMaxPacketSize是在device descriptor里,所以正确的读取顺序为,先读8个bytes,解析后得到wMaxPacketSize后,再去用真实的size继续操作。
还有一点,这个wMaxPacketSize,也就是max packet大小,指的是DATA transaction里的真实data filed的size。像SETUP,STATUS这些transaction,都是USB定义好的,所以size也是固定的,所以就跟这个最大size没有关系了。如果传输的数据大于packet的max size里,那就分多个packet。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:43074次
排名:千里之外
转载:43篇
(1)(1)(3)(1)(1)(1)(1)(2)(2)(1)(2)(7)(2)(4)(5)(5)(4)

我要回帖

更多关于 网名特殊符号漂亮的 的文章

 

随机推荐