LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting the output seperately in 2 terminals simultaneously (https://www.linuxquestions.org/questions/programming-9/getting-the-output-seperately-in-2-terminals-simultaneously-919440/)

karthikkm1987 12-19-2011 03:25 AM

Getting the output seperately in 2 terminals simultaneously
 
Hai I have written a c program
test.c

Code:

#include<stdio.h>
main()
{
int i,j;
for(i=1,j=10;i,j;i++,j++)
{

printf("%d\n",i);
printf("%d\n",j);

sleep(1);
}
}

the output comes like this and it is for infinite loop
./testexe
1
10
2
11
3
12
4
13
5
14
6
15
7
16
...................so on;

after that I am putting the output into a file by executing shell script


Code:

#!/bin/bash
./testexe>file1


now I need to get the values of i in one terminal like

1
2
3
4
.........so on;


and I need to get values of j to be put in another terminal like

10
11
12
13
14
...........so on;


Please help me get this.(so total of 3 terminals are opened)



I tried writing script for getting i values


Code:

#!/bin/bash
echo "Executing the program"
i=1
for(( ; ; ))
do
i=$i+1
awk 'NR==$i {print}' file1
done

but i am not getting any output.

Cedrik 12-19-2011 08:39 AM

In one terminal you could tail the odd lines from file1, with something like:
Code:

i=0
tail -f file1 | while read line;do
  if (($i % 2)); then
    echo $line
  fi
  let i++;
done

And do the same in other terminal for even lines with if (($i % 2 == 0))

Surelly there are clever ways...

karthikkm1987 12-19-2011 10:30 PM

Mr.Cedric I tried everything.
Have you seen the c code?
It is in infinite loop.
I am not getting the data in the other two terminals.

firstfire 12-19-2011 10:48 PM

Hi.
You do not see any output because of buffering. Try adding fflush(stdout); line:
Code:

#include<stdio.h>
main()
{
        int i,j;
        for(i=1,j=10;i,j;i++,j++)
        {

                printf("%d\n",i);
                printf("%d\n",j);
                fflush(stdout);

                sleep(1);
        }
}

This should solve the issue.

NevemTeve 12-19-2011 11:06 PM

Dear OP, it would be great if you simply told us what do you really want to achieve with your program. I suppose it has something to do with multiple output-files (terminals, you said), but the details aren't clear.

karthikkm1987 12-20-2011 12:31 AM

The thing is it is a real time application i am designing for my college project.It is a modbus program where in my arm board can run only one program.So i am getting all the related data to my PC when i execute the program.
Here when you get the output of a program which is executed in one terminal.So I need to know whether it is possible to seperate the datas which i am without changing my Modbus program and displaying it on seperate terminals so that all the adc values are displayed in one terminal and all gas sensor values in other terminal.One more thing it should be seperate processes which are running.

karthikkm1987 12-20-2011 01:17 AM

Quote:

Originally Posted by firstfire (Post 4554193)
Hi.
You do not see any output because of buffering. Try adding fflush(stdout); line:
Code:

#include<stdio.h>
main()
{
        int i,j;
        for(i=1,j=10;i,j;i++,j++)
        {

                printf("%d\n",i);
                printf("%d\n",j);
                fflush(stdout);

                sleep(1);
        }
}

This should solve the issue.

Hi is there fflush(stdout) kind of command or anything in shell scripting???
Please help.

NevemTeve 12-20-2011 01:30 AM

The best solution would be prefixing the messages, eg:

Code:

LOG: mesurement started
THERM1: 123 F
THERM2: 134 F
PRESS1: 771 Hgmm
LOG: power off

In this case you could separate the lines with awk easily.

gnashley 12-20-2011 01:36 AM

You could send one part (either i or j) to stderr(or some other file descriptor) instead of stdout. Then you'd be able to redirect the output from the separate fd's directly from the shell.

firstfire 12-20-2011 02:09 AM

Quote:

Originally Posted by karthikkm1987 (Post 4554241)
Hi is there fflush(stdout) kind of command or anything in shell scripting???
Please help.

Take a look at this discussion. `man stdbuf' may also be useful.

karthikkm1987 12-20-2011 03:08 AM

Quote:

Originally Posted by firstfire (Post 4554270)
Take a look at this discussion. `man stdbuf' may also be useful.

Hi I looked into your post and can you be more descriptive,I didnt understand anything.Can you give me an example?

firstfire 12-20-2011 05:55 AM

Consider your first program without 'fflush(stdout)':
Code:

$ cat test.c
#include<stdio.h>
main()
{
        int i,j;
        for(i=1,j=10;i,j;i++,j++)
        {

                printf("%d\n",i);
                printf("%d\n",j);
                //fflush(stdout);
                sleep(1);
        }
}
$ gcc test.c
$ stdbuf -oL ./a.out > /tmp/file1

Run `tail -f /tmp/file1' in the second terminal and see what happens.

`stdbuf' forces your program to flush output on each newline character (`-oL' option means that output stream should be line buffered) as if you uncommented fflush() line. stdbuf only works if program uses FILE* streams (I think).

theNbomr 12-20-2011 09:54 PM

If you truly want to send data to multiple terminals, you will have to do quite a bit of work. Most (if not all) processes have at most one controlling terminal. Similarly, most (if not all) terminals are mapped uniquely to their owner process. To put text into the terminal space of another process generally requires the cooperation of that other process. This is the premise of Interprocess Communications (IPC). A very nice primer on IPC is Beej's Guide to IPC. If this is really what you're trying to do, then I think you probably want to use message queues, as that models the producer/consumer paradigm quite well.

After having said all of that, I'm still not convinced that your question isn't actually asking something different from what you actually want. Please confirm what your vision of a 'terminal' is. If you really truly want to use terminals, you might find it easiest to use the GNU screen utility. It allows you to write to an open screen session from a shell command, using the 'stuff' option.

On re-reading your requirements, I will hazard a guess that you have a program running on a single board computer, that reads data from a modbus PLC. You want to demultiplex the modbus data, and forward it to three separate applications running concurrently on some other host. For that, it would seem to indicate that you need three clients listening on three TCP ports, and the SBC application sends the data to one of the three ports depending on its nature. Does this sound something like what you're attempting.

--- rod.


All times are GMT -5. The time now is 02:55 PM.