Control stript which is runing in background.

jak nospam at please.ty
Sat Jan 2 06:06:42 EST 2021


Il 02/01/2021 01:07, Alan Bawden ha scritto:
> jak <nospam at please.ty> writes:
> 
>     Il 01/01/2021 06:23, Alan Bawden ha scritto:
>     > jak <nospam at please.ty> writes:
>     >
>     >     Running the command:
>     >
>     >     $ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > cmdpipe
>     >
>     >     the three texts do not mix....
>     >
>     > The three texts do not mix as long at the contents of bible.txt is short
>     > enough (and provided `cat' only calls `write' once).  In the POSIX
>     > specification, the manual page for the `write' system call describes
>     > writing to a pipe or FIFO as follows:
>     >
>     >    Write requests of {PIPE_BUF} bytes or less shall not be interleaved
>     >    with data from other processes doing writes on the same pipe.  Writes
>     >    of greater than {PIPE_BUF} bytes may have data interleaved, on
>     >    arbitrary boundaries, with writes by other processes, whether or not
>     >    the O_NONBLOCK flag of the file status flags is set.
>     >
>     Ok. And...
>          ...Running the command:
> 
>          $ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > cmdpipe
> 
>          the three texts do not mix....
> 
> Saying it again doesn't make it any more true.  If bible.txt is large
> enough, they most definitely DO mix!  Just to make sure I wasn't missing
> something, I tested your exact command before I sent my previous reply.
> They mixed.
> 
You're right it's mixed. I tried this:
terminal 1:

$ python3 pmkfifo.py > output.txt

terminal 2:

cat bible.txt > mypipe & cat bible.txt > mypipe & cat bible.txt > mypipe

result:

-rw-rw-r-- 1 jak jak  4351187 gen  1 02:56 bible.txt
prw-rw-r-- 1 jak jak        0 gen  2 11:40 mypipe
-rw-rw-r-- 1 jak jak 13053561 gen  2 11:40 output.txt
-rw-rw-r-- 1 jak jak      237 gen  1 02:46 pmkfifo.py

then I split ouput.txt:

$ split -n 3 -d output.txt result

result:

-rw-rw-r-- 1 jak jak  4351187 gen  1 02:56 bible.txt
prw-rw-r-- 1 jak jak        0 gen  2 11:40 mypipe
-rw-rw-r-- 1 jak jak 13053561 gen  2 11:40 output.txt
-rw-rw-r-- 1 jak jak      237 gen  1 02:46 pmkfifo.py
-rw-rw-r-- 1 jak jak  4351187 gen  2 11:41 result00
-rw-rw-r-- 1 jak jak  4351187 gen  2 11:41 result01
-rw-rw-r-- 1 jak jak  4351187 gen  2 11:41 result02

but:

$ cmp result00 result01
result00 result01 differ: byte 1, line 1

$ cmp result01 result02
result01 result02 differ: byte 1, line 1

...but not happy I wanted to try sure that each process used a single 
write and I wrote this program in C:

int main()
{
     FILE *fp;
     long len, readed;
     char *buffer;

     if((fp = fopen("bible.txt", "rt")) == NULL)
         return -1;
     // get file size
     if(!fseek(fp, 0, SEEK_END))
     {
         len = ftell(fp);
     }
     else
         return -1;
     // build buffer
    if((buffer = malloc(len)) == NULL)
         return -1;
     // reset position
     if(fseek(fp, 0, SEEK_SET))
         return -1;
     if((readed = fread(buffer, len, 1, fp)) != 1)
         return -1;
     if((fwrite(buffer, len, 1, stdout)) < 1)
	return -1;
     fflush(stdout);
     free(buffer);
     fclose(fp);
     return 0;
}

... and runned:

$ ./mycat > mypipe & ./mycat > mypipe & ./mycat > mypipe

I, however, got the same result.


More information about the Python-list mailing list