Thread: Keep getting -1 from waitpid

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    33

    Keep getting -1 from waitpid

    I'm trying to work with foreground/background processes, but seem to keep getting -1 from waitpid and I'm not sure why

    Code:
    void handleSignal(int signal) {
        printf("terminated by signal %d", signal);
        fflush(stdout);
        exit(signal);
    }
    
    struct sigaction sigAct;
        sigAct.sa_handler = handleSignal;
        sigAct.sa_flags = 0;
        sigfillset(&(sigAct.sa_mask));
    
    pid_t spawnpid = -5;
            int childStatus;
            spawnpid = fork();
            
      while(1) {
    if (spawnpid == -1) {
    //            perror("ERROR: fork()\n");
                printf("failed to start child process\n");
                status = 1;
                fflush(stdout);
                continue;
            } else if (spawnpid == 0) {
                /*
                 * inside the child process
                 */
                printf("inside child process\n");
            
                sigaction(SIGINT, &sigAct, NULL);
                
                // execute the command
                execvp(command->cmdArgs[0], command->cmdArgs);
    
                // if reach here then execvp failed
                printf("command not found");
                fflush(stdout);
                status = 1;
            } else if (spawnpid > 1) {
                /*
                 * inside the parent process
                 */
                printf("inside parent process\n");
                sigaction(SIGINT, &sigAct, NULL);
                
                if (command->background) {
                    printf("background pid is %d", spawnpid);
                    fflush(stdout);
                } else {
                    // wait for child process to finish
                    pid_t childpid = waitpid(spawnpid, &childStatus, 0);
                    if (childpid == -1) {
                        perror("ERROR: waitpid()\n");
                        status = 1;
                        continue;
                    }
                    ...
                    
                    fflush(stdout);
                }
            }
        }
    }
    The output I keep getting is something like this:
    Code:
    : ls
    inside parent process
    inside child process
    csXXX_proj3
    ERROR: waitpid()
    : Interrupted system call
    :

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you should put your while(1) loop around just the waitpid call, instead of round a whole mess of code.

    Perhaps further, you should escape from that while(1) loop when waitpid returns a success result.

    Interrupted system call is to be expected from time to time, and you need to handle this by retrying the call (if appropriate).

    Other than that, I got an endless stream of
    inside parent process
    ERROR: waitpid()
    : No child processes
    with your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    33
    The reason I have that while(1) loop is because I want to keep asking the user for input (sort of like a shell). I'm confused about the interrupted system call. I thought that waitpid waits for the child process to finish, and the sigaction was suppose to allow the parent process to do other things while the child process does its own thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not sure why waitpid() isn't working
    By frank1 in forum C Programming
    Replies: 3
    Last Post: 09-22-2016, 11:49 AM
  2. waitpid() in loop, not ending correctly
    By Gatsu in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 02:05 AM
  3. waitpid behaving strange...
    By mattholm in forum C Programming
    Replies: 6
    Last Post: 11-28-2011, 01:12 PM
  4. waitpid() non-blocking fork
    By codevyper in forum C Programming
    Replies: 5
    Last Post: 06-21-2011, 04:05 PM
  5. help with waitpid()
    By kotoko in forum C Programming
    Replies: 1
    Last Post: 09-28-2008, 01:23 PM

Tags for this Thread