LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 05-28-2012, 05:01 AM   #1
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Rep: Reputation: Disabled
IPC using pipes


Hey I was going through this code for IPC. The program creates three processes, in parent we create a pipe, in process A we take a string input and finally in process B we reverse that string.
I am stuck on the dup2 part. I know that dup2 is redirecting input and output to p_des[0] and p_des[1]. but I am not able to understand that how the rev_str part is using the same string taken input by get_str part.

************PARENT PROCESS**********************
#include <stdio.h>
#include <ctype.h>
main()
{
int p_des[2];
pipe( p_des ); /* The pipe descriptor */
printf("Input a string \n");
if ( fork () == 0 )
{
dup2(p_des[1], 1);
close(p_des[0]); /* process-A closing read end of the pipe */
execlp("./get_str", "get_str", 0);
/*** exit(1); ***/
}
else
if ( fork () == 0 )
{
dup2(p_des[0], 0);
close(p_des[1]); /* process-B closing write end of the pipe */
execlp("./rev_str", "rev_str", 0);
/*** exit(1); ****/
}
else
{
close(p_des[1]); /* parent closing both the ends of pipe */
close(p_des[0]);
wait(0);
wait(0);
}
fflush(stdout);
}

****PROCESS A*****************
get_str.c
#include <stdio.h>
#include <ctype.h>
void get_str(str)
char str[];
{
char c;
int ic;
c = getchar();
ic = 0;
while ( ic < 10 && ( c != EOF && c != '\n' && c != '\t' ))
{
str[ic] = c;
c = getchar();
ic++;
}
str[ic] = '\0';
return;
}

************PROCESS B*******************
rev_str.c
void rev_str(str1, str2)
char str1[];
char str2[];
{
char c;
int ic;
int rc;
ic = 0;
c = str1[0];
while( ic < 10 && (c != EOF && c != '\0' && c != '\n') )
{
ic++;
c = str1[ic];
}
str2[ic] = '\0';
rc = ic - 1;
ic = 0;
while (rc-ic > -1)
{
str2[rc-ic] = str1[ic];
ic++;
}
return;
}
 
Old 05-29-2012, 10:42 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,106

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
without the source code to dup2() it is hard to guess at what this program is doing. also execlp() executes programs but there are no main () entry points for get_str.c or rev_str.c.
 
Old 05-29-2012, 04:14 PM   #3
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
dup2() copies the file descriptor. In this case it is copying the file descriptor of stdout in p_des[1] and stdin in p_des[0].

get_str and rev_str are separate C programs lying in same directory.
 
Old 05-29-2012, 05:46 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
I would recommend using the Report button to get this moved to the Programming Forum
 
Old 05-29-2012, 06:38 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,106

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
Quote:
Originally Posted by vikasbansal27 View Post
dup2() copies the file descriptor. In this case it is copying the file descriptor of stdout in p_des[1] and stdin in p_des[0].

get_str and rev_str are separate C programs lying in same directory.
you never put anything in p_des[]. you should ask to move this to the programming subforum.
 
Old 05-30-2012, 02:51 AM   #6
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hello


I have moved this to http://www.linuxquestions.org/questi...75#post4690975
please help me solve this.
 
Old 05-30-2012, 12:26 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by vikasbansal27 View Post
Hello


I have moved this to http://www.linuxquestions.org/questi...75#post4690975
please help me solve this.
The correct way would be reporting your own thread and ask for moving, not disseminating duplicates across different forums. You can use the REPORT button in the lower-right corner of your own post, compile the form with your request (please and thanks are appreciated) and submit. It will be received by a moderator and managed accordingly.

This thread is going to be closed since it is a duplicate of http://www.linuxquestions.org/questi...1/#post4690975 now.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
IPC vishnu anand Linux - Newbie 4 12-16-2011 07:48 AM
Please help a problem in client-server ipc message 2 pipes communication ouou Programming 1 06-09-2011 03:26 PM
Shared Memory vs. Pipes for IPC ta0kira Programming 11 12-23-2007 03:56 PM
Ipc gautam143 Programming 1 09-25-2007 04:50 PM
about IPC iclinux Programming 1 01-14-2005 11:16 PM


All times are GMT -5. The time now is 02:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration