Stopping the program if the next process in the pipeline was stopped

when I run the following pipeline:

cat my_large_file.txt | head | wc

the process stops almost immediately. OK

but when i run my java program

java MyProgramReadALargeFile my_large_file.txt | head | wc

the output from "wc" is printed on stdout, but the java program continues to work . How can he detect that the pipeline is closed?

Thank,

+3
source share
3 answers

Most UNIX utilities pay attention to SIGPIPE , which, when received, indicates that the thread to which the process is running does not listen at the other end, so there is no need to continue to work.

- "java posix signals" , Java.

+1

"head" 10 , , Java , "head" "wc" . , Java, System.out , , . , JVM System.out( /dev/null ).

( , , , "cat" ""?)

+1

, System.out java prog. /dev/null, , Java, .. Exit().

"cat", , . , "MyProgramReadALargeFile". -, .

, "cat", ,

cat my_large_file.txt > /dev/null

, .

0

Source: https://habr.com/ru/post/1729562/


All Articles