我需要有关此示例应用程序的帮助。当我运行它时,它在子进程打印“Child Sending!”后卡住了。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#define INPUT 0
#define OUTPUT 1
int main()
{
int fd1[2];
int fd2[2];
int pid;
if (pipe(fd1) < 0)
exit(1);
if (pipe(fd2) < 0)
exit(1);
if ((pid = fork()) < 0)
{
perror("fork");
exit(1);
}
else if (pid == 0)
{
close(fd1[INPUT]);
close(fd2[OUTPUT]);
char *str = "Hello World!";
printf("Child sending!\n");
write(fd1[OUTPUT], str, strlen(str));
char *bufferc = (char *)malloc(1000);
char *readbufferc = (char *)malloc(80);
int rdc;
int gotdata = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在两个 java 程序之间传输文本。为了简单起见,我展示这段代码:
import java.io.DataInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
DataInputStream stdin = new DataInputStream(System.in);
String completeText = "";
while (stdin.available() > 0) {
byte[] tempByte = { stdin.readByte() };
completeText += new String(tempByte);
}
System.out.println(completeText);
}
}
Run Code Online (Sandbox Code Playgroud)
在 Linux 或 Windows 上执行以下操作时,文本似乎被省略,就好像管道被阻塞或随机丢失一样。有时一切都会通过,有时则不然:
echo "omg wtf" | java Test | java Test
Run Code Online (Sandbox Code Playgroud)
对此有什么想法吗?CPU 速度越慢,文本通过的频率似乎越高。当输入从 java System.out.println() 传送时,“available”是否会因任何原因返回错误的结果?
干杯!
这可能是新手逃避问题。我正在尝试在这样的 for 循环中运行命令
$ for SET in `ls ../../mybook/WS/wsc_production/`; do ~/sandbox/scripts/ftype-switch/typesort.pl /media/mybook/WS/wsc_production/$SET ./wsc_sorter/$SET | tee -a sorter.log; done;
Run Code Online (Sandbox Code Playgroud)
但我最终还是sorter.log空虚了。(我确定有一些输出。)如果我转义管道符号 ( \|),我最终会得到任何输出sorter.log。
我究竟做错了什么?
$ bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Run Code Online (Sandbox Code Playgroud)
编辑:哎呀,/media/mybook/ 睡着了,所以实际上没有输出。该代码首先是正确的。不过还是谢谢大家的评论。
当命令将用户输入作为其某些参数时,我使用 fork()/exec()/wait() 而不是 system(),因此用户不能输入类似...
&rm -rf /home/* && echo HAHA
Run Code Online (Sandbox Code Playgroud)
...作为一个论点。
我假设 popen 与 system() 一样危险,因为它需要单个字符串,而不是像 exec 系列函数那样需要字符串列表。
不过,我只能从 exec 函数获取返回值。是否有一个“安全”版本的 popen,我可以使用用户输入运行并在父进程中处理 stdout / stderr ?
可能有人问过这个问题,但我找不到。在 R 中,我想使用 | 管道而不是 %>%。
我已经习惯了 Linux,如果我能用 dplyr 或类似的东西做到这一点,我会很高兴。
是否可以使用此管道运算符?
df | filter(state == "New York")
Run Code Online (Sandbox Code Playgroud) 我有这个命令
$ sha1sum file.bin | cut -f1 -d " "
Run Code Online (Sandbox Code Playgroud)
计算某些 eeprom 内容的 sha1sum。管道后面跟着 cut 是为了去掉输出中的文件位置,只检索 sha1。
我的问题是我需要N在file.bin. 该文件是一个 32 字节(固定长度)行的表/数组。
我知道我必须输入一些head -n N,但不知道如何进行重定向。语法是head -n N <file>.
我天真地尝试过,sha1sum file.bin | cut -f1 -d " " | head -n 8但没有用。我需要 shell 以某种方式理解这一点:sha1sum(head -n 8 file.bin) | cut -f1 -d " ",但我猜我们不能在 shell 中使用数学函数语法......
请问怎么做?问候
我想生成大量的随机数。我编写了以下 bash 命令(请注意,我在cat这里使用是出于演示目的;在我的实际用例中,我将数字通过管道传输到进程中):
for i in {1..99999999}; do echo -e "$(cat /dev/urandom | tr -dc '0-9' | fold -w 5 | head -n 1)"; done | cat
Run Code Online (Sandbox Code Playgroud)
数字以非常低的速度打印。但是,如果我生成较小的数量,它会快得多:
for i in {1..9999}; do echo -e "$(cat /dev/urandom | tr -dc '0-9' | fold -w 5 | head -n 1)"; done | cat
Run Code Online (Sandbox Code Playgroud)
请注意,唯一的区别是9999而不是99999999。
为什么是这样?数据是否在某处缓冲?有没有办法优化这一点,以便随机数cat立即通过管道/流传输?
library(tidyverse)
library(data.table)
dt <- data.table(x=1:3)
dt[x==1]
myfun <- function(d) d[x==1,x:=NA]
dt2 <- dt %>% myfun
dt[x==1]
Run Code Online (Sandbox Code Playgroud)
在这个例子中,dt(一个data.table)作为参数通过管道发送给一个函数(myfun)。然后将结果保存到对象 dt2 中。
为什么 dt 被修改?(如您所见,第 1 行中 x 的值从 1 变为 NA)
我想从 C 程序运行外部命令。假设,作为最小的工作示例,我想运行“cat”命令。我使用 fork() 和 execl() 来生成新进程,并通过管道与它通信。
现在这就是我的问题所在。在终端中,我会通过按 CTRL-D 告诉“cat”我已完成输入。在这里,我试图通过关闭文件描述符来做到这一点——请参阅下面代码中带有 close(outpipefd[1]) 的行——但这似乎不起作用。我的代码停止,因为“猫”正在等待更多输入。
我的代码如下...我做错了什么?提前致谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
int main(void)
{
pid_t pid=0;
int inpipefd[2];
int outpipefd[2];
/*
We create the pipes for communicating with the child process
*/
pipe(inpipefd);
pipe(outpipefd);
if((pid=fork())==0)
{
/*
Child
*/
dup2(outpipefd[0],STDIN_FILENO);
dup2(inpipefd[1],STDOUT_FILENO);
dup2(inpipefd[1],STDERR_FILENO);
/*
We spawn the process
*/
execl("/bin/cat","cat",(char *)(NULL));
/*
Nothing below this line should be executed by child process.
If so, it …Run Code Online (Sandbox Code Playgroud) 如果标准输入为空,我想打破整个管道。我尝试结合 xargs -r 和 tee,这意味着如果 stdin 为空则不打印和写入,但它失败了
...| upstream commands | xargs -r tee output.txt | downstream commands | ...
任何反馈表示赞赏。