Home
Home Page
Circuits of blocking in Databases
Blocking of the Table
Blocking of page
Compression of the data with a view of economy of a place and acceleration of job Oracle
Compression of the data with a view of economy of a place and acceleration of job Oracle
Service of events in the SQL-server
Offered variant of the decision
SQL Server in questions and answers
Boards - how to benefit
Development Ajax: Use XML in searches and answers
Transfer of pairs name / value (name/value)
Transfer XML on the server
All in the frameworks
Creation of wap-pages (in a format *.wml)
How to recover Perl/CGI a script
Mezhprocessovye interactions
Kill of the target data
Correctness at construction of a resource!
Example of a tree
Links
 

 

Kill of the target data


How much poznaju Perl, all not perestaju to be surprised. So much pleasant surprises did not meet still anywhere. Language is so logical (if it is possible so to be expressed), that opens all secrets to the close{attentive} programmer, and without the often reference{manipulation} to the documentation. And recently, there was at me a necessity to filter the target data of a certain CGI-program:


Pogodite to knead fingers. First we shall drink on a mug kofeju, and I, at this time, shall depict a situation as a whole. What is the filter on the target data all understandably? Well if to whom it is not understandable, besides - we shall present. The program that there deduces that in STDOUT (a standard stream of a conclusion), and at this time any other program silently and imperceptibly steals these data and does{makes} with them all that will like. A real example? Well the very first that has occurred to me is a replacement of all URL on hyperlinks. Or in the help rasejannomu to the programmer eternally forgetting about HTTP-headings to check presence of these headings and to add them if it is necessary. Actually, all can be much more complex{difficult}. For example, a cutting of not normative lexicon (the such invisible censor) from the text of the message sent by means of the WEB-interface before it will be passed to input{entrance} SENDMAIL. Well and in such spirit.


In general, first our purpose, it somehow to palm off new STDOUT which we can read, to the program, from which we shall filter a conclusion. But here variants are possible{probable}. For example, there can be we want to organize a conclusion as transaction: or the program is carried out up to the end, and all contents are deduced, or, in case of a mistake it is necessary to dump{reset} the data, and to deduce{remove}, for example, LOCATION on page of processing of mistakes. That is, all depends on a level of the control over a filtered target stream. That would become absolutely understandable, about what I here raspinajus`, let's write idle time primerchik, a showing "stupid{blunt}" filter - date stamp of lines.



*!/usr/bin/perl-w

* nfilter.plfilter ();

for ($I = 0; $I <20; $I +) {

print " Output linen ";

}

sub filter {

die " Cannot fork "

unless defined ($fpid = open (STDOUT, "-"));

return if ($fpid! = 0);

num = 0;

while (<>) {

print " $num:t $ _ ";

$num ++;

}

exit;

}


Do not take in head to start. What, have already started? Then press Ctrl+C. For now for ever remember - it is necessary to close descriptors (all is desirable:). In what put? Why the program zavisla? All generated procesy are processes of a uniform problem{task}. Streams of input of a conclusion are automatically closed, when last process comes to the end. The design open (STDOUT, "-") implicitly causes fork. Recollect the documentation on file operations:



open (HANDLE, "$cmd"); * to direct the information on an input{entrance} of the program


And so, here the similar situation, only as the program here is created affiliated process. And as as a descriptor we specify STDOUT in the present{true} process he is redefined. As well as in a case with fork, concerning the data - their status for a moment before a call fork is duplicated. Thus, in affiliated process gets normal not redefined STDOUT. I shall notice, that open with the specified arguments implicitly causes fork, and as result returns the same values, as fork. Further, the program defines{determines} in what she a stream - if not in generated ($fpid! = 0) then comes back and emulates a conclusion of lines. The filter reads STDIN yet the data will not end. And the data will end, when the stream of input will be closed (for parental procesa, it is a stream of a conclusion). Parental proces has already finished the job, and the system waits when last process will come to the end what to close streams. And so on, and so on. Feel, where the dog is buried? After the conclusion of lines is completed, it is necessary to close a stream of a conclusion, what the filter accepting the target data through a stream of input has left a cycle



while (<>) {print " $num:t $ _ "; $num ++;}


So that, we take and correct



*!/usr/bin/perl-w

* nfilter.plfilter ();

for ($I = 0; $I <20; $I +) {

print " Output linen ";

} close (STDOUT);

sub filter {

die " Cannot fork "

unless defined ($fpid = open (STDOUT, "-"));

return if ($fpid! = 0);

num = 0;

while (<>) {

print " $num:t $ _ ";

$num ++;

}

exit;

}


Well it certainly, heflily primitively. But if we need only to filter, a stream of a conclusion will leave. And if we, for example, write super-system of processing of mistakes, it all the same a little. We shall present, that the such watchman filters a conclusion and at once sends it{him} at present STDOUT. And if there was a fatal mistake? We deduce{remove} the message on a mistake, but all this in dogonku to that stuff that has already been sent in STDOUT. Such processing of mistakes, as they say, " that dead a poultice ". It is possible to accumulate, certainly, a conclusion inside the filter and to deduce{remove} only entirely. In case of that, it is possible prishibit` generated proces with the help of the operator kill. But alas, the stream of a conclusion is already redefined irrevocably.


