开始一个进程,不看输入或输出。

huangapple go评论71阅读模式
英文:

C start a process without seeing the input or output

问题

我正在尝试编写一个初始化系统,我想要在不涉及输入输出的情况下启动一个进程。换句话说,我不想看到输出,也不想输入。我目前使用了suckless的sinit中的以下函数 -

void spawn(char *const argv[]) {
    switch (fork()) {
        case 0:
            sigprocmask(SIG_UNBLOCK, &set, NULL);
            setsid();
            execvp(argv[0], argv);
            perror("execvp");
            _exit(1);
            break;
        case -1:
            perror("fork");
            break;
        default:
            break;
    }
}

但是,如果我启动一个进程(以top作为测试),它不会在后台运行。我该如何做到这一点?

英文:

I'm trying to write an init system, and I want to spawn a process without the IO. meaning, I don't want to see output or for it to take input. I currently use the function from suckless's sinit -

void spawn(char *const argv[]) {
	switch (fork()) {
		case 0:
			sigprocmask(SIG_UNBLOCK, &set, NULL);
			setsid();
			execvp(argv[0], argv);
            perror("execvp");
			_exit(1);
            break;
		case -1:
			perror("fork");
            break;
        default:
            break;
	}
}

but if I start a process (used top as a test), it doesn't run "in the background". how can I do that?

答案1

得分: 2

以下是翻译好的部分:

所有进程都期望从它们的父进程继承文件描述符 0、1 和 2,分别作为标准输入、标准输出和标准错误。

传统的方法涉及将它们重定向到 /dev/null。所有输出都会消失,而任何试图从标准输入读取的尝试都会立即显示文件结束指示。

将它们重定向到 /dev/null 包括关闭它们,并在它们的位置打开 /dev/null

void spawn(char *const argv[]) {
    switch (fork()) {
        case 0:
            sigprocmask(SIG_UNBLOCK, &set, NULL);
            setsid();
            close(0);
            close(1);
            close(2);
            open("/dev/null", O_RDONLY);
            open("/dev/null", O_WRONLY);
            open("/dev/null", O_wRONLY);
            execvp(argv[0], argv);

鲁棒性建议要仔细检查每个 open() 是否成功,如果打开 /dev/null 失败,您将有更多问题需要担心。

英文:

All processes expect to inherit file descriptors 0, 1, and 2 from their parent process, as standard input, output and error, respectively.

The traditional approach involves redirecting them to /dev/null. All output disappears, and any attempts to read from standard input result in an immediately end-of-file indication.

And redirecting them to /dev/null consists of closing them, and opening /dev/null in their place.

void spawn(char *const argv[]) {
    switch (fork()) {
        case 0:
            sigprocmask(SIG_UNBLOCK, &set, NULL);
            setsid();
            close(0);
            close(1);
            close(2);
            open("/dev/null", O_RDONLY);
            open("/dev/null", O_WRONLY);
            open("/dev/null", O_wRONLY);
            execvp(argv[0], argv);

Robustness suggests meticulously checking if each open() succeeded, or not. But, if opening /dev/null fails you'll have more problems to worry about.

huangapple
  • 本文由 发表于 2023年1月9日 02:36:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定