I'm using the Ubuntu system, and I've written a piece of code to fork a subprocess. Then I use the Ps-el command to view the PID number of the main process and the child process. Surprised to find that the main process is no longer present, the ppid of the subprocess is a PID of the Init process, and this init pid is not 1.
After finding the cause, I learned that the main function of the master process was executed by fork, and the main process I wrote was finished, so the master session ended. If you prevent the main process from ending, you can add GetChar () at the end of the main function. Also, in Ubuntu, there will be two init processes, the PID of an init process is 1, and the PID of an init process is not 1. The specific reason is not known.
With curiosity, I wrote a C file, observing the relationship between the main thread and the child thread. The conclusion is that when the main thread is finished, the child threads are ended. And so the process will end. This c file is:
/* Test: If the main thread finishes running, if the child thread ends immediately, if the process is still in/#include <stdio.h> #include <pthread.h>void *child_thread (void*arg ); int main () {pthread_t tid; Pthread_create (&tid, NULL, &child_thread, NULL); Usleep (9000000); return 1;} void *child_thread (Void*arg) {while (1) {usleep (1000000);//hibernate 1s printf ("%s", "[child_thread][usleep]\r\ N\r\n "); } printf ("%s", "[Child_thread][dead]");}
Testing and observation of Linux processes and threads