The order problem of Daemon, fork and Creation pthread of C program under Linux

Source: Internet
Author: User

Recently discovered that there was a problem between daemon and thread creation when writing a Linux C service program. After the discovery program is daemon, the thread is all hung up.

After checking some documents, I finally got to know why.

The operation of the daemon function is actually very simple,

1.fork a process, 2. Processing 3 standard input and output streams

When you're done, the main process exits and the child process is actually running. The child process is hung on the init process, which is the PID 1 process.

The problem occurs at this daemon point in time.

If after pthread initializes the thread and then daemon, the child process does not have the previous thread, but the previous thread ends up with the main process.

So the correct order is:

Daemon-----> Pthread Create

The same problem also occurs on the fork, the correct order is

Fork-------> Child process pthread Create, parent process exits

Related Article

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.