What is the difference is the placement of wait() here - Unix

This is a discussion on What is the difference is the placement of wait() here - Unix ; Is there a difference in behavior if I place wait() after the following function call like below: #include #include #include #include #include #include #include #include void printme(char *user) { pid_t pid; pid = fork(); if (pid perror("fork failed"); exit(EXIT_FAILURE); } ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: What is the difference is the placement of wait() here

  1. What is the difference is the placement of wait() here

    Is there a difference in behavior if I place wait() after the
    following function call like below:

    #include
    #include

    #include
    #include
    #include
    #include
    #include
    #include

    void printme(char *user) {
    pid_t pid;

    pid = fork();

    if (pid < 0) {
    perror("fork failed");
    exit(EXIT_FAILURE);
    }

    if (pid == 0) {
    execlp("write", "write", user, (char *)0);
    perror("execlp tel failed");
    exit(EXIT_FAILURE);
    }

    }

    int main(int argc, char *argv[]) {
    char **users;
    users = argv + 1;
    int status;

    printme(*users);

    status = 0;
    wait(&status);
    if (status != 0) {
    fprintf(stderr, "user not logged on, bailing.\n");
    exit(EXIT_FAILURE);
    }

    exit(0);
    }


    Or inside the function call itself

    #include
    #include

    #include
    #include
    #include
    #include
    #include
    #include

    void printme(char *user) {
    pid_t pid;
    int status;

    pid = fork();

    if (pid < 0) {
    perror("fork failed");
    exit(EXIT_FAILURE);
    }

    if (pid == 0) {
    execlp("write", "write", user, (char *)0);
    perror("execlp tel failed");
    exit(EXIT_FAILURE);
    }

    status = 0;
    wait(&status);
    if (status != 0) {
    fprintf(stderr, "user not logged on, bailing.\n");
    exit(EXIT_FAILURE);
    }

    }

    int main(int argc, char *argv[]) {
    char **users;
    users = argv + 1;
    int status;

    printme(*users);

    exit(0);
    }


    Both appear to give the same results. Is there a difference here?

    [cdalten@localhost ~]$ ./forkit test
    write: test is not logged in
    user not logged on, bailing.
    [cdalten@localhost ~]$


  2. Re: What is the difference is the placement of wait() here

    K-mart Cashier wrote:
    > Is there a difference in behavior if I place wait() after the
    > following function call like below:


    I don't see what differences would you expect. The only problem I
    can see is your use of the value you get from wait(), you should
    have another look at the man page for wait(2) and check out the
    macros WIFEXITED() and WEXITSTATUS() since the status value you
    get from wait(2) is a combination of the process' exit status
    and some more information about the reasons the process exited.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

+ Reply to Thread