Processes and threads (to be continued)

Source: Internet
Author: User

First, the definition

Textbook answer, a process is the smallest unit of resource allocation, and a thread is the smallest unit of CPU scheduling.

The process has its own independent address space (virtual, 4G size), and one process crashes without affecting other processes. After a thread in the same process crashes, the entire process crashes. Because threads have their own stacks and local variables, there is no separate address space between the threads.

Multi-process is more robust than multi-threaded programs, but because of the cost of switching more resources and time, so there is a high concurrency requirements, the general use of multithreading.

To distinguish between process and thread, let's take a look at what is contained in the independent address space of the process.

The virtual independent address space for a process mainly includes the following: Text Segment, Data Segment (), BBS (uninitialized), stack, heap, kernel space

Textsegment represents the code snippet of the program;

Datasegment ( Global initialization data area/static data area, or abbreviated data segment ) represents a global variable and a static local variable that has been initialized with an initial value of not 0;
BBS (uninitialized data area-specifically global variables and static local variables ) represents a global variable and a static local variable with an uninitialized or initial value of 0, and the operating system is responsible for loading and allocating memory space for these segments, which are segmented at compile time. Initializes a 0 or a null pointer before the program executes.

stack is used to store local variables, function parameters, function return values

heap is used to store the memory of a program's dynamic request and needs to be released manually by the programmer. Because the memory in the free space is not contiguous, the heap is stored in the operating system using a linked list.

kernel space (kernel spaces) is part of the operating system and resides in memory.

1 intA =0; global initialization Zone2 Char*p1; global uninitialized zone3 4 voidMain ()5 {6     intb; Stack7     CharS[] ="ABC"; Stacks S and"ABC"It's all on the stack.8     Char*p2; stack9     Char*P3 ="123456";123456in the constant area, p3 on the stack. Ten     Static intc =0; Global (static) initialization zone OneP1 = (Char*)malloc(Ten); Heap AP2 = (Char*)malloc( -); Heap -}

Now you know that each process space holds those things. We went on to see the multi-process.

Process context

Process context: The value in all registers of the CPU, the state of the process, and the contents on the stack when a process executes. When the kernel is switching to a process, it needs to save all the state of the current process, that is, the context of the current process, so that when the process is executed again, it can resume the state of the switchover and continue execution.

A process will have only one parent process, so any child process can get the process ID of its parent process by calling the Getppid () function.

Multi-process

After the fork () function is successfully called, the PCB and user memory space will be requested for the child process. A child process is a copy of the parent process. The user space copies all the data from the parent process user space (code snippets, data segments, BBS, heaps, stacks), replicating the vast majority of information in the parent process's kernel space PCB . The child process inherits from the parent process the following example properties: Valid user, group number, process group number, environment variable, The shutdown flag, signal processing mode settings, signal masking collection, current working directory, root directory, file mode mask, file size limit, and open file descriptors for file Execution (special note: Share the same file table entry). I can assume that parent-child processes are almost identical because they have the same system resources (except for their respective PID)?

Multithreading

Instead of having a unique virtual address space, the thread shares the address space with other threads in the same process. But the threads each have separate registers (which are otherwise volatile) and stacks (such as local variables for each thread).

Multithreading vs Multi-process
      • Multi-threaded heap memory sharing, while the process is independent of each other, inter-thread communication can be directly based on shared memory to achieve, more than the process of those common multi-process communication way lighter.
      • In context switching, both the multi-threaded and the process involve the register, the stack save, but the thread does not need to switch the page map (virtual memory space), file descriptors, etc., so the context of the thread switch is more than the process of light-weight
      • Multiple processes are more secure than multithreading, and a process basically does not affect another process
        In the actual development, the general different tasks (can be a thread, process called a task) need to communicate, the use of multi-threaded scene than many processes. However, multiple processes have higher fault tolerance, the crash of a process will not cause the whole system to crash, and in the case of high task security, take multi-process.

Processes and threads (to be continued)

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.