LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-21-2021, 06:51 PM   #1
hellraiserx9
LQ Newbie
 
Registered: Mar 2021
Posts: 6

Rep: Reputation: Disabled
using only one pipe for n processes (C programming)


I'm confused on how can i use only one pipe for connecting n processes.
 
Old 03-21-2021, 07:44 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by hellraiserx9 View Post
I'm confused on how can i use only one pipe for connecting n processes.
Welcome to the forums,

When you fork, you can continue to use the same handles, a.k.a. pipe.

Please consider reading a very old blog I wrote describing how to use pipes.

Using PIPES For Interprocess Communications

Otherwise suggest some general research or explanation as to what you do have in mind.
 
1 members found this post helpful.
Old 03-21-2021, 08:45 PM   #3
hellraiserx9
LQ Newbie
 
Registered: Mar 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
/deleted

Last edited by hellraiserx9; 03-22-2021 at 01:30 PM.
 
Old 03-21-2021, 08:46 PM   #4
hellraiserx9
LQ Newbie
 
Registered: Mar 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
Welcome to the forums,

When you fork, you can continue to use the same handles, a.k.a. pipe.

Please consider reading a very old blog I wrote describing how to use pipes.

Using PIPES For Interprocess Communications

Otherwise suggest some general research or explanation as to what you do have in mind.


Please see my code and tell me if there's any errors.
 
Old 03-21-2021, 09:51 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,270
Blog Entries: 24

Rep: Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208Reputation: 4208
Welcome to LQ and the Programming forum!

Please wrap your code and data snippets inside [CODE]...[/CODE] tags. Doing so will preserve indentation and provide other visual clues which make it easier for others to comprehend. You may write those yourself as shown, or use the # button available with Advanced edit options. (A complete list of BBCode tags is always available via a link near the bottom of every thread view).

Although everyone here is willing to help, it is generally preferred that you attempt to understand your problems first, then ask for specific help when you get stuck, providing any error messages you cannot resolve along a short description of what you have done so far to try to understand and fix it.

Asking others to evaluate and debug code without further explanation may not attract the most helpful answers.

That said, the blog post linked by rtmistler looks like an excellent place to start, and one I had not previously seen!

Again, welcome to LQ, and good luck!
 
1 members found this post helpful.
Old 03-22-2021, 02:22 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by hellraiserx9 View Post
... I have to do this using only one pipe. ...
Code:
const size_t cmds_count = argc - 1;

// creating pipes
int fd_pair[2];

for (size_t i = 0; i < cmds_count - 1; i ++)
    pipe(fd_pair);
that is altogether N calls to the function pipe

Otherwise yes, please use code tags to make your post readable.

Is this your homework? Where is this code coming from? Did you try to test it somehow?

Last edited by pan64; 03-22-2021 at 03:10 AM.
 
1 members found this post helpful.
Old 03-22-2021, 02:49 AM   #7
hellraiserx9
LQ Newbie
 
Registered: Mar 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
/deleted

Last edited by hellraiserx9; 03-22-2021 at 01:32 PM.
 
Old 03-22-2021, 03:13 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
I don't understand this code (better to say I don't think it can work), but probably you can explain...
 
Old 03-22-2021, 03:57 AM   #9
hellraiserx9
LQ Newbie
 
Registered: Mar 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
Ofcourse. I can, i have a process and splitting the process into child again and again for until it becomes n processes using fork and then using pipe to connect every process using only one pipe
 
Old 03-22-2021, 04:34 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
but why do you think this common pipe will do what you want?
 
Old 03-22-2021, 05:17 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,882
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@OP Could you please quote verbatim the text of your homework?
 
Old 03-22-2021, 06:21 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Have you used a debugger to follow the first process to confirm it does what you want, and the done the same with child #1?

How about have you printed out the pipe handles and/or the addresses of the descriptors, along with doing same for all the pids?

All of you've done is to ask a question for people to do that effort for you and provide you with the answer.

The code looks reasonable, not sure you need the dup's or to close descriptors unless you're making them all daemons. If it were me, I'd avoid any complications and use either debug method described above, but since they're there, then confirm they work properly, again using debug methods.
 
Old 03-22-2021, 08:05 AM   #13
hellraiserx9
LQ Newbie
 
Registered: Mar 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
Can someone suggest me some good article on pipes and forks?
 
Old 03-22-2021, 08:53 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by hellraiserx9 View Post
Can someone suggest me some good article on pipes and forks?
Apologies but we're not any more adept at searching the web than you.

Please review this link for further information.

EDIT: (wrong thread edit addition - removed)

Last edited by rtmistler; 03-22-2021 at 03:58 PM.
 
Old 03-22-2021, 09:30 AM   #15
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,920

Rep: Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038
Beej's guide to IPC is my goto
 
  


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
Using netcat to pipe stdout to pipe crabbit Linux - Networking 5 03-27-2018 12:06 PM
PHP shell_exec() always gives error "Broken pipe" when using pipe burek Linux - Server 1 01-19-2012 06:04 AM
pipe using no pipe soathana Programming 2 02-22-2003 04:33 PM

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

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