{Linux Application Programming} process and thread Summary

Source: Internet
Author: User

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 attribute
Indicates 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:

  1. Void sig_chld (INT signo)
  2. {
  3. Pid_t PID;
  4. Int Stat;
  5. While (pid = waitpid (-1, & stat, wnohang)> 0)
  6. Printf ("child % d terminated \ n", pid );
  7. Return;
  8. }

Pthread_join (pthread_t tid, vodi ** rval_ptr); // obtain the thread exit Information


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.