0

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

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
  • 7
    You need to show us the declaration of `pid`. The declaration of `pid` should be `pid_t pid`. I'm guessing that you declared it as an unsigned type. – user3386109 Apr 28 '15 at 08:51

1 Answers1

3

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you very much, that is the solution. Of course the compiler has problems with matching 'unsigned int' to 'smaller than zero'. On OpenVMS it was no problem, but the compiler there really accepted anything. – Jörg Mohren Apr 28 '15 at 13:54