How does xargs know when a stdin input ends, so that it can start processing it? [duplicate]











up vote
7
down vote

favorite
1













This question already has an answer here:




  • How does a program detect the end of stdin input?

    1 answer




After reading Stephen Kitt's reply, xargs waits for receiving the stdin input before processing any of the input, such as splitting it into arguments.



How does xargs know when a stdin input ends, so that it can start processing it?



Is -E used for specifying the end of a stdin input?



Without it, how does xargs knows when it ends? Is there some timeout?










share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by muru, Michael Homer, Thomas, RalfFriedl, Jeff Schaller Nov 25 at 15:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 5




    How does any program know when stdin ends? How does cat know to exit when it's reached the tail of the file it's reading, for example? Why is the answer to how xargs knows when it's reached the end of its stdin any different?
    – Charles Duffy
    Nov 25 at 1:33






  • 1




    I don't know. Could you enlighten me?
    – Ben
    Nov 25 at 1:34










  • That actually helps to clarify the question quite a lot -- that you're not looking for an xargs-specific answer but a generic UNIX-file-operations answer. Stephen is correct -- read() returning 0 indicates EOF. From the read(2) man page, section RETURN VALUES: If successful, the number of bytes actually read is returned. Upon reading end-of-file, zero is returned. Otherwise, a -1 is returned and the global variable errno is set to indicate the error.
    – Charles Duffy
    Nov 25 at 1:39












  • ...so, read() will either actually read some bytes (and return a positive number with the number of bytes read), or fail to read some bytes (and return a negative number that indicates how/why it failed), or hit end-of-file (and return 0).
    – Charles Duffy
    Nov 25 at 1:41















up vote
7
down vote

favorite
1













This question already has an answer here:




  • How does a program detect the end of stdin input?

    1 answer




After reading Stephen Kitt's reply, xargs waits for receiving the stdin input before processing any of the input, such as splitting it into arguments.



How does xargs know when a stdin input ends, so that it can start processing it?



Is -E used for specifying the end of a stdin input?



Without it, how does xargs knows when it ends? Is there some timeout?










share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by muru, Michael Homer, Thomas, RalfFriedl, Jeff Schaller Nov 25 at 15:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 5




    How does any program know when stdin ends? How does cat know to exit when it's reached the tail of the file it's reading, for example? Why is the answer to how xargs knows when it's reached the end of its stdin any different?
    – Charles Duffy
    Nov 25 at 1:33






  • 1




    I don't know. Could you enlighten me?
    – Ben
    Nov 25 at 1:34










  • That actually helps to clarify the question quite a lot -- that you're not looking for an xargs-specific answer but a generic UNIX-file-operations answer. Stephen is correct -- read() returning 0 indicates EOF. From the read(2) man page, section RETURN VALUES: If successful, the number of bytes actually read is returned. Upon reading end-of-file, zero is returned. Otherwise, a -1 is returned and the global variable errno is set to indicate the error.
    – Charles Duffy
    Nov 25 at 1:39












  • ...so, read() will either actually read some bytes (and return a positive number with the number of bytes read), or fail to read some bytes (and return a negative number that indicates how/why it failed), or hit end-of-file (and return 0).
    – Charles Duffy
    Nov 25 at 1:41













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1






This question already has an answer here:




  • How does a program detect the end of stdin input?

    1 answer




After reading Stephen Kitt's reply, xargs waits for receiving the stdin input before processing any of the input, such as splitting it into arguments.



How does xargs know when a stdin input ends, so that it can start processing it?



Is -E used for specifying the end of a stdin input?



Without it, how does xargs knows when it ends? Is there some timeout?










share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












This question already has an answer here:




  • How does a program detect the end of stdin input?

    1 answer




After reading Stephen Kitt's reply, xargs waits for receiving the stdin input before processing any of the input, such as splitting it into arguments.



How does xargs know when a stdin input ends, so that it can start processing it?



Is -E used for specifying the end of a stdin input?



Without it, how does xargs knows when it ends? Is there some timeout?





This question already has an answer here:




  • How does a program detect the end of stdin input?

    1 answer








xargs






share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 24 at 22:51









Ben

2769




2769




