Understanding of Windows and Linux processes and threads

Source: Internet
Author: User

For Windows, the concepts of processes and threads are clearly defined, and the concept of a process corresponds to a running instance of a program (instance), while a thread is the smallest unit of execution of a program's code. This means that Windows is defined for processes and threads that are consistent with the process and threading concepts taught in the classic OS course.

Provides Api,createthread () for creating a new thread, passing the entry address of the thread function and calling the parameter to the new thread, and then the new thread executes.

Under Windows, a typical thread has its own stack, register (including a program counter PC, which points to the location of the next instruction to be executed in memory), while code snippets, data segments, and open files are shared by multiple threads within the same process. Therefore, the different threads of the same process can easily communicate through global variables (data segments), which can be read and written, which is convenient and is also criticized in terms of security, because it requires the programmer to be aware that the data is not thread independent at all times.

For Linux, there is no clear process, threading concept. First Linux only processes and no threads, but its process can behave like a thread under Windows. Linux uses the fork () and the EXEC function family to manipulate multithreading. The fork () function can be called at any stage of the process execution, and once called, the current process is forked into two processes-the parent and child processes, both with the same Code snippet and the same temporary data segment (although temporarily the same, but from the moment of bifurcation is the logical two data segments, it is said to be logical, is because this is a "copy-on-write" mechanism, that is, unless there is a last-ditch process to write data segments, the system does not replicate the data segment, so that the burden is minimal), the difference is the fork () function return value, for the child process returned to 0, The child process ID is returned for the parent process, so it can be passed the IF (fork () ==0) ... else ... To allow the parent-child process to execute different pieces of code to achieve "forking."

The function of the EXEC function family is to start a new process for another program and then completely replace itself with that process (the code snippet is replaced, the data segment and stack are discarded, and the original process ID is preserved). Thus, if you start another process with exec in the sub-process code snippet after fork (), it is equivalent to CreateThread () under Windows, so that the process under Linux can behave like a thread under Windows.

However, processes under Linux cannot communicate as easily as Windows threads, because they do not share data segments, address spaces, and so on. The communication between them is carried out through the so-called IPC (interprocess communication). Specific pipelines (nameless pipes are used for parent-child interprocess communication, named pipes can be used for any two interprocess communication), shared memory (a process requests a piece of memory that can be shared by the system, and the other process obtains the memory through an identifier and connects it to its own address space. The effect resembles a shared data segment between multiple threads under Windows), semaphores, sockets.

Understanding of Windows and Linux processes and threads

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.