Original address: http://www.cnblogs.com/work115/p/5620272.html
Want to know more about the process: https://www.zhihu.com/question/20511233
First, the concept
1. Process
Process is a computer program on a data set on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure. In the early process design-oriented computer architecture, the process is the basic execution entity of the program, and in the contemporary thread-oriented computer architecture, the process is the container of the thread. A program is a description of instructions, data, and its organization, and the process is the entity of the program.
A process is a run-time activity of a program with independent functionality about a data collection. It can apply and own system resources, is a dynamic concept, is an active entity. It is not just the code of the program, it also includes the current activity, which is represented by the value of the program counter and the contents of the processing register.
The concept of the process is mainly two points: first, the process is an entity. Each process has its own address space, which, in general, includes the text area, the data region, and the stack area. The text area stores the code executed by the processor, the data region stores variables and the dynamically allocated memory used during process execution, and the stack area stores the instructions and local variables for the active procedure call. Second, the process is an "executing procedure". The program is an inanimate entity, and only the processor gives the program life (the operating system executes it) to become an active entity, which we call a process.
2. Threads
A thread, sometimes called a lightweight process (lightweight PROCESS,LWP), is the smallest unit of program execution flow. A standard thread consists of a thread ID, a current instruction pointer (PC), a collection of registers, and a stack. In addition, the thread is an entity in the process, is the basic unit that is dispatched and dispatched by the system independently, the thread does not own the system resources, only has a bit of resources that are essential in the operation, but it can share all the resources owned by the process with other threads belonging to one process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process. Because of the mutual constraints between threads, the thread is running in a discontinuous. Threads also have three basic states of readiness, blocking, and running. A ready state is a thread that has all of the conditions running, is logically capable of running, is waiting on a processing machine, and a running state is a thread-owning processor that is running; a blocking state is a thread waiting for an event, such as a semaphore, to be logically unenforceable. Each program has at least one thread, and if the program has only one thread, it is the program itself. A thread is a single sequential control flow in a program. A relatively independent, scheduled execution unit within a process is the unit of dispatch for the system to dispatch and dispatch CPUs independently of the operating program. Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading. A thread is a single sequential control flow in a program. A relatively independent, scheduled execution unit within a process is the unit of dispatch for the system to dispatch and dispatch CPUs independently of the operating program. Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading. 3, the process of a program can contain multiple co-processes, can be compared with a process to contain multiple threads, so let's compare the co-process and threads. We know that multiple threads are relatively independent, have their own context, that the switchover is controlled by the system, and that the process is relatively independent, has its own context, but that its switchover is controlled by itself, and that the current coprocessor is controlled by the current process. http://www.zhihu.com/question/20511233 4, Daemon Daemon () program is a running service-side program, also known as the daemon process. Usually run in the background of the system, no control terminal, no interaction with the foreground, daemon program is generally used as a system service. Daemon is a long-running process that normally runs after the system starts and ends when the system shuts down. Generally speaking, the daemon program runs in the background because it does not control the terminal and cannot interact with the foreground user. Daemon programs are generally used as service programs, waiting for the client program to communicate with it. We also call the running Daemon program The daemon.
A daemon thread is a thread that provides a generic service in the background while the program is running, such as a garbage collection thread that is a competent guardian, and that thread is not an integral part of the program. Therefore, when all non-daemon threads end, the program terminates and kills all the daemon threads in the process. Conversely, the program will not terminate as long as any non-daemon threads are still running.
The only difference between the user thread and the daemon is that the virtual machine leaves: If the user thread is all out of operation, only the daemon thread is present, and the virtual machine exits. Because there is no guardian, the daemon will have no work to do, there is no need to continue to run the program.
5. Multi-process and multi-threading
A process is the smallest unit of resource allocation, and a thread is the smallest unit of CPU scheduling. The difference between a thread and a process is that the child process and the parent process have different code and data spaces, while multiple threads share the data space, and each thread has its own execution stack and program counter for its execution context. Multithreading is mainly to save CPU time, play use, depending on the situation. The running of a thread requires the use of the computer's memory resources and CPU.
Multi-process: a process is an execution activity on a computer by a program. When you run a program, you start a process. Obviously, the program is Dead (static), and the process is alive (dynamic). Processes can be divided into system processes and user processes. The process used to complete the various functions of the operating system is the system process, which is the operating system itself; All user-initiated processes are user processes. A process is the unit in which the operating system allocates resources. The process is also refined to a thread, which means that there are several smaller units under a process that can run independently. At the same time, if two or more processes are allowed to run in the same computer system, this is multitasking. Modern operating systems are almost always multitasking operating systems that can manage multiple processes at the same time. The benefits of multitasking are obvious, such as you can listen to the Internet while listening to MP3, and even print the downloaded documents without interfering with each other. So here is the question of parallelism, as the saying goes, one cannot use two, this is the same for computers, in principle a CPU can only be assigned to a process in order to run the process. We usually use a computer with only one CPU, that is, only one heart, to make it multitasking, running multiple processes at the same time, you must use concurrency technology. The implementation of concurrency technology is quite complex, the most easy to understand is the "time slice rotation process scheduling algorithm", its ideas are briefly described as follows: Under the management of the operating system, all running processes take turns using the CPU, each process allows the CPU time is very short (such as 10 milliseconds), so that users do not feel The CPU is in turn serving multiple processes as if all the processes were running uninterrupted. But there is actually only one process that occupies the CPU at any one time. If a computer has multiple CPUs, the situation is different, if the number of processes is less than the number of CPUs, then different processes can be assigned to different CPUs to run, so that multiple processes are really running concurrently, which is parallel. However, if the number of processes is greater than the number of CPUs, concurrency technology is still required. CPU allocation is done in threads, a process may be composed of multiple threads, and the situation is more complicated, but in short, the following relationships are:
Number of bus <= CPUs: running in parallel
Number of bus threads > number of CPUs: concurrently running
The efficiency of parallel operation is obviously higher than that of concurrency, so multitasking is more efficient in multi-CPU computers. However, if you run only one process (thread) in a multi-CPU computer, you will not be able to take advantage of multiple CPUs. This involves multitasking operating systems, where the rationale for multitasking operating systems (such as Windows) is that the operating system allocates the CPU's time slices to multiple threads, each of which is done within the time slice specified by the operating system (note that multiple threads here are part of different processes). The operating system constantly switches from one thread execution to another, and so on, on the macro level, it seems as if multiple threads are executing together. Because these threads belong to different processes, it seems to us as if multiple processes are executing simultaneously, thus enabling multitasking
Multithreading: In computer programming, a basic concept is to control multiple tasks at the same time. Many programming problems require that the program be able to stop the work at hand, and instead deal with some other problems before returning to the main process. This can be achieved in a variety of ways. At the very beginning, programmers who mastered the low-level language of the machine wrote some "interrupt service routines", and the pauses of the main process were implemented by hardware-level interrupts. Although this is a useful method, the programming is difficult to transplant, resulting in another category of costly problems. Interruptions are necessary for tasks that are very real-time. But for many other problems, it only requires dividing the problem into a separate running program fragment, so that the whole program can respond to the user's request more quickly.
At the very beginning, a thread is just a tool to allocate processing time for a single processor. But if the operating system itself supports multiple processors, then each thread can be assigned to a different processor, really into the "parallel operation" state. One of the most valuable features of multithreaded operations from a programming language perspective is that programmers don't have to worry about how many processors are being used. The program is logically divided into several threads, and if the machine itself has multiple processors installed, the program will run faster without any special tuning. According to the previous discussion, you may feel that threading is very simple. But you have to pay attention to one problem: sharing resources! If multiple threads are running at the same time, and they try to access the same resources, they will encounter a problem. For example, two threads cannot send information to a printer at the same time. To solve this problem, for those resources that can be shared (such as printers), they must enter a lock during use. So a thread can lock the resource and, after completing its task, unlock (release) the lock so that other threads can then use the same resource.
Multithreading is to accomplish many tasks synchronously, not to improve the efficiency of the operation, but to improve the efficiency of the use of resources to improve the efficiency of the system. Threads are implemented at the same time when multiple tasks need to be completed.
An application that employs multithreaded technology can make better use of system resources. Its main advantage is to make full use of the idle time slice of the CPU, can respond to the user's request with as little time as possible, make the overall running efficiency of the process be improved greatly, and enhance the flexibility of the application. More importantly, because all threads of the same process share the same memory, there is no need for a special data transfer mechanism, no need to establish a shared storage or shared files, so that the coordination between different tasks and operations, data interaction, resource allocation and other issues more easily resolved.
interprocess communication (ipc,inter-process communication), which is a technique or method that transmits data or signals between at least two processes or threads. A process is the smallest unit of resources allocated by a computer system. Each process has its own part of a separate system resource that is isolated from each other. Inter-process communication is achieved in order to enable different processes to access resources and coordinate work with each other. These processes can run on the same computer or on different computers that are connected to the network.
Interprocess communication technologies include messaging, synchronization, shared memory, and remote procedure calls. IPC is a standard UNIX communication mechanism.
Reasons to use IPC:
Information sharing
Accelerate
Modular;
Convenient And
Private right separation.
The main IPC method
Method provider (operating system or other environment)
File most operating systems
Signal most operating systems
Sockets most operating systems
Message Queuing (En:message queue) most operating systems
Pipeline (En:pipe) for all POSIX systems, Windows.
Named pipe (en:named pipe) for all POSIX systems, Windows.
Semaphore (En:semaphore) for all POSIX systems, Windows.
Shared memory for all POSIX systems, Windows.
Message passing (En:message passing) is used for MPI specifications, Java RMI, CORBA, MSMQ, Mailslot, and others.
Understanding of processes, threads, and co-routines