gpt4 book ai didi

c - 如何使用 tcsetpgrp 函数

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:52 25 4
gpt4 key购买 nike

这是一个使用 tcsetpgrp 函数的简单示例:

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

static void judge(void){
pid_t pid;
pid = tcgetpgrp(STDIN_FILENO);
if(pid == -1){
perror("tcgetpgrp");
return;
}else if(pid == getpgrp()){
printf("foreground\n");
}else{
printf("background\n");
}
}

int main(void){
signal(SIGTTOU,SIG_IGN);
judge();
int result;
result = tcsetpgrp(STDIN_FILENO,getpgrp());
if(result == -1){
perror("tcsetpgrp");
return -1;
}
judge();
return 0;
}

我会在后台运行它,如我所料,输出如下:

tod​​d911@virtual-machine:./a.out &

[1] 15894

tod​​d911@virtual-machine:~$ 背景

前景

但实际上,它的行为是这样的:

tod​​d911@virtual-machine:./a.out &

[1] 15894

tod​​d911@virtual-machine:~$ 背景

前景

退出

最后,终端自动退出,有人知道原因吗?

最佳答案

如果控制终端的进程组退出,则 shell 或终端可能会确定 session 已结束并将其终止。我用 ksh93 和 bash 在 gnome 终端上尝试了你的程序,但它没有退出。

关于c - 如何使用 tcsetpgrp 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20276985/

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