multiple read() calls
- From: "subramanian100in@xxxxxxxxx, India" <subramanian100in@xxxxxxxxx>
- Date: Wed, 23 Jan 2008 22:27:29 -0800 (PST)
I am using Redhat Enterprise Linux kernel 2.6.9 with gcc 3.4.3
Consider the following program.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main(int argc, char** argv)
{
char arr[5] = { 0 };
ssize_t no_of_chars_read;
errno = 0;
if ((no_of_chars_read =
read(STDIN_FILENO, arr, sizeof(arr) - 1)) < 0)
{
printf("read() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
errno = 0;
if (write(STDOUT_FILENO, arr, no_of_chars_read) !=
no_of_chars_read)
{
printf("write() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
errno = 0;
if ((no_of_chars_read =
read(STDIN_FILENO, arr, sizeof(arr) - 1)) < 0)
{
printf("second read() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
errno = 0;
if (write(STDOUT_FILENO, arr, no_of_chars_read) !=
no_of_chars_read)
{
printf("write() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
When I run this program as
[subbu@localhost doubts]$ ./a.out
1234567890abcdefghijklmnopqrstuvwxyz
the following output is produced:
12345678[subbu@localhost doubts]$ 90abcdefghijklmnopqrstuvwxyz
bash: 90abcdefghijklmnopqrstuvwxyz: command not found
[subbu@localhost doubts]$
My questions:
1)The second read() call does not wait for input. How does the second
read() call get input? Shouldn't the characters remaining after the
first read() call be ignored(looks like the characters remaining after
the first read() call are available in some buffer which I am unable
to understand) ?
2)How does "90abcdefghijklmnopqrstuvwxyz" get printed at the command
prompt?
Kindly clarify.
Thanks
V.Subramanian
.
- Follow-Ups:
- Re: multiple read() calls
- From: Sergei Organov
- Re: multiple read() calls
- Prev by Date: Re: RS232 Redirector Program in C using Linux
- Next by Date: Re: multiple read() calls
- Previous by thread: Kdevelop: executable works, debug run does not?
- Next by thread: Re: multiple read() calls
- Index(es):
Relevant Pages
|