检查子 PID 是否正在等待 Scanf [英] Check if a child PID is waiting for an Scanf

查看:69
本文介绍了检查子 PID 是否正在等待 Scanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,在某些时候会派生一个孩子.孩子唯一的代码是一个 scanf.父进程需要监控tis子进程的状态:1) 运行2) 在 scanf 中等待输入3) 终止

I have a code that in some point fork a child. The child only code is a scanf. The parent process needs to monitor the state of tis child process: 1) running 2) waiting for input in scanf 3) terminated

使用waitpid 和WNOHANG 检查是否终止很简单.问题是我不知道如何区分子进程是否在等待输入.

Check for termination or not is simples with waitpid and WNOHANG. The problem is that i dont know how to distinguish if the child process is waiting for input or not.

PS.:我的情况有一个限制,即我无法将信息从孩子发送给其父母.否则我可以在 scanf 之前和之后使用变量.

PS.: My case have a limitation that i cannot send information from child to its parent. Otherwise i could just use variables before and after the scanf.

PS.: 我在 linux 下

PS.: I'm under linux

如何检查子进程是否进入等待 scanf 输入(或一般的 IO)状态?

How can i check if a child process entered in a state of waiting for a scanf input ( or IO in general ) ?

推荐答案

进程和 IO 的概念通常是分离的.子进程和父进程之间没有信号交换,除了发送到父进程的 unix (kill) 信号,这些信号会传播到子进程.

The concepts of processes and IO are generally disjunct. There are no signals that are exchanged between child and parent process, except for unix (kill) signals sent to the parent pid, that propagate to the children.

Waitpid 只是等待子 pid 的终止,返回其状态码.

Waitpid just waits for termination of a child pid, returning its status code.

如果你想在父子进程之间交换数据,你需要在两个进程之间创建一个pipe(参见man -s 2 pipe),参见手册页一个例子.

If you want to exchange data between parent and child you need to create a pipe (see man -s 2 pipe) between both processes, see the manual page for an example.

如果要在child中使用scanf(从stdin输入),需要将pipefd[0]绑定到stdin文件描述符0(或 STDIN_FILENO).

If you want to use scanf (input from stdin) in the child, you need to bind the pipefd[0] to the stdin file descriptor 0 (or STDIN_FILENO).

现在您可以在父进程中使用 selectpoll 来检查子进程是否准备好读取父进程发送到 pipefd[1] 的数据.

Now you can use select or poll in the parent process to check if the child is ready to read data sent by the parent to pipefd[1].

如果您使用 printf 或其他一些 stdio.h 方法写入子级(例如通过 STDOUT_FILENO),则父级上的 IO 可能会阻塞,即使如果 selectpoll 告诉孩子已经准备好接收数据,如果孩子读取太慢或停止读取太早并且输出缓冲区已满(其中有一个我认为默认大小为 4096 字节).

If you use printf or some other stdio.h method to write to the child (via STDOUT_FILENO for example), the IO on the parent might block anyway, even if select or poll told that the child is ready to receive the data, if the child reads too slow or stops reading too early and the output buffer is full (which has a default size of 4096 bytes, I think).

unistd.h 写调用可能会返回一个值

A unistd.h write call might return a value

nw = write(pipefd[1], buffer, nbytes);

nw 如果孩子没有读取那么多 (nbytes) 字节的输入.

nw < nbytes if the child did not read that many (nbytes) bytes of input.

所以在进行异步通信时要小心绊倒的危险.当您了解异步方法时,请检查 CSP(通信顺序进程)方法作为一种不同且更稳定的通信方法,它使用同步通信.

So be carefull with the tripping hazards when doing asynchronous communication. Check the CSP (Communicating Sequential Processes) method as a different and more stable approach to communication, that uses synchronous communication, when you understood the asynchronous methods.

这篇关于检查子 PID 是否正在等待 Scanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