multithreading - What will happen two different background process access the same file in unix? -


please explain me on below cmd:

touch temp.txt touch temp1.txt > temp.txt & cat temp.txt | wc -l >temp1.txt  & 

say temp.txt file empty file. so, present in temp.txt , temp1.txt files after execution.

could explain possible cases, since jobs running in background mode?

it depends. cat(1) might see or nothing in input file.

here's rough picture of what's going on in final 2 commands. (i'm ignoring touch(1) calls because synchronous.)

  • who >f1
    child forked, output file clobbered, output file moved child's stdout, , new program run.
  • cat f2 | wc -l>f2
    child forked, pipe made, child forked, children move pipe ends stdin or stdout appropriate, 1 child runs command open input file, other child clobbers output , on above.

i don't guarantee that's shell doing -- maybe clobbers before forks, etc. -- that's close enough. so, let's put these shell commands on time dimension:

[time =====================================================================>]  # >f1 -?->[clobber *f1*]-?->[dup stdout]-?->[exec who(1)]-?->[...]  # cat f1 | wc -l>f2 -?->[pipe]-?->[fork]-?->[dup pipe]-?->[clobber *f2*]-?->[dup stdout]-?->[exec wc(1)]                 +----?->[dup pipe]-?->[exec cat(1)]-?->[open *f1*]-?->[...] 

every 1 of -?-> arrows above may arbitrarily long or short -- cannot control that. maybe cat(1) run before first clobber (seeing old data?), maybe not. maybe run before who(1) has chance write output, maybe not.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -