Tag:

强制使用`exec`创建的程序执行无缓冲的I / O.

我正在尝试使用pipe , fork和exec与C中的外部程序进行交互。 我想强制外部程序执行无缓冲的I / O. 这是我的代码到目前为止的相关代码段: … pid = fork(); if (pid == (pid_t) 0) { /* child process */ /* make pipe connections to standard streams */ dup2(wpipe[0], STDIN_FILENO); dup2(rpipe[1], STDOUT_FILENO); /* close pipe endings */ close(wpipe[0]); close(rpipe[0]); close(wpipe[1]); close(rpipe[1]); /* unbuffered I/O */ setvbuf(stdin, NULL, _IONBF, BUFSIZ); setvbuf(stdout, NULL, _IONBF, BUFSIZ); if […]

Win32流句柄 – 更改为二进制模式

我正在使用CreatePipe创建句柄,然后在启动子进程时传递。 子进程将二进制信息写入其标准输出,该标准输出被重定向到我创建的管道中。 然而,当写入数字’10’时,事情变得混乱,我得到太多的输出字符 – 我假设这是因为流在文本模式下打开并且它自动添加13。 在Win32中是否有任何方法可以从CreatePipe返回HANDLE并将流模式更改为二进制,就像我使用函数_setmode一样,如果我有一个FILE *? 或者有没有办法将他的句柄翻译成文件*所以我可以使用_setmode? 示例代码: HANDLE hOutputReadTmp,hInputWriteTmp; SECURITY_ATTRIBUTES sa; bool Binary = true; // Set up the security attributes struct. sa.nLength= sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; if (!CreatePipe(&hInputRead,&hInputWriteTmp,&sa,0)) { Error = “Unable to Create Input Pipe”; return false; }; if (Binary == true) { //Put a magic something here […]

用于多个流程的管道

目前正在做一些功课,并且很难过。 目标是生成100,000个数字并将它们全部加在一起,方法是将工作分成10个进程(每个10,000个数字) 我想我已经想出了如何分叉进程(希望如此),但是使用Pipe()来传递每个子进程的小计不起作用……下面的程序为每个子进程返回44901,为运行总计返回449010。 我努力奋斗但我觉得这很简单,我应该能够理解。 main() { int i; pid_t pid; int status = 0; int fd[2]; int runningTotal = 0; pipe(fd); int t; int r; for (i = 0; i < 10; i++) { pid = fork(); if (pid == 0){ close(fd[0]); t = ChildProcess(); write(fd[1], &t, sizeof(t)); exit(0); } close(fd[1]); read(fd[0], &r, sizeof(r)); runningTotal […]

管道作为stdin / stdout进程通信。

我正在学习管道而且我遇到了问题。 我希望我的程序能够像: grep [word to find] [file to search] | grep -i [without word] | wc -l grep [word to find] [file to search] | grep -i [without word] | wc -l它编译并且没有错误地工作,但它没有输出(至少不在stdout上,因为我希望它做)。 奇怪的是,当我尝试在最后一个分叉中打印它时,它会在stdin上打印出来。 我没有改变这个叉子或parrent过程中的标准输出因此对我来说似乎很奇怪。 我正在尝试关闭未使用的管道并刷新stdout(它还在做什么吗?),但可能还有更多要做的事情。 #include #include #include void help() { printf( “Usage of the program:\n” “\t./alagrep [fileToSearch] [wordToFind] [wordToExpel]\n”); } int main(int argc, char […]

通过管道从子proc发送到父级的多维数组

我正在尝试将子进程中的二维数组发送到父进程并且非常失败。 不完全确定应如何做,但这是我尝试过的。 #include #include #include #include #include #include int fd[2]; int matrix[2][2]; int main () { pipe (fd); if (0 == fork()) { printf (“Start child process with pid: %d\n”, getpid()); for (int i = 0; i < 2; i++) matrix[i][i] = 1; write (fd[1], matrix, 4); exit (0); } printf ("Start parent process with […]

这个打印订单有什么问题

看看这段代码: #include #include int main() { int pipefd[2],n; char buf[100]; if(pipe(pipefd)<0) printf("Pipe error"); printf("\nRead fd:%d write fd:%d\n",pipefd[0],pipefd[1]); if(write(pipefd[1],"Hello Dude!\n",12)!=12) printf("Write error"); if((n=read(pipefd[0],buf,sizeof(buf)))<=0) printf("Read error"); write(1,buf,n); return 0; } 我希望printf在从管道中读取Hello Dude之前打印Read fd并写入fd。 但事实并非如此……请看这里 。 当我在我们的大学计算机实验室尝试相同的程序时,我的输出是 Read fd:3 write fd:4 Hello Dude! 我们的一些朋友也观察到,更改printf语句以包含更多数量的\n字符会改变输出顺序…例如.. printf(“\nRead fd:%d\n write fd:%d\n”,pipefd[0],pipefd[1]); 意味着打印了读取fd然后消息Hello Dude! 然后打印写入fd。 这是什么行为? 注意:Out lab使用我们运行终端的linux服务器,但我不记得编译器版本。

便携式C如何在从管道读取时向前寻找

由于fseek()在管道上不起作用,有哪些方法可以模拟前进? 天真的方法是使用fread()并将读取的内容丢弃到内存缓冲区中。 为了避免巨大的缓冲区,你只需要使用缓冲区的一部分就可以反复使用相同的缓冲区。 但这是唯一的方法吗? 有没有另一种方法可以避免缓冲区和潜在的多重读取?

C:dup2,管道和fork没有按预期工作

我正在尝试做一个简单的fork – >执行另一个程序 – >对那个子进程说“你好” – >回读一些东西 – >打印收到的东西。 作为孩子使用的程序只是等待任何输入行,并向stdout打印一些东西,比如“你好!” 这是我的“主机”程序(不起作用): #include #include #include #define IN 0 #define OUT 1 #define CHILD 0 main () { pid_t pid; int pipefd[2]; FILE* output; char buf[256]; pipe(pipefd); pid = fork(); if (pid == CHILD) { printf(“child\n”); dup2(pipefd[IN], IN); dup2(pipefd[OUT], OUT); execl(“./test”, “test”, (char*) NULL); } else { […]

使用管道在两个子进程之间进行通信

我正在尝试编写产生两个子进程的代码,这些进程通过管道相互发送消息然后终止。 但是,当我运行以下代码时,只有child2打印出问候语,但是子1仍然会打印从子2获取的消息,而子节点1则不会。 有人知道我的方法有什么问题吗? #include #include #include #include #include int main(int argc, char** argv) { char chld_1_send[20] = “hello from child 1”; char chld_1_recv[20]; char chld_2_send[20] = “hi from child 2”; char chld_2_recv[20]; int chld_1_outgoing[2]; int chld_2_outgoing[2]; pipe(chld_1_outgoing); pipe(chld_2_outgoing); int chld_1_status, chld_2_status; pid_t chld_1, chld_2; chld_1 = fork(); if(chld_1 == 0) { chld_2 = fork(); } […]

C管道,fork,dup和exec()

我正在尝试通过管道传递字符串列表到子进程,它应该通过/bin/cat使用execl() 。 我之前有工作,除了管道没有关闭所以程序一直在等待。 不知道我做了什么,现在根本不工作。 有人可以看到我的代码并告诉我,在子进程中cat没有显示str数据我做错了吗? int main(int argc, char** argv) { char *str[] = {“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”}; int fds[TOTAL_CHILDREN]; int writeFds; int catPID; int status; FILE * write_to_child; //create pipe if (pipe(fds) == -1) { perror(“creating pipe: failed”); exit(EXIT_FAILURE); } pipe(fds); //create subprocess for cat child switch (catPID) { […]