unbuffered streams.


 
Thread Tools Search this Thread
Top Forums Programming unbuffered streams.
# 1  
Old 07-27-2005
unbuffered streams.

#include "../ourhdr.h"

int main(void)
{
int c;
char *buf;
setvbuf(stdin,buf,_IONBF,10);
setvbuf(stdout,buf,_IONBF,10);
while((c=getc(stdin)) != EOF)
{
if(putc(c,stdout) == EOF)
err_sys("output error");
}
if (ferror(stdin))
err_sys("input error");
exit(0);
}


for the above program i expected following output.
$> ./a.out
hh
(user input is in black colour and output is indicated in blue colour)
but the output is coming after pressing return key

h<return-key>
h

see in the above code i made stdin and stdout as unbuffered streams..

so can anyone explain why the output is coming after pressing return key..
# 2  
Old 07-27-2005
You have to set the tty to canonical ---

This reads one key at a time without requiring the <return>
Code:
int getch(void) {
      int c=0;

      struct termios org_opts, new_opts;
      int res=0;
          //-----  store old settings -----------
      res=tcgetattr(STDIN_FILENO, &org_opts);
      assert(res==0);
          //---- set new terminal parms --------
      memcpy(&new_opts, &org_opts, sizeof(new_opts));
      new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
      tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
      c=getchar();
          //------  restore old settings ---------
      res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
      assert(res==0);
      return(c);
}

# 3  
Old 01-02-2009
I too was suffering from this problem, and I hit another snag. When I tried this sample code it still wouldn't work form me, until I entered a total of 4 characters or hit return 3 times after having entered my first desired character.

After a lot of digging, I found that there is a minimum value setting in the termios structure that dictates the minimum number of characters that must be read before it passes the data to you.

The default is suppose to be 1, but in my case it was set to 4. How? I don't know, but it is.

I resolved this problem by adding the following line:
Code:
    new_opts.c_cc[VMIN]=1;

before the line:
Code:
    tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);

I found this information from the termio man page, look for MIN. There are also other values such as TIME in the event that you to set a timeout -- but I leave that for additional reading.

I just wanted to point out that this was 99% perfect for me, and this 1% was all that I lacked.

This may help someone else too.

-=John
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK Too many open streams to print/printf

hallow all i need your advice about this script i have script like this: INDEX=/zpool1/NFS/INDEX/${1} SCRIPT=/zpool1/NFS/script/${1} LIST=SAMPLE cd ${SCRIPT} for i in `cat ${LIST}` do GETDATE=`echo ${i}|awk '{print substr($1,9,8)}'` /usr/xpg4/bin/awk -F ":" '{close(f);f=$4}{print >>... (4 Replies)
Discussion started by: zvtral
4 Replies

2. Homework & Coursework Questions

grep Simulation Using Only Unbuffered I/O - C

1. The problem statement, all variables and given/known data: Basically, I'm trying to search for a particular string pattern within a text file and print "textfile: line_no line". I need to read this in line at a time, but I'm restricted to using unbuffered I/O functions. I'm not sure if I'm... (3 Replies)
Discussion started by: jdkirby
3 Replies

3. Programming

Why must flush all line-buffered output streams?

Hi, Mentioned in Stevens & Rago "Advanced Programming in the UNIX" I don't understand why must flush all line-buffered output streams when (a)an unbuffered or (b)a line-buffered stream require data from kernel? (2 Replies)
Discussion started by: Edward114
2 Replies

4. AIX

AIX STREAMS driver question

Hi all, I have a AIX kernel STREAMS question need your help, I need to implement a firewall on AIX and get packet raw data then decide pass or drop it, I've seen similiar firewall code on HP-UX, on HP-UX, you have to implement a "dlpi STREAMS driver", and specify it as a "dlpi" driver in... (1 Reply)
Discussion started by: rocktilldie
1 Replies

5. Shell Programming and Scripting

Problem with pipes on infinite streams

Here is an example code that shows the issue I have: #!/bin/bash counter() { seq 1000 | while read NUM; do echo $NUM echo "debug: $NUM" >&2 sleep 0.1 # slow it down so we know when this loop really ends done } counter | grep --line-buffered "" | head -n1 ... (10 Replies)
Discussion started by: tokland
10 Replies

6. SCO

WARNING: No Memory for Streams (NSTRPAGES)

Anybody have a clue what might have caused the Failures under Class 6? I did a reboot and so far so good, I had been up for about 55 days prior to the reboot. I'm running SCO_SV rel 3.2v5.0.7. Steve #netstat -m streams... (1 Reply)
Discussion started by: Steve_93630
1 Replies

7. Shell Programming and Scripting

Help capturing and reformatting buffered and unbuffered output

Ok, so I have a shell script which runs a specific command, and that command sends it's output to the display. At certain times, the command sends buffered output, and at other times, the command sends unbuffered output in the form of a % progress bar, so if I run the command, the output I receive... (0 Replies)
Discussion started by: vikingshelmut
0 Replies

8. UNIX for Advanced & Expert Users

Transparent ioctls Streams calls

What are transparent ioctls messages and when and why we have to issue copyin or copyout kernel utilities with respect to ioctls calls to a Stream. (2 Replies)
Discussion started by: S.P.Prasad
2 Replies

9. UNIX for Dummies Questions & Answers

STREAMS

Hi Everyone I am building some A Class HP boxes as web proxy servers, have just installed HP-UX 11.00 and am starting to configure one according to our standard build policy. However on the A Class I just happened to place the software depot this error message keeps popping up every 2 mins: ... (1 Reply)
Discussion started by: alwayslearningunix
1 Replies
Login or Register to Ask a Question