Process and thread, process thread

Source: Internet
Author: User

Process and thread, process thread

Reference blog: http://blog.csdn.net/zheng548/article/details/54669908

1. Multiple programs

Multi-channel programming technology is the first technology introduced by the operating system. It is designed to allow multiple programs to enter the memory at the same time and allow them to run in the CPU alternately, they share various hardware and software resources in the system. When a program is paused due to an I/O Request, the CPU immediately switches to another program. The purpose is to improve the system efficiency through CPU utilization.

Before the introduction of multi-channel programming technology, multiple programs were executed in serial. Only one program counter (PC, program counter) exists. After a program is executed, the next program will be executed. Multi-channel programming technology allows multiple programs to enter the memory and run at the same time, it is necessary to allocate a program counter for each program. If four programs are executing concurrently in the memory, four program counters are required.

1.1 The running process of a single program:
In program A's computing, I/O is idle, and in program A's I/O operations, the CPU is idle (Program B is also the same). After job A is completed, B can enter the memory to start working. The two are serial and the total completion time = T1 + T2.

 

The running process of more than 1.2 programs is as follows:
The A and B programs are stored in the memory at the same time. under the control of the system, they can be interspersed with each other and run on the CPU alternately: when program A waives the CPU because it requests an I/O operation, program B can use the CPU to run, so that the CPU is no longer idle, the I/O devices that are performing a I/O operation are not idle. Obviously, both the CPU and I/O devices are in the "busy" status, which greatly improves the resource utilization, this also improves the system efficiency. The time required for completing A and B is <T1 + T2.

Multi-channel programming technology not only makes full use of the CPU, but also improves the utilization of I/O devices and memory, this improves the resource utilization and system throughput of the entire system (number of jobs (programs) processed per unit time), and ultimately improves the efficiency of the entire system.

Ii. Time-Sharing System

As the CPU speed is constantly improved and time-sharing technology is adopted, a computer can connect multiple user terminals at the same time, and each user can use the computer online on his terminal, as if he had exclusive machine.

Time-sharing technology:Divides the running time of the processor into short time slices, and distributes the processors to various online jobs in turn based on the time slice.

If a job cannot complete its computing in the time slice assigned to it, the job is temporarily interrupted, and the processor is used by another job, waiting for the next round to continue running. Because of the fast speed of the computer and the fast rotation of jobs, the impression is that he exclusive to a computer. Each user can send various operation control commands to the system through their own terminal to complete the operation of the job with full human-computer interaction.

A computer system with the above features is called a time-sharing system, which allows multiple users to use computers online at the same time.

 


Features:
(1) multiplexing. Several users use one computer at the same time. At the micro level, users use computers in turn, while at the macro level, users work in parallel.
(2) interaction. The user can further submit a new request to the system based on the system's response to the request. This kind of work that enables human-machine interaction between users and the system is obviously different from the batch processing system. Therefore, a time-sharing system is also called an interactive system.
(3) independence. Users can operate independently of each other without interfering with each other. The system ensures the integrity of each user's program and does not confuse or corrupt each other.
(4) Timeliness. The system can respond to user input in a timely manner. One of the main indicators of time-sharing system performance is the response time, which refers to the time required from the terminal to the system to respond.

The main goal of the time-sharing system is to timely respond to users, that is, it does not take a long time for users to wait for each command to process.

The time-sharing system can accommodate dozens or even hundreds of users at the same time. Due to limited memory space, the storage method of swap (also called swap) is usually used. The next step is to put a job that is not "in turn" into the disk. Once "in turn", it will be transferred to the memory. After the time slice is used up, store jobs back to the disk (commonly known as "rolling in" and "rolling out"), so that the same storage area can take turns to serve multiple users.

Multi-user time-sharing systems are the most common operating systems in today's computer operating systems.

3. Process)

Multi-channel programming technology allows multiple programs to enter the memory and run at the same time. In such a concurrent environment, how can we describe and portray such programs? Therefore, the "process" is introduced ".

