ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
The following example is a pipe example. Please, see the question inside the code.
Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
void piping(char* ptr)
{
int status;
int pid[2];
int pipe_fd[2];
char *prog1_argv[4];
char *prog2_argv[2];
char* args[getNrOftokens(ptr, "|")];
//tokenizer( ptr, "|", args );
/* for int i = 0; i< ,.asd,sa.d,sd */
/* if( ptr[len-2] == ' ' ) */
/* ptr[len-2] = 0; */
/*
* Build argument list
*/
prog1_argv[0] = "ls";
prog1_argv[1] = "-lR";
prog1_argv[2] = "/";
prog1_argv[3] = NULL;
prog2_argv[0] = "more";
prog2_argv[1] = NULL;
/*
* Create the pipe
*/
if (pipe(pipe_fd) < 0)
perror ("pipe failed");
exit (errno);
}
/*
* Create a process space for the ls
*/
if ((pid[0]=fork()) < 0)
{
perror ("Fork failed");
exit(errno);
}
if (!pid[0])// Child
{
/*
* Set stdout to pipe
*/
printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);
close (pipe_fd[0]);// Close stdin
dup2 (pipe_fd[1], 1);<<<------------------ Is this really usefeul? I don't understand why.
printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);
close (pipe_fd[1]);
/* Execute the ls */
execute (prog1_argv[0], prog1_argv);
}
if (pid[0])//Parent
{
/*
* We're in the parent
*/
/*
* Create a process space for the more
*/
perror ("pipe failed");
exit (errno);
}
/*
* Create a process space for the ls
*/
if ((pid[0]=fork()) < 0)
{
perror ("Fork failed");
exit(errno);
}
if (!pid[0])// Child
{
/*
* Set stdout to pipe
*/
printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);
close (pipe_fd[0]);// Close stdin
dup2 (pipe_fd[1], 1);
printf("%d\t%d\n", pipe_fd[0], pipe_fd[1]);
close (pipe_fd[1]);
/* Execute the ls */
execute (prog1_argv[0], prog1_argv);
}
if (pid[0])//Parent
{
/*
* We're in the parent
*/
/*
* Create a process space for the more
*/
if ((pid[1]=fork()) < 0)
{
perror ("Fork failed");
exit(errno);
}
if (!pid[1])
{
/*
* We're in the child
*/
/*
* Set stdin to pipe
*/
close (pipe_fd[1]);
dup2 (pipe_fd[0], 0);
close (pipe_fd[0]);
/* Execute the more */
execute (prog1_argv[0], prog1_argv);
//execvp (prog2_argv[0], prog2_argv);
}
/* This is the parent */
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid (pid[1], &status, 0);
printf ("Done waiting for more.\n");
}
}
You need dup2 in cases where you want to pipe standard input and output between two programs as opposed to having everything taken from or sent to the terminal. dup can be very useful when opening a temporary stream on a file descriptor: when the stream is closed so is the file descriptor, so you need to duplicate it so the original isn't closed when the temporary stream is. dup2 is also useful for replacing a file descriptor at the process level. It allows you to change the underlying file for a file descriptor, allowing multiple threads, functions, and contexts to seamlessly write across multiple files.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.