Getting piped input from a program that's buffering it's stdout
The old buffering problem again, in a very specific case. On FreeBSD this time, but it's the generic line-buffered vs fully-buffered problem.
I'm trying to pick an available bluetooth speaker (all named audio_N), by pinging all of them en mass and taking the first to respond.
The pinger-utility holds it's output, when it is going into a pipe, so there's a delay before I get the first response.
Can anyone think of a generic solution to make things snappy? I'm not so irritated by the delay, that it would warrant a custom C-program or a patch into l2ping. I have heard of something called expect, but have not used it ever.
Juha
Edit: I didn't think it through. I'll just launch a player to each of them, and one or zero of those succeeds. Meh...
$1 for the script is audio_. in my case.
Last edited by Juha Nurmela; 10-04-2016 at 01:43 PM..
Nothing outside the program can induce it to print its output early. Some languages like python respond to a particular environment variable which forces them to not print buffered.
I had a peek into the expect, and it does look like there would a mechanism to run the children in a pty, to seduce line-buffering. Very long manpage though.
I had a peek into the expect, and it does look like there would a mechanism to run the children in a pty, to reduce line-buffering.
Some programs are smart enough to turn off buffering when talking to a terminal or pipe but nothing forces them to. Its a choice the program makes - "I am talking to a terminal, therefore, turn off the internal buffer". It's not something chosen from the outside. It's not even anything system at all. It's a choice between calling write() once, to write blocks of several thousand bytes at a time, or calling write() many times, one for each line. It's a difference in a program's logic.
In short, still entirely up to the program and can't be forced from the outside by any means if the program doesn't already support it somehow.
expect won't help. I've looked through l2ping.c and it prints line-buffered anyway, it's not what's holding you up.
I don't think this construct:
...necessarily does what you think. Background commands are liable to freeze themselves when they try to write to the terminal. Make sure they have absolutely no connection to the terminal:
In particular, sed may be making you wait, too. Make sure you run sed -u.
Doing the explicit sleep actually works really snappy. Immediate response!
Argument has to be -fc1, else the program makes an extra sleep after each ping.
Suppose I have a program that I've written that accepts input, ie this C++ program:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter something:" << endl;
int x;
cin >> x;
cout << "You entered data" << endl;
}
Suppose that I have a text file,... (5 Replies)
Hi,
Here is my question:
I want a shell script which I name as 'del', and can be used as del(string).
when run del(string), it will delete several directories at different locations in my system,like:
rm -fr /lustre/fs/scratch/user/$string
rm -fr /home/user/$string
rm -fr... (4 Replies)
Hi
I am new to writing script and want to use a Bash Piped while-read and read from user input.
if something happens on server.log then do while loop or if something happend on user input then do while loop.
Pseudocode something like:
tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Hi,
I have 1 program that writes in to the STDIN of another program as shown below. Both programs contain 4 or 5 lines & would terminate in under a second.
$ driver.exe | program.exe
How is that I can attach the debugger (gdb) to program.exe ? so that I can step through and see what all... (0 Replies)
Can anyone tell me why does'nt the following command work?
find /gfp1/home/arijit -name "*.sas7bdat" | gzip
I am trying to compress all files with extension sas7bdat with gzip.
It gives error message
gzip: compressed data not written to a terminal. Use -f to force compression.... (5 Replies)
I have an Shell script which has few global variables
eg :
range=100;
echo "$range"
I want to use the same variable in my C++ program for example
int main()
{ cout << range << "\n"; }
i tried using this
int main(int argc, char *argv)
{ cout << range << "\n"; }
but... (5 Replies)
Using ls input as example..
ls | sed 's/\n/ /'outputs with line breaks, where I was expecting the \n to disappear. I've tried \r as well wondering if terminal output used different breaks.
Is there a way to remove the line breaks without saving to file and then working from there?
----------... (2 Replies)
Hi,
Program A: uses pipe()
I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using:
* child
-> dup2(fd, STDOUT_FILENO);
-> execl("/path/PROGRAM B", "PROGRAM B", NULL);
* parent
-> char line;
-> read(fd, line, 100);
Question:... (2 Replies)
Hello!
Will someone out there pls help in clearifying what is really wrong with my system. I use window 98 as my operating system. I am connected to a proxy server for browsing the net. Whenever l am listening to music online l have the problem of intermitted break in which the playing will... (10 Replies)
I have a project where I have to use bzcat to uncompress a file and use that output as the data to run another program on.
I understand that you would do (bzcat filename.bz2 ! program name) but then how do you access that data in the c program??? Please help thanks (2 Replies)