0

When using wait functions the SIGCHLD signal was consumed by wait when executing sample 1.1 as shown below.

sample 1.1

if (pid==0){
    printf("child\n");
    printf("%d\n",pid);
}
else{
    printf("Parent\n");
    printf("%d\n",pid);
    wait(NULL);
    sleep (10);                             
}

If I run sample 1.2 (below) where the wait function is omitted, the child becomes defunct. Why is the wait function handling the SIGCHLD signal?

Sample 1.2

if (pid==0){
    printf("child\n");
    printf("%d\n",pid);
}
else{
    printf("Parent\n");
    printf("%d\n",pid);
    sleep (10);                             
}

  • Would you add the output of your terminal to the question so that we may determine how your program went "defunct"? – DannyNiu Jan 29 at 13:18
1

While we can read your 2 samples, your question hardly make any sense.

According to the POSIX / Single Unix Specification version 4 2018 Edition, when a process had not explicitly indicated to the system that it wants to ignore the statuses of its child processes:

  • Status information (exit status code, etc.) shall be generated.

  • The [exiting] process shall be transformed into a zombie process. ...

  • The process' lifetime shall end once its parent obtains the process' status information ...

  • If one or more threads in the parent process of the calling process is blocked in a call to wait(), waitid(), or waitpid() awaiting termination of the process, one (or, if any are calling waitid() with WNOWAIT, possibly more) of these threads shall obtain the [exiting] process' status information ... and become unblocked.

  • A SIGCHLD shall be sent to the parent process.

Note the last point - the SIGCHLD is always generated (unless the parent specified to ignore it)!

Update

I tested both of your program fragments, wrapped in a stub that I assume to be plausible. Both run successfully. Here's the 2 full program source code and the terminal output:

Full Sample 1.1:

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    pid_t pid;

    pid = fork();

    if( pid == 0 ) {
        printf("child\n");
        printf("%d\n", pid);
    }
    else {
        printf("Parent\n");
        printf("%d\n", pid);
        wait(NULL);
        sleep(10);
    }
}

Full Sample 1.2:

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    pid_t pid;

    pid = fork();

    if( pid == 0 ) {
        printf("child\n");
        printf("%d\n", pid);
    }
    else {
        printf("Parent\n");
        printf("%d\n", pid);
        sleep(10);
    }
}
//64-bit Mac Mini./
$ make s1 s2 ; ./s1 ; ./s2
make: `s1' is up to date.
make: `s2' is up to date.
Parent
46431
child
0
Parent
46449
child
0

//64-bit Mac Mini./
$
| improve this answer | |
  • my question is about why the process get defunct when executing the above code without any wait functions? and why the process doesn't get defunct when executing the above sample code with wait function ? can you explain it more detail. – MP Creations Jan 29 at 12:44
  • In that case, we need the terminal output of the 2 sample programs, start from the invocation, ending at the next prompt. We need the text in-between to diagnose the cause. Merely saying "defunct" doesn't help, as this word has no definition in the POSIX standard, and it can mean a lot of things in a lot of situations. – DannyNiu Jan 29 at 13:04
  • 1
    @MPCreations any process becomes defunct (zombie) until some other process is wait()int on it (until it's reaping it). The SIGCHLD doesn't factor into it. On some systems like Linux you can set the SIGCHLD handler to "ignore" to prevent children from becoming zombies, but that's more of a historical hack. – Uncle Billy Jan 29 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.