0

This is my code (it's just an example)

int a = system("pwd");
printf("pwd with return: %d\n", a);
cout << "errno is " << errno << "\n";

signal(SIGCHLD, SIG_IGN);

a = system("pwd");
printf("pwd with return: %d\n", a);
cout << "errno is " << errno << "\n";

I just run "pwd" command using system call before and after "SIGCHLD" and I get two different result:

<actual path>
pwd with return: 0
errno is 0
<actual path>
pwd with return: -1
errno is 10

In both case the command is executed properly.

Why signal(SIGCHLD, SIG_IGN); creates this issue? Is there a way to avoid it?

Walter
  • 67
  • 12
  • Be careful when using `errno`. Most calls only modifies `errno` if they fail, leaving it in an *indeterminate* state if they do not fail. Only check `errno` when the function is documented to set it (like when it fails). – Some programmer dude Jul 20 '21 at 10:35
  • You should capture the value of `errno` as early as possible, because other operations — such as outputting it to `cout` — may in the interim change its state. You may want to `errno = 0;` before calling a function if you are not checking the functions return code for failure before looking at `errno`. – Eljay Jul 20 '21 at 10:56

0 Answers0