LinuxQuestions.org
Help answer threads with 0 replies.
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 02-23-2011, 09:53 AM   #1
longli
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Rep: Reputation: 0
How can I allow interleave output when I run commands concurrently


Hi, guys:


I am working on the shell using c language. I just would like to ask when I run some command in the backgrounds, how can I allow interleave out?

I want to the output like this:

shell> command1
shell> command2 &
shell> command3


This is the code to run the command:

pid=fork();

if (pid==0&&bg==0) // foreground
{


exec(command); // execute a command

exit(1);

}

else if (pid==0&&bg==1) // background
{

setpgid(pid,0);


exec(command); // execute a command

exit(1);
}

else if (bg!=1)
{
waitpid();

}




Thanks
 
Old 02-24-2011, 03:18 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
There's no easy way to control what gets written when you share output to your /dev/tty between multiple (essentially independent) processes.

One alternative is to make the processes work in lockstep (instead of in parallel). Not a good solution

Another is to create some kind of semaphore (either a bona fide "ipc semaphore", or some global alternative) to regulate who does a "printf()" and who waits. Again - not a good solution.

Another choice is to redirect stdout to a disk file (e.g. "my_child_prog > $$.txt"), and then have the "master" process read the individual files at its leisure.

'Hope that helps .. PSM
 
Old 02-25-2011, 03:35 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I've used advisory file locking with success. See man fcntl and look for F_GETLK.
Kevin Barry
 
Old 02-25-2011, 04:34 AM   #4
prowla
Member
 
Registered: Feb 2011
Location: UK
Distribution: RHEL 5 & 6, Ubuntu 10
Posts: 93

Rep: Reputation: 3
You could write something that opens a pipe reading from each command and does (pseudo code)

read(pipe1,s); print s;
read(pipe2,s); print s;
read(pipe3,s); print s;

Alternatively, if it's not for real-time, you could tag each line with a number and join them after.
 
  


Reply

Tags
[c]



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
Concurrently Running dd and md5sum Commands Woodsman Slackware 4 04-03-2009 05:15 PM
interleave and or fastpath question/issue rh-penguin Linux - Networking 1 12-27-2006 08:43 AM
Run 4-processes concurrently using fork() ugp Programming 1 02-26-2006 08:49 AM
Output commands when programs run? TKS Slackware 2 12-12-2005 11:07 PM
threads won't run concurrently slicht Programming 2 09-22-2004 09:01 AM

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

All times are GMT -5. The time now is 02:54 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