Tag: pipe

C – 通过管道传递管道

我正在为学校做一个项目,我不确定我试图解决它的方式是否可行。 该项目涉及制作一个程序,分离2个孩子,然后必须用其他程序替换他们的pid,并让2个孩子通过使用read()和write()通过管道进行交谈。 我的问题是使用execve并将管道传递给那个孩子。 我现在拥有的是: 家长计划 – 分叉和让孩子打电话: #include #include #include #include #include #include #define BUF_SIZE 16 /* 2 cmd line arguments 1. file already in system (to be copied) 2. file to be created (the copy) create pipe (for communication) + 2 child processes first child replace pid with reader second child with writer wait […]

execve(“/ bin / sh”,0,0); 在管道里

我有以下示例程序: #include int main(int argc, char ** argv){ char buf[100]; printf(“Please enter your name: “); fflush(stdout); gets(buf); printf(“Hello \”%s\”\n”, buf); execve(“/bin/sh”, 0, 0); } 我和我在没有任何管道的情况下运行它应该工作并返回一个sh promt: bash$ ./a.out Please enter your name: warning: this program uses gets() which is unsafe. testName Hello “testName” $ exit bash$ 但这不适用于管道,我想我知道为什么会这样,但我无法找到解决方案。 示例运行波纹管。 bash$ echo -e “testName\npwd” | ./a.out Please […]

为什么管道在Windows / unix / linux中被认为是危险的?

为什么管道被认为是危险的? 可以采取哪些措施来避免这些安全问题? 我最感兴趣的是Windows,但如果您有其他操作系统信息,请提供。

在C中逐行读取管道

如何分离来自管道的线。 在管道中有这样的文字: HALLO:500\n TEST:300\N ADAD ADAWFFA AFAGAGAEG 我想将管道与管道分开,因为我想将值保存在变量中。 这是我的c代码: #include #include #define BUFFERSIZE 1 int main(int argc, char **argv){ unsigned char buffer[BUFFERSIZE]; FILE *instream; int bytes_read=0; int buffer_size=0; buffer_size=sizeof(unsigned char)*BUFFERSIZE; /* open stdin for reading */ instream=fopen(“/dev/stdin”,”r”); /* did it open? */ if(instream!=NULL){ /* read from stdin until it’s end */ while((bytes_read=fread(&buffer, buffer_size, 1, instream))==buffer_size){ […]

使用stdin和stdout使用Java与外部C程序通信

我要做的是在Java应用程序中启动C程序可执行文件,并允许它们使用stdin和stdout相互通信。 C程序将等待来自Java应用程序的命令并回显它。 我用“gnugo –mode gtp”测试了java代码(gnugo在gtp模式下与stdin和stdout通信)并且它工作正常,但我的C代码不起作用。 任何建议都会非常感激。 C代码 #include #include #include int main(void) { unsigned int byte_read; char *string, *tok; int cmd_id; int len = 64; string = (char *) malloc(len + 1); while (1) { byte_read = getline(&string,&byte_read, stdin); if (byte_read == -1) { printf(“Error reading input\n”); free(string); exit(0); // } else { printf(“Got command: […]

使用系统调用fork()的多个管道实现execvp()wait()pipe() – 它根本不起作用

我需要实现处理多个管道命令的shell。 例如,我需要能够处理这个: ls | grep -i cs340 | sort | uniq | cut -c 5 ls | grep -i cs340 | sort | uniq | cut -c 5 ls | grep -i cs340 | sort | uniq | cut -c 5 。 我假设问题是我没有将前一个命令的输出传递给下一个命令的输入。 当我执行我的代码时,它没有给我输出。 我正在使用这个伪代码: for cmd in cmds if there is a next cmd […]

C中的这个多管道代码是否有意义?

几天我就提出了一个问题 。 我的解决方案符合接受的答案中的建议。 但是,我的一个朋友提出了以下解决方案: 请注意,代码已更新几次(查看编辑修订版)以反映下面答案中的建议。 如果您打算给出新的答案,请考虑这个新代码,而不是那些有很多问题的旧代码。 #include #include #include #include int main(int argc, char *argv[]){ int fd[2], i, aux, std0, std1; do { std0 = dup(0); // backup stdin std1 = dup(1); // backup stdout // let’s pretend I’m reading commands here in a shell prompt READ_COMMAND_FROM_PROMPT(); for(i=1; i 1) { dup2(aux, 0); close(aux); } […]

将两个命令与管道组合在一起

我试图在一个(不存在的)命令中“合并”两个命令并管道它。我的意思是..假设你有这两个命令: grep text out.txt grep text out.txt ,可以表示这两个命令的(不存在)命令可能类似于(grepwc -l < file.txt) ,然后将行数输出到out.txt中。 基本上这些(grepwc)命令应该与grep text out.txt具有相同的行为 grep text out.txt但更短。 我尝试过这样的事情,但我认为我的方法远远不能实现目标。 我使用名为commlist的结构,其中包含已由cmd,argc和argv解析的命令。 inputfile和outputfile是open()上使用的文件的路径名。 我正在使用的结构。 typedef struct command { char *cmd; int argc; char *argv[MAXARGS+1]; struct command *next; } COMMAND; 和代码: void execute(COMMAND *commlist) { int fd[2]; pid_t pid; int n_pipes=2; //pipes needed COMMAND *aux = commlist; int […]

在C中遇到fork(),pipe(),dup2()和exec()时遇到问题

这是我的代码: #include #include #include #include #include #define NUMPIPES 2 int main(int argc, char *argv[]) { char *bBuffer, *sPtr, *aPtr = NULL, *pipeComms[NUMPIPES], *cmdArgs[10]; int fdPipe[2], pCount, aCount, i, status, lPids[NUMPIPES]; pid_t pid; pipe(fdPipe); while(1) { bBuffer = readline(“Shell> “); if(!strcasecmp(bBuffer, “exit”)) { return 0; } sPtr = bBuffer; pCount = -1; do { aPtr = […]