|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
[Discussion] paper written with the problem of more than one process At the same time more open process in the same way with a document written under the conditions of :
1. If we add [/b] [b] write, all processes will be written into all the details of keeping documents say the process has been completed between each other into the operation of the [b] shows [/b];
2. If [b] [/b] covered write, only the last one into the operation of the implementation process will preserve its contents into the document, that is to say the process has been completed into operation between each other is not visible [/b] [b].
Why? Hope everyone speak, made their own suggestions.
FOSS tests are as follows : (I used the test environment for RHLinux8.0)
[code]#include "Unistd.h>;
#include "Stdio.h>;
#include "Stdlib.h>;
#include "String.h>;
#include "Memory.h>;
#include "Fcntl.h>;
#include "Signal.h>;
#include "Sys/types.h>;
//#define APPEND
# OUTFILE "abc"
# 3 CHLDCNT
LOOPCNT # 2
BUFSIZE # 1
Static int ChildSigNo;
Static void ChildSigProc (int SigNo)
{
ChildSigNo = SigNo;
}
Static void Child (int ChildNum)
{
Int File;
Char Buffer[BUFSIZE];
Memset (Buffer, ChildNum + '0', BUFSIZE);
Signal (SIGTERM, ChildSigProc);
Signal (SIGUSR1, ChildSigProc);
Kill (getppid (), SIGUSR1);
If ((File = open (OUTFILE, O_WRONLY | O_CREAT
#ifdef APPEND
| O_APPEND
#endif
) ) >;= 0 ) {
While (pause () ====== -1) (
If (ChildSigNo ====== SIGUSR1) (
Write (File, Buffer, BUFSIZE);
}
Else (
Break;
}
}
Close (File);
}
Exit (0);
}
Static void ParentSigProc (int SigNo)
{
}
Int main ()
{
Int i, j, k;
Pid_t pid[CHLDCNT];
Signal (SIGCHLD, SIG_IGN);
Signal (SIGUSR1, ParentSigProc);
For (i = 0 i "CHLDCNT; i + +) (
If ((pid[i] = fork ()) ====== 0)
Child (i);
Else if (pid[i] "; 0) (
If (pause ()> -1)
Break;
}
Else
Break;
}
Sleep (1);
If (i ====== CHLDCNT) (
For (j = 0, j "LOOPCNT; j + +) (
For (k = 0 k "CHLDCNT; k + +) (
Kill (pid[k], SIGUSR1);
Sleep (1);
}
}
}
For (; i ";= 0 i --) (
If (pid[i] "; 0) (
Kill (pid[i], SIGTERM);
Sleep (1);
}
}
Return 0
}
[/code] |
[Discussion] paper written with the problem of more than one process 1) O_WRONLY
Write function is handled this way, such as the current position into five, into the length of 10 to 20 length of the document. Then write only update the content will not change the length of the document. If the length of the 16 included, in addition to updating content, but also the length of the document update. [color=green] Positions for each privatization process, but for more than the length of the document sharing process. [/color] Other words, a process of opening a document, based on the length of the process to change the document, the original process did not know.
Used in this manner, a number of mutual process will be included in the coverage.
2) O_WRONLY|O_APPAND
Then write into the way 1), into, first into the localization process to the end of the document, and then into, which is an atomic operation, it certainly does not cover the contents into other processes. |
[Discussion] paper written with the problem of more than one process Oh, you can refer to the "high-level programming environment for Unix," the third chapter, I could not put it more clearly stevens.
[code]
3. Added to a document 11.1
Consider a process that data should be added to the tail end of a document. Early support is not open to the UNIX版本
O ̲ APPEND choice items, the procedures were developed into the following forms :
If (lseek (fd, 0L, 2) "0) /*position to EOF*/
Err_sys ( "lseek error");
If (write (fd, buff, 100)> 100) /*and write*/
Err_sys ( "write error");
For a single process, this process can be normal, but if more than process, it will create problems. (If this process
Orderly process carried out simultaneously by a number of different news added to a paper diary, this situation would arise. )
Assumed to be two separate processes A and B are added to the same document operation. Each process has been to open the document,
O ̲ APPEND without using signs. At this time the relationship between the structure of the data in figure 3 - 2 as shown. Each process has its own
Their table, but, v nodes sharing a table item. A process of transfer of lseek assumption that it would A process for the document
The current provision for displacement 1 5 0 0 bytes (the tail end of the current document). Then make the process of switching process B core operations. B. Implementation Process
Lseek also be set up to document the current displacement 1 5 0 0 bytes (the tail end of the current document). Call B then write it
B of the document to the current document displacement 1 6 0 0. Because of the increased length of the document, the core of the node v
The update to the current document length 1 6 0 0. Then, another kernel A process to make the process of switching to resume operations. When the write A Call
, A displacement from the current document (1 5 0 0) data will be written document. This substitution also wrote the text just a process B
Pieces of data.
The problem here lies with the logic operation "position paper to boot stall, and then wrote" Call the use of two separate functions.
The way to solve the problem is to bring these two operations for other processes becomes an atomic operation. No more than one request
Calling the operation can function as atomic operations, in two transfer function between the kernel may be linking into temporary
Way (as we have assumed).
UNIX provides a method for such operations as atomic operations, the methodology is installed in the open documents
O ̲ APPEND signs. As described in the previous section, which has made the core of each of these documents before writing, the process will
At present the document to set up the boot of displacement, and therefore no longer required in each written before calling lseek.
[/code] |
[Discussion] paper written with the problem of more than one process The two upstairs withheld consent.
I think this is a two way inconsistency, it is uncomfortable, not perfect. |
[Discussion] paper written with the problem of more than one process That the world was not a perfect world. : D |
| |