I. Process
Sub-processes for creating, exiting, and sharing spaces, exiting processes, process owner, debugging, And zombie processes;
Signal: The operation is interrupted. For example, kill is equivalent to a message in windows.
Process Communication: Pipeline (famous Pipeline and unknown pipeline), System v ipc/posix ipc, shared memory, semaphore, Message Queue
Ii. Thread:
Create and exit
Thread Synchronization: mutex (also a lock) and read/write locks (one is a read lock and the other is a write lock), condition variables, thread attribute operations, and lock operations should not be blocked (when there is a DDoS attack .....).
The main purpose of evolving a thread from a process is to better support SMP and reduce (process/thread) Context switching overhead.
Http://bbs.chinaunix.net/thread-132343-1-1.html
For network servers with multi-process servers and multi-thread servers, some people say that the multi-line is good, but there are multiple processes in which each process has its own independent memory space. The death of a process will not affect other processes.
The thread execution overhead is small. The threads in the same process share the same virtual address space. Therefore, the thread generation speed is fast, and inter-thread communication speed and switching speed are fast, however, data access synchronization must be considered.
Different processes have their own virtual address spaces, so they consume more system resources. However, process data is independent and can achieve better logical abstraction and operational abstraction.
There is no absolute good or bad, and there is only one more suitable question.Let's take a look at how to make a proper decision in practical applications.
In concurrency and multi-process (multithreading ),Resource Recovery (zombie process):
Int pthread_detach (pthread_t thread) http://biancheng.dnbcw.info/linux/369315.html
In some cases, we do not care much about the exit status of the thread. We just want the thread to terminate and then clear the resources it uses. In this case, we use this function to detach the thread. In this way, there will be no "zombie" threads. Separating a running thread does not affect the thread proxy. It only notifies the system that all its resources can be recycled when the thread ends.
RecommendedSet thread attributesTo Recycle resources.
Pthread_create_detached attributeIndicates that the thread is in the separated state. The thread can be reused once it exits.IDAnd other resources,
Add a parent processSigchld SignalWhen the child process exits, the parent process will receive this signalThe parent process calls wait or waitpid to process the exited child process information.Make it a zombie Process
Void (* signal (int signum, void (* Handler) (INT); // set the signal to signal (sigchld, Handler ); /* sig_err error returned */void handler (INT signo );
Pid_t wait (int * statloc); // you can obtain the end status of multiple sub-processes created by yourself,Will temporarily stop the execution of the current processUntil a signal arrives or the sub-process ends. If the server does not call wait to obtain the end state of the sub-process, a large number of zombie processes will occur.
Pid_t wait_pid (pid_t PID, int * statloc, int options );
// This is better for multiple processes.
Example:
- Void sig_chld (INT signo)
- {
- Pid_t PID;
- Int Stat;
- While (pid = waitpid (-1, & stat, wnohang)> 0)
- Printf ("child % d terminated \ n", pid );
- Return;
- }
Pthread_join (pthread_t tid, vodi ** rval_ptr); // obtain the thread exit Information