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