New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by muru, Michael Homer, Thomas, RalfFriedl, Jeff Schaller Nov 25 at 15:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by muru, Michael Homer, Thomas, RalfFriedl, Jeff Schaller Nov 25 at 15:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 5




    How does any program know when stdin ends? How does cat know to exit when it's reached the tail of the file it's reading, for example? Why is the answer to how xargs knows when it's reached the end of its stdin any different?
    – Charles Duffy
    Nov 25 at 1:33






  • 1




    I don't know. Could you enlighten me?
    – Ben
    Nov 25 at 1:34










  • That actually helps to clarify the question quite a lot -- that you're not looking for an xargs-specific answer but a generic UNIX-file-operations answer. Stephen is correct -- read() returning 0 indicates EOF. From the read(2) man page, section RETURN VALUES: If successful, the number of bytes actually read is returned. Upon reading end-of-file, zero is returned. Otherwise, a -1 is returned and the global variable errno is set to indicate the error.
    – Charles Duffy
    Nov 25 at 1:39












  • ...so, read() will either actually read some bytes (and return a positive number with the number of bytes read), or fail to read some bytes (and return a negative number that indicates how/why it failed), or hit end-of-file (and return 0).
    – Charles Duffy
    Nov 25 at 1:41














  • 5




    How does any program know when stdin ends? How does cat know to exit when it's reached the tail of the file it's reading, for example? Why is the answer to how xargs knows when it's reached the end of its stdin any different?
    – Charles Duffy
    Nov 25 at 1:33






  • 1




    I don't know. Could you enlighten me?
    – Ben
    Nov 25 at 1:34










  • That actually helps to clarify the question quite a lot -- that you're not looking for an xargs-specific answer but a generic UNIX-file-operations answer. Stephen is correct -- read() returning 0 indicates EOF. From the read(2) man page, section RETURN VALUES: If successful, the number of bytes actually read is returned. Upon reading end-of-file, zero is returned. Otherwise, a -1 is returned and the global variable errno is set to indicate the error.
    – Charles Duffy
    Nov 25 at 1:39












  • ...so, read() will either actually read some bytes (and return a positive number with the number of bytes read), or fail to read some bytes (and return a negative number that indicates how/why it failed), or hit end-of-file (and return 0).
    – Charles Duffy
    Nov 25 at 1:41








5




5




How does any program know when stdin ends? How does cat know to exit when it's reached the tail of the file it's reading, for example? Why is the answer to how xargs knows when it's reached the end of its stdin any different?
– Charles Duffy
Nov 25 at 1:33




How does any program know when stdin ends? How does cat know to exit when it's reached the tail of the file it's reading, for example? Why is the answer to how xargs knows when it's reached the end of its stdin any different?
– Charles Duffy
Nov 25 at 1:33




1




1




I don't know. Could you enlighten me?
– Ben
Nov 25 at 1:34




I don't know. Could you enlighten me?
– Ben
Nov 25 at 1:34












That actually helps to clarify the question quite a lot -- that you're not looking for an xargs-specific answer but a generic UNIX-file-operations answer. Stephen is correct -- read() returning 0 indicates EOF. From the read(2) man page, section RETURN VALUES: If successful, the number of bytes actually read is returned. Upon reading end-of-file, zero is returned. Otherwise, a -1 is returned and the global variable errno is set to indicate the error.
– Charles Duffy
Nov 25 at 1:39






That actually helps to clarify the question quite a lot -- that you're not looking for an xargs-specific answer but a generic UNIX-file-operations answer. Stephen is correct -- read() returning 0 indicates EOF. From the read(2) man page, section RETURN VALUES: If successful, the number of bytes actually read is returned. Upon reading end-of-file, zero is returned. Otherwise, a -1 is returned and the global variable errno is set to indicate the error.
– Charles Duffy
Nov 25 at 1:39














...so, read() will either actually read some bytes (and return a positive number with the number of bytes read), or fail to read some bytes (and return a negative number that indicates how/why it failed), or hit end-of-file (and return 0).
– Charles Duffy
Nov 25 at 1:41




...so, read() will either actually read some bytes (and return a positive number with the number of bytes read), or fail to read some bytes (and return a negative number that indicates how/why it failed), or hit end-of-file (and return 0).
– Charles Duffy
Nov 25 at 1:41










2 Answers
2






active

oldest

votes

















up vote
13
down vote













To read its input, xargs uses the read function (see also the corresponding Linux manpage).



read reads the next available data from a file descriptor into an area of memory specified by the calling program. As used by xargs, read waits until either data is available, or an error occurs, or it determines that no more data will ever be available. It returns respectively a positive integer, -1, or 0 in each of these cases.



To determine when it’s finished reading its input (its standard input, or the input specified by the -a option), xargs looks for the specified end-of-file marker (if the -E option was used), or a 0 return value from read.



You can see this in action by running



printf '%s ' {1..1024} | strace -e read xargs -s 2048 -x





