UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » General
  • » plink strange output buffer behavior

#1 2011-10-11 01:26 PM

xinglp
Member
Registered: 2008-04-13
Posts: 35

plink strange output buffer behavior

This is my example code, to print a line per second. With any parameters, it will print to stderr, otherwise to stdout.

#include <unistd.h>
#include <stdio.h>

int main(int argc,char* argv[])
{
	int      i;
	FILE*  f;

	f=argc>1?stderr:stdout;

	for(i=0;i<5;++i)
	{
		fprintf(f,"line %d\n",i);
		sleep(1);
	}
	return 0;
}

When I use " plink.exe [email protected] ./a.out " to execute it, it did not "print a line per second", but print all of the 5 lines after 5 seconds at once.
When use " plink.exe [email protected] ./a.out tostderr " to make it to print to stderr.  It works normal.

So I use  " plink.exe [email protected] strace ./a.out " to find out what happend.

rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0xbfeb37f4)           = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0xbfeb37f4)           = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0xbfeb37f4)           = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0xbfeb37f4)           = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({1, 0}, 0xbfeb37f4)           = 0
write(1, "line 0\nline 1\nline 2\nline 3\nline"..., 35) = 35
line 0
line 1
line 2
line 3
line 4
exit_group(0)                           = ?

There is only one "write(1, "line 0\nline 1\nline 2\nline 3\nline"..., 35) = 35" at last.

Then where am I wrong?

Thanks in advance

Offline

#2 2011-10-11 07:34 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: plink strange output buffer behavior

Offline

#3 2011-10-11 07:46 PM

xinglp
Member
Registered: 2008-04-13
Posts: 35

Re: plink strange output buffer behavior

ssh -t 127.0.0.1 ls /proc/self/fd/ -ln
total 0
lrwx------ 1 1000 1000 64 Oct 12 02:42 0 -> /dev/pts/2
lrwx------ 1 1000 1000 64 Oct 12 02:42 1 -> /dev/pts/2
lrwx------ 1 1000 1000 64 Oct 12 02:42 2 -> /dev/pts/2
lr-x------ 1 1000 1000 64 Oct 12 02:42 3 -> /proc/630/fd

ssh 127.0.0.1 ls /proc/self/fd/ -ln
total 0
lr-x------ 1 1000 1000 64 Oct 12 02:44 0 -> pipe:[764]
l-wx------ 1 1000 1000 64 Oct 12 02:44 1 -> pipe:[765]
l-wx------ 1 1000 1000 64 Oct 12 02:44 2 -> pipe:[766]
lr-x------ 1 1000 1000 64 Oct 12 02:44 3 -> /proc/637/fd

plink [email protected] ls /proc/self/fd -ln
total 0
lr-x------ 1 1000 1000 64 Oct 12 02:45 0 -> pipe:[797]
l-wx------ 1 1000 1000 64 Oct 12 02:45 1 -> pipe:[798]
l-wx------ 1 1000 1000 64 Oct 12 02:45 2 -> pipe:[799]
lr-x------ 1 1000 1000 64 Oct 12 02:45 3 -> /proc/661/fd

plink -t [email protected] ls /proc/self/fd -ln
total 0
lrwx------ 1 1000 1000 64 Oct 12 02:49 0 -> /dev/pts/2
lrwx------ 1 1000 1000 64 Oct 12 02:49 1 -> /dev/pts/2
lrwx------ 1 1000 1000 64 Oct 12 02:49 2 -> /dev/pts/2
lr-x------ 1 1000 1000 64 Oct 12 02:49 3 -> /proc/667/fd

Last edited by xinglp (2011-10-11 07:50 PM)

Offline

#4 2011-10-11 07:53 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: plink strange output buffer behavior

Offline

#5 2011-10-11 08:51 PM

xinglp
Member
Registered: 2008-04-13
Posts: 35

Re: plink strange output buffer behavior

Last edited by xinglp (2011-10-11 09:48 PM)

Offline

#6 2011-10-12 01:20 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: plink strange output buffer behavior

Offline

#7 2011-10-12 01:33 PM

xinglp
Member
Registered: 2008-04-13
Posts: 35

Re: plink strange output buffer behavior

Thanks, using "-t" to get a real tty is the best choice for me.

Offline

  • Index
  • » General
  • » plink strange output buffer behavior

Board footer

Powered by FluxBB