sigaction and waitpid causes "interrupted system call" when callingfgets() - Linux

This is a discussion on sigaction and waitpid causes "interrupted system call" when callingfgets() - Linux ; Hi, I'm using ubuntu 7.10. I'm trying to run "ls" in the background while I continue to receive input. I also want to do something when "ls" finishes. The program loops and waits for input. Whenever it receives input, it ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: sigaction and waitpid causes "interrupted system call" when callingfgets()

  1. sigaction and waitpid causes "interrupted system call" when callingfgets()

    Hi,

    I'm using ubuntu 7.10.

    I'm trying to run "ls" in the background while I continue to receive
    input.
    I also want to do something when "ls" finishes.

    The program loops and waits for input.
    Whenever it receives input, it does the following.

    The parent calls fork().
    The child uses execvp to run "ls".
    The parent doesn't call waitpid()

    An error happens when I call fgets(). Any help is appreciated.

    Below is the code:
    --------------------------------------------------------------------------------------
    #include
    #include
    #include
    #include

    void signal_handler(int signo, siginfo_t *info, void *context)
    { //do nothing
    }

    void install_sigaction_handler()
    {
    struct sigaction sa;

    sa.sa_sigaction = signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO;
    sigaction(SIGCHLD, &sa, NULL);
    }

    void ls()
    {
    char *argv[2];
    argv[0] = "ls";
    argv[1] = NULL;

    int pid;
    pid = fork();

    if (pid == -1)
    abort();

    if (pid == 0)
    { execvp(argv[0], argv);
    abort();
    }
    else
    {
    //if the parent doesn't call waitpid(pid,NULL,0);, we're in trouble
    }
    }

    int main(void)
    {
    char input[1024];

    //install a signal action handler
    install_sigaction_handler();

    //loop to receive input
    while (!feof(stdin))
    {
    printf("enter something :");
    fflush(stdout);

    if (fgets(input, 1024, stdin) == NULL)
    {
    if (ferror(stdin))
    perror("my program error : ");
    break;
    }

    //fork a child and run "ls"
    //parent doesn't call waitpid
    ls();
    }

    return 0;
    }
    ------------------------------------------------------------

  2. Re: sigaction and waitpid causes "interrupted system call" when calling fgets()

    popcornnnn@gmail.com wrote in
    news:dcadb95c-8f6a-4dbe-ac2d-3cca375964b6@i7g2000prf.googlegroup
    s.com:

    > void install_sigaction_handler()
    > {
    > struct sigaction sa;
    >
    > sa.sa_sigaction = signal_handler;
    > sigemptyset(&sa.sa_mask);
    > sa.sa_flags = SA_SIGINFO;
    > sigaction(SIGCHLD, &sa, NULL);
    > }


    Look up the SA_RESTART flag in the sigaction man page. That might
    help.

    Also, it seems to me that you need to reap your forked children
    at some point, presumably in the SIGCHLD handler.

    MV

+ Reply to Thread