我可以关闭信号处理程序中的文件描述符吗?

时间:2021-05-30 16:07:42

标签: c signals

目前在我的课程中,我们正在学习如何用 C 语言处理信号。我发现它非常有趣,所以我一直在尝试学习它。
问题是:我想制作一个程序,它在无限循环中从 stdin 读取并将输入的任何内容写入文件。当我想让它停止时,我会使用 Sub MakeAnyShapeVisible() Dim oSh as Shape ' Change 42 to the slide number, SomeName to the shape name ' you want to make visible: Set oSh = ActivePresentation.Slides(42).Shapes("SomeName") Call LogInShapeLoad(oSh) End Sub 并终止程序。
我想弄乱信号函数以使其更优雅地终止,但是它不起作用。谁能告诉我如何获得所需的行为?
这是我的代码:

CTRL + C

重点是在 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <signal.h> #include <sys/types.h> #include <sys/wait.h> void sigint_handler (int signum) { printf("\nTerminating program...\n"); close(fd); _exit(0); } int main (int argc, char *argv[]) { signal(SIGINT, sigint_handler); int fd; if ((fd = open("client", O_WRONLY | O_CREAT)) == -1) { perror("Error opening file"); return 1; } int len = 0; char buffer[1024]; while ((len = read(STDIN_FILENO, buffer, 1024)) > 0) { write(fd, buffer, len); } printf("Terminated\n"); close(fd); return 0; } 函数内部,它会关闭文件描述符并终止程序。
但是,我不能这样做。有没有更好的方法来获得想要的结果?
或者可以在不关闭文件描述符的情况下终止程序吗? 如果有人能帮助我更好地理解这一点,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TERMINATING "\nTerminating program...\n"

void sigint_handler (int signum) {
    // you can use write to avoid issues raised by @Shawn
    write(2, TERMIATING, sizeof(TERMINATING));
    _exit(0); /* close is not needed _exit guarantees the close */
}

int main (int argc, char *argv[]) 
{
    int fd;
    if ((fd = open("client", O_WRONLY | O_CREAT)) == -1) {
        perror("Error opening file");
        return 1;
    }

    // install signal handler ONLY AFTER successfully opening fd
    signal(SIGINT, sigint_handler);

    int len = 0;
    char buffer[1024];
    while ((len = read(STDIN_FILENO, buffer, 1024)) > 0) {
        write(fd, buffer, len);
    }
    // print messages like this to stderr 
    fprintf(stderr, "Terminated\n");
    close(fd); // superfluous because you are about exit
    return 0;
}

执行处理程序的更好方法是将 fd 保持在 main 本地,但使用 exit(0) 而不是 _exit,因为 exit(0) 会导致内核关闭所有打开的文件。

相关问题