Testing and observation of Linux processes and threads

Source: Internet
Author: User
Tags usleep

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.