Serial vs. parallel

Source: Internet
Author: User

Summary: program or program segments have two ways of executing in the system. One is sequential execution, and one is concurrent execution. But most of the time we write a serial program, and the operating system does have to execute the program concurrently, which often results in some bugs that make the programmer feel "strange." This article discusses the concurrency problems under UNIX and Windows, and from the example we can see that the operating system is actually executing the program concurrently.

1. Questions raised:

Many people find that when you debug a program, you get the correct results when you run a single program entity, and the results are incorrect when you run two or more entities at the same time. This error is also easy for the novice programmer, because the operating system is executing the program concurrently, and the programmer is writing a serial program.

2, Serial and parallel:

There are two ways to execute a program. One is sequential execution and the other is concurrent execution. The so-called sequential execution means that the program section in the procedure must be executed in order, that is, only the previous program segment is finished, the later program section can be executed. This is a huge waste of CPU resources, such as there is a program in the system waiting for I/O input, so the CPU can not do anything but wait. In order to improve the efficiency of CPU and support multi-task operation, concurrency technology is introduced in the operating system. The so-called concurrency refers to the system of multiple programs or program segments can be executed at the same time, where the simultaneous execution does not mean that a certain time a multi-segment program in the same (unless there are multiple CPUs), but the CPU can divide the time into different segments. For example, before waiting for I/O, if using concurrent technology, when a program waits for I/O, the system can allocate CPU resources to another program, which can reduce CPU idle time and improve resource utilization.

3. How the operating system implements parallelism:

There are several technologies that implement concurrency in the operating system, the most common being multi-process and multi-threading technology. A process is a process in which a program runs once, and it is dynamic and has a life cycle. I think we can give an example to understand a process. The process of building a house is like this: first the designers design the house, then the construction staff according to the design of the drawings to build up the house. Here, we write the program code as if it is a well-designed drawing, and the process is to follow this drawing of the "house" to build up the entire process, that is, according to the "drawings" to achieve the entire program function process. A thread is a relatively independent, scheduled execution unit within a process. Threads exist in a process and can have multiple threads in each process.

4, the characteristics of parallelism:

1, non-reproducibility: also said that when a program is repeated execution, even if the initial conditions are the same, the results may not be the same.

2, intermittent: Due to multiple programs at the same time in the system execution, the system to allocate CPU time for each program, which will lead to a program may be deprived of the use of the CPU after the completion of the operation, resulting in the program is interrupted. In concurrent systems, many programs run according to the rules of execution----pause----execution, some of which are interrupted because the allocated CPU time is exhausted, and some are interrupted for synchronization.

3, loss of sealing: The program concurrent execution of shared system resources, so that the system can be funded by a number of programs to change, that is, one program can affect the operation of another program, so that the program is not running in a closed environment.

5, concurrency control technology:

The techniques for dealing with concurrency problems include signal, semaphore, file lock, mutex, critical area, event and so on. Some of these are used to solve the problem of mutual exclusion, some are used to solve the synchronization problem, the following I mainly discuss the use of file locks in concurrent operations. The so-called file lock on a file lock, when a process to lock a file, you can restrict other processes to access the file. For example, there are two processes A and B in the system to read and write to the same file, because A and B are executed concurrently in the system, so A and B read "dirty" data without locking. Now with the file lock, assuming that the first time a process has been locked, when the B process wants to read and write will be rejected, after a write data and then unlock the lock, then B can access the file and lock it, b read and write and then release the lock. So a, B will not read the dirty data, the operation results will be correct.

5, example demonstration:

The following are examples of two concurrent execution programs. The first is an example of a UNIX concurrency program. The function of the program is that two processes simultaneously read and write to a file, each time it reads a number plus 1 from the file and prints the number on the screen, then writes the added number into the file and repeats the operation 10 times. The second program also implements the same function, but it is a program under the WINODWS, which uses multithreading to implement concurrent operations. The following are the results of the program running:




Results analysis: When the program is executed sequentially, the result of the operation is that all the numbers are incremented sequentially. And now the result is not, this is because when a thread or a process from the file to read the number plus 1, after it put the number after the addition of 1 to the file, another B thread or process to obtain the CPU, so the B thread or process reads the original number, instead of adding 1 after the number.

Below we will add parallel processing technology to the above program. The first program adds a file lock to achieve mutual exclusion, the second program is mutually exclusive with the critical section, the following is the result of the operation:






Results analysis: From the above results we can see that the program works correctly after locking and gets the results we want. From the above results we can also see that these two threads are allocated time is also no rules, there is randomness. Concurrency technology is used in many programs, especially in service programs, only by using concurrency technology, it can allow multiple users to get services at the same time, and customers feel as if they are self-exclusive of the entire service.

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.