Program 1:
#include<stdio.h>
void main()
{
printf("Hello\n");
}
Output:
$strace ./a.out
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
fstat64(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0
.
.
.
.
write(1, "Hello\n", 6Hello
) = 6
exit_group(6) = ?
$
Program 2:
#include<stdio.h>
void main()
{
char buf[2];
setbuf(stdout,buf);
printf("Hello\n");
}
Output:
$ strace ./a.out
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
fstat64(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0
.
.
.
.
write(1, "Hello\n", 6Hello
) = 6
exit_group(6) = ?
$
My requirement is to find the use of setbuf function. So, I experimented two programs which is showed above. In program 1, I didn't set the buffer to stdout. So, it uses the inbuilt buffer. The size of inbuilt buffer is 4096(Approximately). For that, Hello is enough for single write. So, write system call will be called only once.
But In program 2, I explicitly set the buffer with the size of 2 character. So, printf uses this buffer. So, my expected output is that the write system should be called for 3 to 4 times. But, the strace output for both of the programs are remain same.
So In program 2, the printf function uses the buffer(buf) are not. If it is used buf, then write system call will be called for 4 times. So, How to I check how many times my "a.out" program call write function.