Tag: 管道

作为进程子进程,如何知道哪个文件描述符是父进程

我正在尝试编写一个程序,该程序分叉并等待他的孩子完成,然后孩子在输入上做一些工作然后以与父母一样的方式分叉等等。 现在,我知道分支复制到子文件描述符数组,我应该关闭与父文件关联的那些,但我无法弄清楚哪些是父文件。 我需要给孩子这是父母的pid吗? 我一直试图在一小时的大部分时间里绕过它,我想我有一种心灵障碍,因为我无法得出结论。 TL; DR:作为子进程,我如何知道哪些文件描述符属于我的父进程?

重定向CMD.exe输出并通过管道输入c

我试图打印输出和输入cmd命令收到stdout就像普通的cmd.exe一样。 我可以使用函数_popen但是当我启动像Python或Powershell这样的程序时,它不起作用。 所以我需要输出子进程并能够向子进程发送命令。 这里有一个类似的问题 所以我修改了这个链接的代码,如下所示: void WriteToPipe(char* command){ DWORD dwRead, dwWritten; BOOL bSuccess = FALSE; bSuccess = WriteFile(g_hChildStd_IN_Wr, command, strlen(command), &dwWritten, NULL); // Close the pipe handle so the child process stops reading. if (!CloseHandle(g_hChildStd_IN_Wr)) ErrorExit(TEXT(“StdInWr CloseHandle”)); } void ReadFromPipe(void){ DWORD dwRead, dwWritten; CHAR chBuf[BUFSIZE]; BOOL bSuccess = FALSE; HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE); bSuccess = […]

坏文件描述符

有没有人看到这个问题,它不工作说坏文件描述不知道为什么? pipe(pipefd[0]); if ((opid = fork()) == 0) { dup2(pipefd[0][1],1);/*send to output*/ close(pipefd[0][0]); close(pipefd[0][1]); execlp(“ls”,”ls”,”-al”,NULL); } if((cpid = fork())==0){ dup2(pipefd[0][1],0);/*read from input*/ close(pipefd[0][0]); close(pipefd[1][1]); execlp(“grep”,”grep”,”.bak”,NULL); } close(pipefd[0][0]); close(pipefd[0][1]);

fork,pipe exec和dub2

这段代码应该打印“输出来自’ls -l’:”并附加’ls -l’的结果,但它没有…有没有人有这个问题的线索? #include #include #include #include void readStringFromFile (int file, char * readbuffer) { int nbytes = read(file, readbuffer, sizeof(readbuffer)); readbuffer[nbytes] = 0; } int main(int argc, char const *argv[]) { int fd[2]; pipe(fd); if (fork()==0)//child process { close(fd[0]); dup2(fd[1],1); int retValue = execl(“/bin/ls”,”ls”,”-l”, NULL); printf(“Exec failed: retValue = %d\n”,retValue); } else { int […]

在这种情况下使用perror?

我写了一个小程序(带有来自SO的代码),它做了printenv | sort | less printenv | sort | less 现在我想用perror添加error handling并检查返回值。 我之前从未这样做过,但我认为它类似于exception处理。 我需要检查execvp,fork,pipe和dup2的错误。 我有这个代码 #include #include #include #include #include #include struct command { const char **argv; }; /* Helper function that spawns processes */ int spawn_proc (int in, int out, struct command *cmd) { pid_t pid; if ((pid = fork ()) == 0) { […]

C,让父母和子进程使用信号工作

我试图让我的子进程生成1到9之间的随机数,然后将其发送到我的父进程,每次按下control + Z时它都会显示它。 我也使用dup2()将printf和scanf中的流更改为每个进程的管道读写端。 代码如下所示,按控制C启动主程序。 现在,每当从子节点发送随机数并在父节点上显示时,它等待控制Z被按下。 我面临的问题是,似乎孩子只在再次按下控制Z时运行一次 ,它只运行父代码。 管道也只显示相同的数字,并且永远不会更改,因此不会更新。 我还注意到,如果我在dup函数printf(“a \ n”)之前删除了printf行,它会输出随机数….但是,运行一次的子进程的问题仍然存在。 如果有人可以帮助我,我将不胜感激。 干杯。 #include #include #include #include #include #include #include int selection = 0; int secondSel = 0; int fd[2]; int pipe1; pid_t fork1; void handleSignal(int sig) { if (sig == SIGINT) { selection = 1; secondSel = 1; } if (sig == SIGTSTP) […]

民意调查如何处理封闭管道

我想知道: pipe关闭时,将为管道的文件描述符设置什么状态poll ? 我尝试下面的代码,在子进程关闭所有文件描述符后,轮询只是认为所有文件描述符都可以读取! 是对的吗? 或者我在这段代码中犯了一些错误? 我使用SUSE和gcc。 #include #include #include “../../../myInclude/apue.h”// this is ok #include int main(int argc, char **argv) { int fd1[2]; int fd2[2]; int fd3[2]; pid_t pid; if(pipe(fd1)<0 ||pipe(fd2)<0 ||pipe(fd3) <0) err_sys("pipe error");//this is a error deal function .it will exit the program and print error message. if((pid = fork()) 0) { printf(“now […]

从信号发送管道读取

我想要: 家长流程: 将数据写入管道 向子进程发送信号 儿童过程: 捕获信号后读取数据。 这是我的试用版: #include #include #include #include #include #include #include #include int fd[2]; //Handler – Exiting process, OR reading from pipe void sigHandler(int signumber){ if(signumber == SIGUSR1) { printf(“SIGUSR1 catched.\n”); //So lets read from pipe int read_data = -1; close(fd[1]); read(fd[0], &read_data, sizeof(read_data)); //This printf never gets called printf(“Received data: %s”, […]

在从该FIFO读取所有数据后,是否应从FIFO块读取?

我正在学习Linux中的管道编程,并且无法理解管道/ FIFO管理。 我写了一个小程序打开我创建的FIFO(我在执行程序之前在终端中执行了mkfifo newfifo )。 然后我反复读取并转储我的字符缓冲区。 我正在使用来自另一个终端的cmd线的echo “message” > newfifo来填充FIFO。 问题是,当我写入FIFO时,我可以在缓冲区中读取该数据,但读取不再阻塞。 我的理解是,在我从FIFO读取数据后,FIFO应为空,读取应该阻塞。 我是在考虑这个错误,还是我错误地管理了FIFO? 代码如下: #include #include #include #include #include #define NEWPIPE “./newfifo” void main() { int great_success = 0; int fd; char buffer[20]; fd = open(NEWPIPE, O_RDONLY); while (1) { great_success = read(fd, buffer, 20); if (great_success < 0) { printf("pipe failed\n"); } else { […]

我可以在C中锁定和解锁fifo(命名管道)吗?

我有两个进度, p1和p2 ,以及一个名为pipe var的ipc,介于两个进度之间。 我想在p1写入时锁定var的rw为p2 ,在p1写完时解锁var 。 PS: 我使用select进行非阻塞,所以当p1把任何东西放到var时, p2会变得可读。可以在p1写完后让var变得可读吗?