The program cannot run independently. It can only run when it is loaded into the memory, and the system allocates resources for it. Such a program is called a process. The difference between a program and a process is that a program is a set of commands, and it is a static description text of the process running. A process is an execution activity of a program and is a dynamic concept.

A process is a running activity of a program with certain independent functions. A process is an independent unit for the system to allocate and schedule resources.

Features

  • A process is an execution process of a program. If the program is executed twice or even multiple times, two or more processes are required.
  • A process is an abstraction of a running program. It represents the running CPU, also known as the process is an abstraction of the CPU. (Virtual technology supports changing a CPU into multiple virtual CPUs)
  • System resources (such as memory and files) are allocated by process.
  • The operating system allocates an independent address space for each process.
  • The operating system uses "scheduling" to grant control to the process.
4. Thread)

Why can applications execute threads concurrently after the thread concept is introduced? Why do we need to dispatch threads in the process?

First, we introduced the process concept, although the processConducive to resource management and protection. However, in practical application, the process has the following problems:
1. High Cost and overhead of process switching;
2. You also need to execute multiple programs in parallel in a process to implement different functions.
3. The process sometimes has low performance.

An operating system is like a factory. There are many production workshops in the factory. Different workshops produce different products. Each workshop is equivalent to a process, and your factory is poor, if the power supply is insufficient, only one workshop can be powered at a time. In order to allow all workshops to be produced at the same time, the electricians in your factory can only supply power to different workshops at a time, but it is your turn to qq workshop, I found that there was only one working worker and the production efficiency was extremely low. What should I do to solve this problem ?.... That's right. You must have thought of adding a few more workers to let several workers work in parallel. Every worker is a thread!

  There are three considerations for introducing threads:

4.1 concepts

A thread is the smallest unit that the operating system can schedule operations. It is included in the process and is the actual operating unit of the process. A thread refers to a single-Order Control Flow in a process. A process can be concurrently executed by multiple threads, and each thread can execute different tasks in parallel.

A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.Suppose you're reading a book, and you want to take a break right now, but you want to be able to come back and resume reading from the exact point where you stopped. One way to achieve that is by jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.If you have a roommate, and she's using the same technique, she can take the book while you're not using it, and resume reading from where she stopped. Then you can take it back, and resume it from where you were.Threads work in the same way. A CPU is giving you the illusion that it's doing multiple computations at the same time. It does that by spending a bit of time on each computation. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.On a more technical level, an execution context (therefore a thread) consists of the values of the CPU's registers.Last: threads are different from processes. A thread is a context of execution, while a process is a bunch of resources associated with a computation. A process can have one or many threads.Clarification: the resources associated with a process include memory pages (all the threads in a process have the same view of the memory), file descriptors (e.g., open sockets), and security credentials (e.g., the ID of the user who started the process).
4.2 thread attributes
  • Identifier ID
  • Status and status conversion are available, so you need to provide some status conversion operations.
  • When not running, you need to save the context, so you need registers such as program counters.
  • Has its own stack and stack pointer
  • Share the address space and other resources of the process
V. Differences between threads and processes
1.Threads share the address space of the process that created it; processes have their own address space.2.Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.3.Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.New threads are easily created; new processes require duplication of the parent process.4.Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.5.Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.

1. threads share memory space, and the memory of processes is independent.

2. The thread can directly access the data part of the process. The process has the data part of its parent process which is independent of each process.

3. Direct communication between threads of the same process (direct communication involves data sharing and information transmission). to communicate between two processes, you must use an intermediate proxy.

4. It is easy to create a new thread. To create a new process, you need to clone its parent process.

5. One thread can control and operate other threads in the same process, but the process can only operate sub-processes.

6. Modifications to the main thread may affect modifications to other threads in the process. Modifications to a parent process will not affect other child processes (as long as the parent process is not deleted)

Related Article

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.