#include <stdio.h> #include <stdlib.h> #include <unistd.h> main(){ int pid1, pid2, status; pid1 = fork(); if (pid1 == 0){ /* proces potomny */ sleep(10); exit(7); } /* proces macierzysty */ printf("Ребенок с идентификатором %d\n", pid1); kill(pid1, 9); pid2 = wait(&status); printf("Статус окончания процесса %d: %x\n", pid2, status); } 

in the end, if I give wait (7) - 700 (status), and if at least 32, then 20 00. I understand that this is due to the transfer to 16th. But if I assume the type of% d, then there will generally be 8192, it is not clear at all with this. And where do these two zeros constantly come from. One zero is probably because in fact there was no error, but the second is completely incomprehensible from where.

    2 answers 2

    The fact is that the result of the work wait is a bit field encoding various information about the process. Specific numbers of bits are not so important; for extracting information, according to the documentation , you need to use the WIFEXITED , WEXITSTATUS , WIFSIGNALED , WTERMSIG and the like.

    If you want to extract the completion code, check first if (WIFEXITED(status)) , with a positive check WEXITSTATUS(status) take WEXITSTATUS(status) .


    Apparently, on POSIX systems, from the exit code of the process (what is passed to exit ), everything is ignored, except for the low byte. So if you say exit(257) , it's like exit(1) .

    Update: according to Wikipedia , the exit argument can be fully waitid via waitid .

      You have already been told that it is wrong to extract the process termination code.

      Moreover, your program always forcibly terminates the child process and in status there will always be a signal code - 9 .

      Here is a little reworked your program.

       #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <signal.h> #include <sys/wait.h> int main (int ac, char *av[]) { int status; pid_t pid1, pid2; pid1 = fork(); if (pid1 == 0){ /* proces potomny */ sleep(1); exit(atoi(av[1] ? av[1] : "0")); } /* proces macierzysty */ printf("Ребенок с идентификатором %d\n", (int)pid1); if (av[1] && av[2]) if (kill(pid1, atoi(av[2]))) printf("kill: %m\n"); // some error else printf("send signal %d to child\n", atoi(av[2])); else puts("wait for child exit"); pid2 = wait(&status); int rc = -1; // undefined const char *msg = "exited"; if (WIFEXITED(status)) rc = WEXITSTATUS(status); else if (WIFSIGNALED(status)) { rc = WTERMSIG(status); msg = "terminated"; } else // WIFSTOPPED or WIFCONTINUED msg = "stopped | continued"; printf("Статус окончания процесса %d: 0x%x (%s)\n", (int)pid2, rc, msg); return puts("End") == EOF; } 

      which in the first argument takes the code with which the child is normally (calling exit() ), and in the second (if specified) the number of the signal that the parent sends to the child.

      You can translate (gcc or g ++ if you want) and see its output.

      PS
      the list of signals can be seen by calling the command kill -l in the shell
      and also, an example of the process completion code is given in man 2 wait