For the decision of this problem we should change the outlook cardinally. I joke, certainly. It is enough to recollect such useful functions as select and pipe. Function select substitutes STDOUT a new descriptor, and returns a descriptor of a stream of a conclusion which was actual for a moment before performance select, in other words current STDOUT. Function pipe connects two descriptors in a mode of reading - recording, that is creates the unilateral channel of data exchange. From here and the name - pipe.


There is very remarkable property of systems UNIX - all streams are equal. It is direct, communism any. Grandfather Lenin would be glad. And to us what advantage{benefit} from it? Well as, we, for example, can easily palm off functions select one of the descriptors connected by function pipe. It is natural, that we shall palm off what is intended for recording, differently I do not warrant{vouch} for consequences. In general such plain programmulina



*!/usr/bin/perl-w

* errfilter.plmy ($fpid, $oldout);

pipe (FOR_READ, FOR_WRITE);

select ((select (FOR_READ), $ = 1) [0]);

select ((select (FOR_WRITE), $ = 1) [0]);

start_filter ();

for ($I = 0; $I <20; $I +) {

print " Output linen ";

}

* Error ("bug");

close (FOR_WRITE);

waitpid ($fpid, 0);

sub start_filter {

die " Cannot fork "

unless defined ($fpid = fork);

unless ($fpid == 0) {

close (FOR_READ);

$oldout = select (FOR_WRITE);

return;

}

close (FOR_WRITE);

my ($out, $num) = (" ", 0);

while () {

$out. = " $numt $ _ "; $num ++;

}

close (FOR_READ);

print $out;

exit;

}

sub Error {

my ($error_text) = _;

select ($oldout);

kill KILL => $fpid;

close (FOR_WRITE);

print " Error: $error_textn ";

exit;

}


Well, have poured the next portion of coffee? Then we shall start analysis of flights. First of all, the program connects two descriptors FOR_READ and FOR_WRITE in the channel with the help pipe. Thus, FOR_READ it will turn out only for reading, and FOR_WRITE, accordingly, only for recording. About following two lines I shall say just they disconnect bufferizaciju. It is not important for us yet, they can be cleaned{removed} in general. Further function start_filter () is caused. On it{her} it is necessary vzgjanut` with all attentiveness.


First of all, function creates the duplicate of process. In a parent process the unnecessary descriptor for reading is closed, and in quality STDOUT one end of the created channel, that which only for recording is palmed off. After that the program parental proces comes back to the basic useless employment{occupation}, to be exact - emulates a conclusion. After the conclusion is completed, forgotten to close a descriptor of a conclusion, differently all will hang owing to above described. Further we wait, when the affiliated stream will come to the end (can zakommentirovat` this line, and see that it will turn out). On it job of the basic process is completed.


Let's return to function start_filter () in that his{its} part where kill is carried out directly. The end of the channel first of all unused in affiliated process is closed. Further, process in a cycle reads out the data from the channel and konkateniruet them in a variable $out. Well further all should be understandable. Start the program. Works? At least should.


Now clean{remove} comments from a line where function Error ("bug") is caused. Start the program again. Well, what result? We achieved it? (the Right answer yes if no you look that there ponapisali).


Let's see, that function Error () does{makes}. First of all restores a standard stream of a conclusion. At this time affiliated process about it does not suspect anything of a mode of accumulation of the data. The following severe operation kills affiliated proces. And affiliated proces still has deduced{removed} nothing in a stream of a conclusion. The ambassador the descriptor for recording is closed, and processing of a mistake is carried out. Well also what to pass last lines of head process it is carried out exit.


That's all. As you will put the received information into practice, your business.