如何让 tcsetpgrp() 在 C 中工作? [英] How do I get tcsetpgrp() to work in C?

查看:29
本文介绍了如何让 tcsetpgrp() 在 C 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让子进程(通过 fork())前台访问终端.

I'm trying to give a child process (via fork()) foreground access to the terminal.

在我fork()之后,我在子进程中运行以下代码:

After I fork(), I run the following code in the child process:

setpgid(0, 0);

还有:

setpgid(child, child);

在父进程中.

这为子进程提供了自己的进程组.对 setpgid() 的调用工作正常.

This gives the child its own process group. The call to setpgid() works correctly.

现在我想让孩子访问终端.

Now I want to give the child access to the terminal.

我在 setpgid() 调用后向孩子添加了以下内容:

I added the following to the child after the setpgid() call:

if (!tcsetpgrp(STDIN_FILENO, getpid())) {
    perror("tcsetpgrp failed");
}

之后,有一个execv()命令来生成/usr/bin/nano.

After that, there is an execv() command to spawn /usr/bin/nano.

然而,没有出现nano,什么也没有发生,终端看起来好像在等待用户输入.

However, instead of having nano come up, nothing happens, and the terminal looks as if it's expecting user input.

此外,在 tcsetpgrp() 调用之后似乎没有代码执行.

Further, no code seems to execute after the tcsetpgrp() call.

我在某处读到我需要向子进程发送 SIGCONT 信号以使其工作.如果该过程停止,我该怎么做?父母必须发送信号吗?

I read somewhere that I need to send a SIGCONT signal to the child process to get it to work. If the process is stopped, how can I do that? Does the parent have to send the signal?

如果这是解决方案,我该如何发送 SIGCONT 信号?

How do I go about sending the SIGCONT signal if that is the solution?

raise(SIGCONT);

另外,我不确定这是否有帮助,但是如果我运行我的程序,代码可以正常工作并产生 nano:

Also, I'm not sure if this helps, but the code works fine and spawns nano if I run my program with:

exec ./program

代替:

./program

有什么想法吗?非常感谢!

Any ideas? Thanks so much!

推荐答案

man 3 tcsetpgrp 声明:

man 3 tcsetpgrp states:

如果 tcsetpgrp() 在其会话中被后台进程组的成员调用,并且调用进程没有阻塞或忽略 SIGTTOU,则会向该后台进程组的所有成员发送 SIGTTOU 信号.

If tcsetpgrp() is called by a member of a background process group in its session, and the calling process is not blocking or ignoring SIGTTOU, a SIGTTOU signal is sent to all members of this background process group.

您需要在父进程中而不是在子进程中调用 tcsetpgrp().但是,如果您的父进程启动并移入后台,它将收到 SIGTTOU 并停止.

You need to call tcsetpgrp() in your parent process not in child. However if your parent process started and moved into background it will receive SIGTTOU and will be stopped.

这篇关于如何让 tcsetpgrp() 在 C 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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