LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2011, 03:25 AM   #1
karthikkm1987
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 12-19-2011, 08:39 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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...
 
Old 12-19-2011, 10:30 PM   #3
karthikkm1987
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-19-2011, 10:48 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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.
 
Old 12-19-2011, 11:06 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
Old 12-20-2011, 12:31 AM   #6
karthikkm1987
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-20-2011, 01:17 AM   #7
karthikkm1987
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
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.
 
Old 12-20-2011, 01:30 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
Old 12-20-2011, 01:36 AM   #9
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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.
 
Old 12-20-2011, 02:09 AM   #10
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by karthikkm1987 View Post
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.
 
Old 12-20-2011, 03:08 AM   #11
karthikkm1987
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
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?
 
Old 12-20-2011, 05:55 AM   #12
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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).
 
Old 12-20-2011, 09:54 PM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 12-20-2011 at 09:59 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Showing command output on the screen while simultaneously logging it. Lufbery Slackware 3 02-04-2010 02:18 PM
dmesg output sent to virtual terminals szim90 Linux - Newbie 3 12-10-2007 02:46 PM
dd 2 output files simultaneously, is it possible? kav Linux - Software 1 07-28-2007 01:39 PM
simultaneously sound output failure Andruha Linux - Hardware 2 03-22-2007 07:29 AM
Send output to seperate terminals? Brain Drop Programming 3 02-25-2005 10:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration