gpt4 book ai didi

c - setpgid 使交互式程序(vim,emacs ... ncurses?)无限循环

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:22 25 4
gpt4 key购买 nike

我正在尝试重新编写一个 shell,我想更改用我的 shell 启动的程序的 pgid!

我尝试在我的 fork() 之后执行不同的功能:

  • setpgid(0, 0) 使诸如 vim、emacs 之类的交互式程序...执行无限循环
  • setsid() 不值得终端控制。它使像 emacs 这样的程序失败了 Could not open file:/dev/tty
  • tcsetpgrp(0, getpid()) 让我的 shell 进入后台!
#include      <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int pid;

if ((pid = fork()) == 0)
{
setpgid(0, 0);
execlp("/usr/bin/emacs", "", "-nw", "test_file", (char*)0);
}
if (pid < 0)
exit (1);
if (pid > 0)
{
printf("Program finished.\n");
waitpid(-1, 0, 0);
}
return (0);
}

你知道如何解决这个问题吗?

最佳答案

所以,我找到了使像 emacs、vim 等需要控制终端的 emacs 程序工作的解决方案!

我不得不将对终端的控制添加到父级中,就像那样:

#include      <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int pid;

if ((pid = fork()) == 0)
{
setpgid(0, 0);
execlp("/usr/bin/emacs", "", "-nw", "test_file", (char*)0);
}
if (pid < 0)
exit (1);
if (pid > 0)
{
tcsetpgrp(0, pid)
printf("Program finished.\n");
waitpid(-1, 0, 0);
}
return (0);

如果您想了解更多信息:这是一个很好的链接 http://www.gnu.org/software/libc/manual/html_node/Launching-Jobs.html

关于c - setpgid 使交互式程序(vim,emacs ... ncurses?)无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29898716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com