当前位置:首页 » 操作系统 » linuxpipe

linuxpipe

发布时间: 2022-03-04 22:52:37

linux pipe 判断是否有数据

判断是否为空可以用if来判断
if [ "$str" = "" ]

❷ 请教!LINUX下的pipe和fork输出的问题,高分悬赏,有加分!

你的问题其实很简单,就是一个多进程同步的问题, 解决同步问题有很多种方法,最常见的是用信号量,消息队列等。既然你题目要求使用pipe,我就在下面的例子中用pipe来实现。 思路很简单,通过两个pipe实现 父子进程间的同步, 父进程总是向第一个pipe写,从第二个pipe读,子进程相反。 当进程读到内容后,则它可以print,print完一个字符后,它写另一个pipe来通知另一个进程print。依次交替。
在我的例子中,由父进程控制总共打印60个字符后结束(两个进程分别打印30次)。
这个例子很简单,就没什么需要再多解释的了,自己看吧,如果对这个例子还有什么不懂的,可以 hi我。 别忘加分, :)

#include <stdio.h>
#include <unistd.h>

/* delay 500ms for each print */
#define DELAY_TIME (1000*500)

int main()
{
int pid, i = 0, cmd;
int p1[2], p2[2];
pipe(p1);
pipe(p2);

pid = fork();

if (pid > 0)
{
/* parent */
close(p1[0]);
close(p2[1]);

for (i = 0; i < 30; i++)
{
printf("%d", (i%10));
fflush(stdout);
usleep(DELAY_TIME); /* sleep to make print slow */

/* write to p1 to tell child continue */
cmd = i;
write(p1[1], &cmd, sizeof(cmd));

/* read from p2 to wait for child complete */
read(p2[0], &cmd, sizeof(cmd));
}

/* -1 to tell child quit */
cmd = -1;
write(p1[1], &cmd, sizeof(cmd));
wait(NULL);
printf("Parent done, quit...\n");
}
else
{
close(p1[1]);
close(p2[0]);

while(read(p1[0], &cmd, sizeof(cmd)) == sizeof(cmd)
&& cmd >= 0)
{
i = cmd;
printf("%c", 'A'+(i%26));
fflush(stdout);
usleep(DELAY_TIME); /* sleep to make print slow */
/* write to p2 to tell parent continue */
write(p2[1], &cmd, sizeof(cmd));
}
printf("\n\nChild receive finish command from parent, quit...\n");
}

return 0;
}

❸ Linux 中 gcc -v ,-### ,-pipe ,-combine 分别是意思,怎么用

一次问的真不少,不过都不怎么常用。
使用man gcc来找答案。
终端里man gcc后键入
/pipe来查找,然后按n查找下一个。
重复以上动作查找/v /### /combine
-v
这个一般是查看版本信息的。
-pipe
Use pipes rather than temporary files for communication between the
various stages of compilation. This fails to work on some systems
where the assembler is unable to read from a pipe; but the GNU
assembler has no trouble.(在不同的编译阶段使用管道替代临时文件,某特别汇编器无法从管道读取的系统无法使用这个参数,但是GNU的汇编器没有这个错误)
-###
Like -v except the commands are not executed and arguments are
quoted unless they contain only alphanumeric characters or "./-_".
This is useful for shell scripts to capture the driver-generated
command lines. (跟-v一样)

奇怪。我没查到combine ????

❹ linux pipe();父进程需要close(fd[0]);子进程close(fd[1]);为什么都需要关闭一个

管道里面是字节流,父子进程都写、都读,就会导致内容混在一起,对于读管道的一方,解析起来就比较困难。常规的使用方法是父子进程一方只能写入,另一方只能读出,管道变成一个单向的通道,以方便使用。

❺ linux中的pipe和fifo的区别

linux中的pipe和fifo的区别
在linux进程间通信(IPC)可以通过信号量、文件系统、消息队列、共享内存还有管道来实现的。其中消息队列、内存管理是在System V中提出的。
进程通信间涉及到了管道,而且管道在shell命令中也大有用处。那就简要说说管道:
管道顾名思义,你可以将其理解为日常生活中的管子,一边流入,一边流出。它可以有半双工和全双工。半双工就是只能一边流入,另一边流出;全双工则是一边可以流入,也可以流出。
pipe就是一种半双工的管道。其中,fd[1] 用来向管道中写入数据,而fd[0]在另一端用来读出数据。如果现有两个进程要利用pipe进行通信。此时,就要保证只能有一个写入端和一个读出端,即:fd[1]和fd[0]只能有一个。
如下程序:
/*实现子进程向管道中写入数据,父进程读出数据*/

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
pid_t childpid;
int fd[2],nbytes;
char string[]="data from child process\n";
char buf[100];

if(pipe(fd)<0)
{
perror("pipe");
exit(1);
}
if((childpid=fork())==-1)
{
perror("fork");
exit(1);
}
if(childpid==0)
{
close(fd[0]);
printf("childpid =%2d\n",getpid());
write(fd[1],string,strlen(string));
exit(0);
}
else
{
close(fd[1]);
printf("parentpid =%2d\n",getppid());
nbytes=read(fd[0],buf,sizeof(buf));
printf("Received string:%s\n",buf);
}
return 0;
}
下面来说道fifo:
fifo是一种全双工,即:它的一端既可以进行读取fd[0],也可以进行写入fd[1]。
正因为它的这种通信方式,使其可以用来涉及基于C/S模式的网络通信。具体做法:
首先让服务器产生一个服务器端的FIFO,然后让各个客户端产生以其PID为名称的客户端的FIFO,在客户于服务器进行通信时,客户端向服务器端发送自己的PID,以使服务器对客户的请求进行响应时,向其客户端的FIFO写入响应信息。代码实现客户端和服务器进行各自的名称和PID交换。

❻ linux/unix 的 shell编程里 pipe功能指的是什么功能

管道功能,简单说就是将一个命令的输出传递给另一个命令作为输入或写入某个文件。就是命令间的数据交换渠道。

热点内容
在同一路由器下如何访问服务器 发布:2024-09-23 20:55:41 浏览:556
天逸哪个配置带电子挡杆 发布:2024-09-23 19:51:22 浏览:547
sqrt在c语言中什么意思 发布:2024-09-23 19:50:04 浏览:507
京东羊毛服务器搭建 发布:2024-09-23 19:33:39 浏览:9
服务器的远程端口被关了如何打开 发布:2024-09-23 18:33:22 浏览:229
phpjs注入 发布:2024-09-23 18:31:51 浏览:596
高性能php应用开发 发布:2024-09-23 18:23:56 浏览:209
广东云存储空间开发 发布:2024-09-23 18:21:47 浏览:384
易语言怎么架服务器 发布:2024-09-23 18:21:46 浏览:790
hibernate缓存清除缓存 发布:2024-09-23 18:11:01 浏览:365