share|improve this answer






























    up vote
    1
    down vote














    How does xargs know when a stdin input ends, so that it can start processing it?




    xargs does not wait for the end of stdin before starting to process it:



    $ while date +%H:%S; do sleep 1; done | xargs -n2 echo
    16:51 16:52
    16:53 16:54
    16:55 16:56
    16:57 16:58
    ^C


    xargs knows that the stdin has ended just like any other program, eg. cat(1) or tee(1), by checking the return value of read(2), calling feof(3), etc. xargs will also treat a read error on stdin just as an end-of-file.






    share|improve this answer








    New contributor




    pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote













      To read its input, xargs uses the read function (see also the corresponding Linux manpage).



      read reads the next available data from a file descriptor into an area of memory specified by the calling program. As used by xargs, read waits until either data is available, or an error occurs, or it determines that no more data will ever be available. It returns respectively a positive integer, -1, or 0 in each of these cases.



      To determine when it’s finished reading its input (its standard input, or the input specified by the -a option), xargs looks for the specified end-of-file marker (if the -E option was used), or a 0 return value from read.



      You can see this in action by running



      printf '%s ' {1..1024} | strace -e read xargs -s 2048 -x





      share|improve this answer



























        up vote
        13
        down vote













        To read its input, xargs uses the read function (see also the corresponding Linux manpage).



        read reads the next available data from a file descriptor into an area of memory specified by the calling program. As used by xargs, read waits until either data is available, or an error occurs, or it determines that no more data will ever be available. It returns respectively a positive integer, -1, or 0 in each of these cases.



        To determine when it’s finished reading its input (its standard input, or the input specified by the -a option), xargs looks for the specified end-of-file marker (if the -E option was used), or a 0 return value from read.



        You can see this in action by running



        printf '%s ' {1..1024} | strace -e read xargs -s 2048 -x





        share|improve this answer

























          up vote
          13
          down vote










          up vote
          13
          down vote









          To read its input, xargs uses the read function (see also the corresponding Linux manpage).



          read reads the next available data from a file descriptor into an area of memory specified by the calling program. As used by xargs, read waits until either data is available, or an error occurs, or it determines that no more data will ever be available. It returns respectively a positive integer, -1, or 0 in each of these cases.



          To determine when it’s finished reading its input (its standard input, or the input specified by the -a option), xargs looks for the specified end-of-file marker (if the -E option was used), or a 0 return value from read.



          You can see this in action by running



          printf '%s ' {1..1024} | strace -e read xargs -s 2048 -x





          share|improve this answer














          To read its input, xargs uses the read function (see also the corresponding Linux manpage).



          read reads the next available data from a file descriptor into an area of memory specified by the calling program. As used by xargs, read waits until either data is available, or an error occurs, or it determines that no more data will ever be available. It returns respectively a positive integer, -1, or 0 in each of these cases.



          To determine when it’s finished reading its input (its standard input, or the input specified by the -a option), xargs looks for the specified end-of-file marker (if the -E option was used), or a 0 return value from read.



          You can see this in action by running



          printf '%s ' {1..1024} | strace -e read xargs -s 2048 -x






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 at 6:30

























          answered Nov 24 at 23:11









          Stephen Kitt

          159k24352425




          159k24352425
























              up vote
              1
              down vote














              How does xargs know when a stdin input ends, so that it can start processing it?




              xargs does not wait for the end of stdin before starting to process it:



              $ while date +%H:%S; do sleep 1; done | xargs -n2 echo
              16:51 16:52
              16:53 16:54
              16:55 16:56
              16:57 16:58
              ^C


              xargs knows that the stdin has ended just like any other program, eg. cat(1) or tee(1), by checking the return value of read(2), calling feof(3), etc. xargs will also treat a read error on stdin just as an end-of-file.






              share|improve this answer








              New contributor




              pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                1
                down vote














                How does xargs know when a stdin input ends, so that it can start processing it?




                xargs does not wait for the end of stdin before starting to process it:



                $ while date +%H:%S; do sleep 1; done | xargs -n2 echo
                16:51 16:52
                16:53 16:54
                16:55 16:56
                16:57 16:58
                ^C


                xargs knows that the stdin has ended just like any other program, eg. cat(1) or tee(1), by checking the return value of read(2), calling feof(3), etc. xargs will also treat a read error on stdin just as an end-of-file.






                share|improve this answer








                New contributor




                pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote










                  How does xargs know when a stdin input ends, so that it can start processing it?




                  xargs does not wait for the end of stdin before starting to process it:



                  $ while date +%H:%S; do sleep 1; done | xargs -n2 echo
                  16:51 16:52
                  16:53 16:54
                  16:55 16:56
                  16:57 16:58
                  ^C


                  xargs knows that the stdin has ended just like any other program, eg. cat(1) or tee(1), by checking the return value of read(2), calling feof(3), etc. xargs will also treat a read error on stdin just as an end-of-file.






                  share|improve this answer








                  New contributor




                  pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  How does xargs know when a stdin input ends, so that it can start processing it?




                  xargs does not wait for the end of stdin before starting to process it:



                  $ while date +%H:%S; do sleep 1; done | xargs -n2 echo
                  16:51 16:52
                  16:53 16:54
                  16:55 16:56
                  16:57 16:58
                  ^C


                  xargs knows that the stdin has ended just like any other program, eg. cat(1) or tee(1), by checking the return value of read(2), calling feof(3), etc. xargs will also treat a read error on stdin just as an end-of-file.







                  share|improve this answer








                  New contributor




                  pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered Nov 25 at 15:09









                  pizdelect

                  1262




                  1262




                  New contributor




                  pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  pizdelect is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.















                      Popular posts from this blog

                      Plaza Victoria

                      Puebla de Zaragoza

                      Musa