Jörg Mohren
Jörg Mohren

Reputation: 87

Return -1 from fork()

I'm trying to port an application from OpenVMS to Linux. The application creates subprocesses in the following way:

if ((pid = fork()) == 0)
{
    // do subprocess
}
else if (pid < 0)
{
    printf("\ncreation of subprocess failed") ;
}
else
{
    wait(pid) ;
}

Now the compiler (gcc) gives me a warning that the case 'pid < 0' will never be reached. But why, and how can I then catch problems in fork()?

Thanks a lot in advance for your help

Jörg

Upvotes: 0

Views: 74

Answers (1)

You don't show the declaration of pid. I guess it was wrongly defined as some unsigned integral type. You should declare:

 pid_t pid;

before the line

 if ((pid = fork()) == 0)

and this is documented in fork(2) which also reminds you that you need to have

 #include <unistd.h>

near the start of your source file.

Upvotes: 3

Related Questions