Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.