Skip to main content
Added reference to POSIX for lseek on terminal devices and special case for Linux
Source Link
cg909
  • 7.5k
  • 1
  • 30
  • 31

//1 ./a.out :

If you do no redirection of stdin (no pipe and no <), stdin is inherited from the parent process. As you run a.out interactively in a shell, it inherits the terminal device that gets your keyboard input as stdin.

Terminal devices aren't usually seekable because they represent user interaction, but according to the POSIX standard lseek may return success and simply do nothing. On Linux lseek fails with an ESPIPE.

//2 ./a.out < /etc/passwd:

Here stdin is redirected to an open file. As /etc/passwd should be a regular file, it is seekable.

//3 cat < /etc/passwd | ./a.out:

Here you start two processes (cat and ./a.out) and connect them with a pipe.

cat (without other arguments) reads it stdin (/etc/passwd) and copies it to its stdout (the pipe connecting to ./a.out). This is not the same case as //2. From the perspective of ./a.out the stdin cannot seek because it is only a pipe connecting to another process.

//4 ./a.out < /var/spool/cron/FIFO:

Here you have a named pipe or similar special file. This case is similar to //3. You have an unidirectional connection to another process. And these are not seekable.

//1 ./a.out :

If you do no redirection of stdin (no pipe and no <), stdin is inherited from the parent process. As you run a.out interactively in a shell, it inherits the terminal device that gets your keyboard input as stdin.

Terminal devices aren't seekable because they represent user interaction.

//2 ./a.out < /etc/passwd:

Here stdin is redirected to an open file. As /etc/passwd should be a regular file, it is seekable.

//3 cat < /etc/passwd | ./a.out:

Here you start two processes (cat and ./a.out) and connect them with a pipe.

cat (without other arguments) reads it stdin (/etc/passwd) and copies it to its stdout (the pipe connecting to ./a.out). This is not the same case as //2. From the perspective of ./a.out the stdin cannot seek because it is only a pipe connecting to another process.

//4 ./a.out < /var/spool/cron/FIFO:

Here you have a named pipe or similar special file. This case is similar to //3. You have an unidirectional connection to another process. And these are not seekable.

//1 ./a.out :

If you do no redirection of stdin (no pipe and no <), stdin is inherited from the parent process. As you run a.out interactively in a shell, it inherits the terminal device that gets your keyboard input as stdin.

Terminal devices aren't usually seekable because they represent user interaction, but according to the POSIX standard lseek may return success and simply do nothing. On Linux lseek fails with an ESPIPE.

//2 ./a.out < /etc/passwd:

Here stdin is redirected to an open file. As /etc/passwd should be a regular file, it is seekable.

//3 cat < /etc/passwd | ./a.out:

Here you start two processes (cat and ./a.out) and connect them with a pipe.

cat (without other arguments) reads it stdin (/etc/passwd) and copies it to its stdout (the pipe connecting to ./a.out). This is not the same case as //2. From the perspective of ./a.out the stdin cannot seek because it is only a pipe connecting to another process.

//4 ./a.out < /var/spool/cron/FIFO:

Here you have a named pipe or similar special file. This case is similar to //3. You have an unidirectional connection to another process. And these are not seekable.

Source Link
cg909
  • 7.5k
  • 1
  • 30
  • 31

//1 ./a.out :

If you do no redirection of stdin (no pipe and no <), stdin is inherited from the parent process. As you run a.out interactively in a shell, it inherits the terminal device that gets your keyboard input as stdin.

Terminal devices aren't seekable because they represent user interaction.

//2 ./a.out < /etc/passwd:

Here stdin is redirected to an open file. As /etc/passwd should be a regular file, it is seekable.

//3 cat < /etc/passwd | ./a.out:

Here you start two processes (cat and ./a.out) and connect them with a pipe.

cat (without other arguments) reads it stdin (/etc/passwd) and copies it to its stdout (the pipe connecting to ./a.out). This is not the same case as //2. From the perspective of ./a.out the stdin cannot seek because it is only a pipe connecting to another process.

//4 ./a.out < /var/spool/cron/FIFO:

Here you have a named pipe or similar special file. This case is similar to //3. You have an unidirectional connection to another process. And these are not seekable.