LinuxQuestions.org
Review your favorite Linux distribution.
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 05-30-2012, 02:40 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-30-2012, 02:50 AM   #2
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
int dup2(int fildes, int fildes2);
In dup2(), the value of the new descriptor fildes2 is specified. If this descriptor is already in use, the descriptor is first deallocated as if a close(2) call had been done first.
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.
In the program given above, we spawn a two child processes and populate their code segment with the programs get_str.c and rev_str.c .
 
Old 05-30-2012, 02:53 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
please use [code][/code] to keep formatting.
dup will not redirect, but duplicate file descriptor.
process A will read the pipe by getchar and process B should send the string into the pipe, but I cannot find that part of the code (probably a putchar)
 
Old 05-30-2012, 02:57 AM   #4
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Process A is not reading the pipe...we have closed the reading end of pipe in
{
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); ***/
}
 
Old 05-30-2012, 03:18 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
ok, in process B there is no code to read or write the pipe or stdin/stdout.
in process A you have getchar which will read stdin, but actually stdout is duped (as you wrote), so there is no connection between A and B. Also there is no main (for A and B) posted, so something is missing....
 
1 members found this post helpful.
Old 05-30-2012, 04:55 AM   #6
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Solved: In process A after getting input through getchar we can printf the same so that it gets duped and Process B can easily read it thru stdin.
 
  


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
IPC using pipes vikasbansal27 Linux - Newbie 6 05-30-2012 12:26 PM
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

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

All times are GMT -5. The time now is 01:45 AM.

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