650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/51/41/wkiom1rjdwtrg8_iaacibsrfrf8897.jpg "title=" 1.png "alt=" wKiom1RjDWTRg8_ Iaacibsrfrf8897.jpg "/>
See execution results:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/51/3f/wkiol1rjdeddsjjzaabcagevzug821.jpg "title=" 2.png "alt=" Wkiol1rjdeddsjjzaabcagevzug821.jpg "/>
It's obviously 8s.
=============================
This is a time-consuming process that does not occupy the processor, and we can run it in a background
To achieve the goal of saving time. See the following improvements:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/51/41/wkiom1rjdyky0e7faadyls6w8ou881.jpg "title=" 3.png "alt=" Wkiom1rjdyky0e7faadyls6w8ou881.jpg "/>
Use "{}" to turn the main executor into a block, with & in the background, four executions all put into the background, we
Requires a wait instruction, waiting for all background process execution to finish,
Otherwise the system will not wait, go directly to follow-up instructions, know the entire program is over.
Look at the results:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/51/41/wkiom1rjdzwssci0aab8xdhis2g154.jpg "title=" 4.png "alt=" Wkiom1rjdzwssci0aab8xdhis2g154.jpg "/>
As you can see, the time has been greatly shortened!
============================
Although the above experiment achieves the goal of multithreading concurrency, there is a flaw that can not control
The number of processes running in the background.
To control the process, we introduced the pipeline and file operators .
Nameless pipes: That's what we use often. For example: Cat text | grep "ABC"
That "|" is the pipeline, but it is nameless, can be directly as a data channel of two processes
Famous pipe: Mkfilo can create a pipeline file, for example: Mkfifo fifo_file
Pipeline has a feature, if there is no data in the pipeline, then the operation of the pipeline data will stall until
The pipeline enters the data, and then it is read out before terminating the operation, in the same vein, the operation of the write pipeline
If there is no read operation, this action will also stall.
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/51/3f/wkiol1rjdhscsifxaabjodvrld4004.jpg "title=" 5.png "alt=" Wkiol1rjdhscsifxaabjodvrld4004.jpg "/>
When we try to write data in a pipe file with echo, there is no process to read it, so
It will stay there waiting for the read operation, at which point we are reading with the cat instruction on the other terminal
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/51/3f/wkiol1rjdietedaaaacaugambvs448.jpg "title=" 6.png "alt=" Wkiol1rjdietedaaaacaugambvs448.jpg "/>
You will find that once the read operation is executed, the write operation can be completed smoothly, the same way, the first read operation is the same:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/51/41/wkiom1rjddcgjvj-aabs2fqmj8i213.jpg "title=" 7.png "alt=" Wkiom1rjddcgjvj-aabs2fqmj8i213.jpg "/>
Because there is no data in the pipeline, the read operation is stuck there waiting for the data to be written
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/51/41/wkiom1rjdeirud6laac6gm8vly8443.jpg "title=" 8.png "alt=" Wkiom1rjdeirud6laac6gm8vly8443.jpg "/>
Once the data is written, the read operation is completed immediately.
The above experiment, to see that just a pipe file seems difficult to achieve our purpose (control the number of background threads),
So the next introduction of file operators, here only to do a simple introduction, if not familiar with the data can be self-access.
System start, the corresponding device is automatically bound to three file operators 0 1 2 corresponding to stdin, stdout, stderr.
You can see these three three files in the/PROC/SELF/FD
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/51/41/wkiom1rjdfwa3cgpaadh8c2ud4a144.jpg "title=" 9.png "alt=" Wkiom1rjdfwa3cgpaadh8c2ud4a144.jpg "/>
The contents of the output to these three files will be displayed. Just because the monitor is bound as the most commonly used output device.
We can define the EXEC command and bind the file operator.
File operators are generally available from n-1
The definition of n ulimit-n here is worth
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/51/43/wkiom1rjedfamkxraaeulgu7scg650.jpg "title=" 10.png "alt=" Wkiom1rjedfamkxraaeulgu7scg650.jpg "/>
can see my n value is 1024, so the file operator can only use 0-1023, can be defined only 3-1023.
Directly on the code, and then analyze the meaning of each line of code according to the code:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/51/42/wkiol1rjefcdp8tqaahp-vzs6ue440.jpg "title=" 1.png "alt=" Wkiol1rjefcdp8tqaahp-vzs6ue440.jpg "/>
Line 3rd: Accept the operation of Signal 2 (CTRL +c)
exec 1000>&-and exec 1000<&-are the means to turn off fd1000
Build bindings can be implemented with exec 1000<>testfifo, but must be written separately when closing
> read bind,< Identity Write binding <> all operations that identify the file descriptor 1000 are equivalent to testfifo the pipe file.
第5-7 Line: Create pipeline file, file operator binding, delete pipeline file, respectively
There may be questions about why you can't use piping files directly.
In fact, this is not superfluous, just explained the pipeline file is an important feature, that is, read and write must exist at the same time, a lack of one operation, the other is stuck, and the binding file operator just solves this problem. (As for why, I have not studied understand, have to know also please inform, thank you)
第9-12: Write to the file operator. Write 10 blank lines through a for loop, and this 10 is the number of background threads we want to define.
Why write a blank line instead of 10 characters?
This is because the pipe file is read in the behavior unit.
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "spacer.gif"/> 650) this.width=650; "Src=" http ://s3.51cto.com/wyfs02/m02/51/43/wkiom1rjez7towvgaactn4e607g717.jpg "title=" 2.png "alt=" Wkiom1rjez7towvgaactn4e607g717.jpg "/>
When we tried to read one of the characters in the pipeline with read, the result was unsuccessful, and just now we have confirmed that using cat can be read.
第17-24: This assumes that we have 100 tasks that we want to implement when we ensure that only 10 processes in the background are running synchronously.
The role of read-u1000 is to read a line in a pipeline, where it reads a blank line.
After reducing the operation of an empty line in the middle school, perform a task (of course, in the background), it is important to note that this task will write a blank line to the file operator after the end of the execution of the background, this is the point, if we do not in some case at some point in time to write to the operator empty line, then the result
After putting 10 tasks in the background, read-u1000 is always paused because there are no empty rows to read in the operator. There is no need to explain, paste the results of the implementation:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/51/42/wkiol1rjehfxbiqaaamltdsjndw507.jpg "title=" 3.png "alt=" Wkiol1rjehfxbiqaaamltdsjndw507.jpg "/>
You can see that only 10 processes are running in each pause.
Time consuming 50s
Altogether 100 missions, 10 each, each 5s exactly 50s
The result graph above is so regular because the 100 tasks we perform are the same time,
For example, the process of putting the first 10 tasks into the background is almost negligible, that is to say
These 10 tasks can be run at the same time, and of course they can be considered to be finished at the same time, and according to the analysis just now,
At the end of a task, a blank line is written to the file descriptor, and since it is finished at the same time, it must be a blank line written at the same time.
So the next batch of tasks runs almost at the same time, so the cycle goes on.
In practice, this is certainly not the case, for example, the first to put in the background to perform tasks, is the most time-consuming,
Then he's going to be the last one. Execution is complete.
So, in fact, as long as a task is completed, the next task can be placed in the background and executed concurrently.
This article is from the "retrograde person" blog, please be sure to keep this source http://lydygly.blog.51cto.com/2837715/1575730
Shell's multi-process