Pipes & exec & C - c

Hi i have the following problem , I have to create a program that performs these Linux commands ls –la | sort | wc –l but in my code i just can read two of this command , can sameone help me with this??
int main(){
pid_t pids[3];
int dados[2],dados2[2],i;
if(pipe(dados) == -1 && pipe(dados2) == -1){
perror("pipe failed");
exit(1);
}
for(i=0;i<3;i++){
pids[i] = fork();
if(pids[i] == 0){
if(i==0){
close(dados[0]);
dup2(dados[1],1);
close(dados[1]);
execlp("ls","ls","-la",NULL);
perror("exec failed");
exit(-1);
}
if(i==1){
close(dados[1]);
dup2(dados[0],0);
close(dados[0]);
close(dados2[0]);
dup2(dados2[1],1);
close(dados2[1]);
execlp("sort","sort",NULL);
perror("exec failed");
exit(-1);
}else{
close(dados2[1]);
dup2(dados2[0],0);
close(dados2[0]);
printf("aaaaaaaaaaaaaaaaaa");
execlp("wc","wc","-l",NULL);
perror("exec failed");
exit(-1);
}
}
}
/* Pai tem de fechar a sua copia da extremidade de escrita
para a leitura do sort desbloquear */
close(dados[1]);
close(dados2[0]);
for(i=0;i<3;i++){
wait(NULL);
}
return 0;
}
i dont understand what miss in this

What catches my eye immediatly is
-if(pipe(dados) == -1 && pipe(dados2) == -1){
+if(pipe(dados) == -1 || pipe(dados2) == -1){
perror("pipe failed");
exit(1);
}
The parent pid just needs to read the output of the last command, so the parent neeeds to close all other pipe ends. The child processes needs to close stdin/stdout and dup2() the pipe descriptors to bind them to stdin/stdout and then exec(). With pipes it is really important that you close the not needed pipe end in your process. See http://linux.die.net/man/2/pipe
Currently i dont have an linux box beneith me, so i cant write and test linux code.
My summary for you is:
- parent reads from the last pipe (stdout of the last process in the chain)
- all other childs need to close() their stdin/stdout, dup2() the right pipeend, close all unneeded pipes (remember to close the dup2() source too, because it is a fd) and then exec.
Here is an example how i did create a pipe and rebind the fds.
// unset FD_CLOEXEC
set_close_on_exec(to_child,false);
set_close_on_exec(from_child, false);
// close and rebind the stdin/stdout/stderr
// dup the fd and recreate stdin/stdout with fd as the target
if (dup2(to_child, STDIN_FILENO) != 0 || dup2(from_child, STDOUT_FILENO) != 1) {
shared::perror("error duplicating socket for stdin/stdout");
exit(EXIT_FAILURE);
}
// close these FDs
close(to_child);
close(from_child);
// now we can exec the new sub process and talk to it through
// stdin/stdout/stderr
execlp(exe.c_str(), exe.c_str(), argv.c_str(), (char*)0);
// this should never be reached
shared::perror("error: executing the binary: " + exe);
exit(EXIT_FAILURE);
Remember that at pipe(int fds[2]) the index 1 is the write end of the pipe and index 0 is the read end of the pipe. So you need
Regards
Georg
Edit: I recommend you the book "The Linux Programming Interface: A Linux and UNIX System Programming Handbook" ISBN-13: 978-1593272203

Related

piping for a simple shell in c

I'm trying to implement piping in a simple shell program that I'm writing in C.
But for some reason, I'm not getting output when I try to run ls | wc -l.
I'm really not sure why this is happening since I'm basically putting the child process's output to pipe[1] which does the command before pipe indicator and I'm putting parent's input to pipe[0] which does the command after pipe indicator and it should be printing to the terminal since the output of parent's never been changed, my approach right now is if piping is flagged the call fork in child and do the piping.
code below
int pipe1[2];
int pipepid;
int piping; /*flag for piping*/
int pipeposition;/*index of pipe indicator*/
//* code... */
if(pipe(pipe1)!= 0){
perror("pipe");
exit(1);
};
/* split commands to before pipe indicator and after */
for(int p = 0;p<pipeposition;p++){
argsbefore[p]=args[p];
}
/* after */
int e=0;
for(int h = pipeposition+1; h<cnt;h++){
argsafter[e]=args[h];
e++;
}
/* code ... */
if(piping){
pipepid = fork();
if(pid == 0){
/* do child */
if(dup2(pipe1[1],1)==-1){
perror("dup2 child");
exit(1);
}
close(pipe1[1]);
if (execvp(argsbefore[0], argsbefore) < 0) {
printf("exec failed\n");
exit(1);
}
exit(0);
}/* else if error */
else if(pid == -1){
printf("ERROR: fork failed\n");
exit(1);
}/* parent */
else{
if(dup2(pipe1[0],0)==-1){
perror("dup2 parent");
exit(1);
}
close(pipe1[0]);
if (execvp(argsafter[0], argsafter) < 0) {
printf("exec failed\n");
exit(1);
}
}
}
you seem to be doing that on a unix-like system. If you're lucky, your system might have a tool that reports every system call your program perform (strace -f my_program my_ar gu_ments would do that on Linux).
That would give you a list of what process did what and when, and whether there have been error code for some operations. That usually helps a lot with these multi-process setups.
It turns out I didn't close all the pipes so the second command wasn't able to finish, after putting close for both ends in the main parent process it was fixed

Trying to re-implement "|" pipe operator in C

I'm trying to re-implement The pipe "|" operator.
The program will be executed as follows:
$ ./exe infile cmd cmd2 cmd3 cmdn outfile.
where at first i'm gonna read from infile process infile's data through the commands and finally pipe it to outfile.
IMPLEMENTAIONS:
My pseudo code looks like the following:
change infile descriptor to stdin.
loop each command.
pipe();
fork();
if (we're in the child process)
change stdin to read_end of pipe.
execute command.
else if (we're in the parent process)
change stdout to write_end of pipe.
execute command.
wait for child process.
change outfile descriptor to stdout.
CODE:
int infile_fd = open(args->infile, O_RDONLY);
dup2(infile_fd, STDIN_FILENO);
// how many commands
while(i < args->len)
{
// error handling stuff
args->cmds[i].cmd = check_exist(args->cmds[i], args);
if (pipe(args->cmds[i].fd) == -1)
{
perror("piping failed\n");
exit(EXIT_FAILURE);
}
if ((args->cmds[i].pid = fork()) == -1)
{
perror("fork failed\n");
exit(EXIT_FAILURE);
} else {
// child process
if (args->cmds[i].pid == 0)
{
close(args->cmds[i].fd[WRITE_END]);
dup2(args->cmds[i].fd[READ_END], STDIN_FILENO);
if (execve(args->cmds[i].cmd, args->cmds[i].flags, env) == -1)
{
perror("execve failed\n");
exit(EXIT_FAILURE);
}
i++;
}
else
{
// parent process
close(args->cmds[i].fd[READ_END]);
dup2(args->cmds[i].fd[WRITE_END], STDOUT_FILENO);
if (execve(args->cmds[i].cmd, args->cmds[i].flags, env) == -1)
{
perror("execve failed\n");
exit(EXIT_FAILURE);
}
wait(NULL);
i++;
}
}
}
int outfile_fd = open(args->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
// change stdout to outfile
dup2(outfile_fd, STDOUT_FILENO);
OUTPUT:
output is garbage, commands read data off the stack and start showing env variables.
EXPECTED OUTPUT:
data first being read from "infile" and then passed through commands until the end of the piping channel at "outfile".
It would be silly to ask what am I doing wrong, because I'm probably doing it all wrong, so a better question: How can I do it right?

Pipe: Closing file descriptors in an array of pipe

I am learning Linux and piping that kind stuff for my system programming course right now, I am having a hard time understanding closing file descriptors in an array of pipes now.
// write the code to loop over the command line arguments (remember to skip the executable name)
for (int i = 1; i < argc; i++) {
// call pipe before we fork
if ((pipe(pipe_fd[i-1])) == -1) {
perror("pipe");
exit(1);
}
// call fork
int result = fork();
if (result < 0) { // case: a system call error
// handle the error
perror("fork");
exit(1);
} else if (result == 0) { // case: a child process
// child does their work here
// child only writes to the pipe so close reading end
if (close(pipe_fd[i-1][0]) == -1) {
perror("close reading end from inside child");
exit(1);
}
// before we forked the parent had open the reading ends to
// all previously forked children -- so close those
int child_no;
for (child_no = 0; child_no < i-1; child_no++) {
if (close(pipe_fd[child_no][0]) == -1) {
perror("close reading ends of previously forked children");
exit(1);
}
}
int len = strlen(argv[i]);
// write len to the pipe as an integer
if (write(pipe_fd[i-1][1], &len, sizeof(int)) != sizeof(int)) {
perror("write from child to pipe");
exit(1);
}
// I'm done with the pipe so close it
if (close(pipe_fd[i-1][1]) == -1) {
perror("close pipe after writing");
exit(1);
}
// exit so I don't fork my own children on next loop iteration
exit(0);
} else {
// in the parent but before doing the next loop iteration
// close the end of the pipe that I don't want open
if (close(pipe_fd[i-1][1]) == -1) {
perror("close writing end of pipe in parent");
exit(1);
}
}
}
I will give a list of what I understand right now:
I understand parent and child process need to close those fds that they don't need to use, in this case child is writing to parent, so parent needs to close writing port and child needs to close reading port.
I understand file descriptors are shared among parent process and children process.
The above code is given from my lecture slide, I feel confused by one thing specifically.
In the loop, I observe that each child is closing its reading port once this child is created by fork, and the code that does this action is:
else if (result == 0) { // case: a child process
// child does their work here
// child only writes to the pipe so close reading end
if (close(pipe_fd[i-1][0]) == -1) {
perror("close reading end from inside child");
exit(1);
}
From what I understand at this point is that, each child is going to close its own reading port after being given birth by fork, and I think the latter children created SHOULD NOT worry about closing previous children's reading port.
But my understanding seems not correct after I read this code:
// before we forked the parent had open the reading ends to
// all previously forked children -- so close those
int child_no;
for (child_no = 0; child_no < i-1; child_no++) {
if (close(pipe_fd[child_no][0]) == -1) {
perror("close reading ends of previously forked children");
exit(1);
}
}
I don't understand why the latter children should go to close previous children's reading port, aren't those reading port already be closed once those children are created?
Thanks for helping me out. :)
A descriptor isn't really closed until all processes that have it open close it. Since each child inherits all the pipe descriptors from the previous process, they should close all the ones they're not using.
The main reason to close reading ports is so that the writing process will get an error or signal if it tries to write to the pipe after the reader has exited. If the other children kept all the reading ports opened, this wouldn't happen until all subsequent children exit.

Implementing shell in C and need help handling input/output redirection

Round 2
After reading some of the answers, my revised code is:
int pid = fork();
if (pid == -1) {
perror("fork");
} else if (pid == 0) {
if (in) { //if '<' char was found in string inputted by user
int fd0 = open(input, O_RDONLY, 0);
dup2(fd0, STDIN_FILENO);
close(fd0);
in = 0;
}
if (out) { //if '>' was found in string inputted by user
int fd1 = creat(output, 0644);
dup2(fd1, STDOUT_FILENO);
close(fd1);
out = 0;
}
execvp(res[0], res);
perror("execvp");
_exit(1);
} else {
waitpid(pid, 0, 0);
free(res);
}
It works, but seems the standard output isn't being reconnected or something to that effect. Here is execution:
SHELL$ cat > file
hello, world
this is a test
SHELL$ cat < file //no output
SHELL$ ls //no output
'<' and '>' both work, but after they are executed there is no output.
Round 1
I have been working on a relatively simple shell in C for a while now, but I am having trouble implementing input (<) and output (>) redirection. Help me find the issues in the following code:
int fd;
int pid = fork();
int current_out;
if (in) { //if '<' char was found in string inputted by user
fd = open(input, O_RDONLY, 0);
dup2(fd, STDIN_FILENO);
in = 0;
current_out = dup(0);
}
if (out) { //if '>' was found in string inputted by user
fd = creat(output, 0644);
dup2(fd, STDOUT_FILENO);
out = 0;
current_out = dup(1);
}
if (pid == -1) {
perror("fork");
} else if (pid == 0) {
execvp(res[0], res);
perror("execvp");
_exit(1);
} else {
waitpid(pid, 0, 0);
dup2(current_out, 1);
free(res);
}
I may have some unnecessary material in there because I have been trying different things to get it to work. I am not sure what is going wrong.
You have way too many file descriptors open after your redirection. Let's dissect the two paragraphs:
if (in) { //if '<' char was found in string inputted by user
fd = open(input, O_RDONLY, 0);
dup2(fd, STDIN_FILENO);
in = 0;
current_in = dup(0); // Fix for symmetry with second paragraph
}
if (out) { //if '>' was found in string inputted by user
fd = creat(output, 0644);
dup2(fd, STDOUT_FILENO);
out = 0;
current_out = dup(1);
}
I'm going to be charitable and ignore the fact that you are ignoring errors. However, you will need to error check your system calls.
In the first paragraph, you open a file and capture the file descriptor (it might well be 3) in the variable fd. You then duplicate the file descriptor over standard input (STDIN_FILENO). Note, though, that file descriptor 3 is still open. Then you do a dup(0) (which, for consistency, should be STDIN_FILENO), getting another file descriptor, perhaps 4. So you have file descriptors 0, 3 and 4 pointing at the same file (and, indeed, the same open file description β€” noting that an open file description is different from an open file descriptor). If your intention with current_in was to preserve the (parent) shell's standard input, you have to do that dup() before you do the dup2() that overwrites the output. However, you would be better off not altering the parent shell's file descriptors; it is less overhead than re-duplicating the file descriptors.
Then you more or less repeat the process in the second paragraph, first overwriting the only record of file descriptor 3 being open with the fd = creat(...) call but obtaining a new descriptor, perhaps 5, then duplicating that over standard output. You then do a dup(1), yielding another file descriptor, perhaps 6.
So, you have stdin and stdout of the main shell redirected to the files (and no way of reinstating those to the original values). Your first problem, therefore, is that you are doing the redirection before you fork(); you should be doing it after the fork() β€” though when you get to piping between processes, you will need to create pipes before forking.
Your second problem is that you need to close a plethora of file descriptors, one of which you no longer have a reference for.
So, you might need:
if ((pid = fork()) < 0)
...error...
else if (pid == 0)
{
/* Be childish */
if (in)
{
int fd0 = open(input, O_RDONLY);
dup2(fd0, STDIN_FILENO);
close(fd0);
}
if (out)
{
int fd1 = creat(output , 0644) ;
dup2(fd1, STDOUT_FILENO);
close(fd1);
}
...now the child has stdin coming from the input file,
...stdout going to the output file, and no extra files open.
...it is safe to execute the command to be executed.
execve(cmd[0], cmd, env); // Or your preferred alternative
fprintf(stderr, "Failed to exec %s\n", cmd[0]);
exit(1);
}
else
{
/* Be parental */
...wait for child to die, etc...
}
Before you do any of this, you should ensure that you've already flushed the shell's standard I/O channels, probably by using fflush(0), so that if the forked child writes to standard error because of a problem, there is no extraneous duplicated output.
Also note that the various open() calls should be error-checked.
You have way too many file descriptors open after your redirection. The code which you need is this.
if (pid == 0)
{ /* for the child process: */
// function for redirection ( '<' , '>' )
int fd0,fd1,i,in=0,out=0;
char input[64],output[64];
// finds where '<' or '>' occurs and make that argv[i] = NULL , to ensure that command wont't read that
for(i=0;argv[i]!='\0';i++)
{
if(strcmp(argv[i],"<")==0)
{
argv[i]=NULL;
strcpy(input,argv[i+1]);
in=2;
}
if(strcmp(argv[i],">")==0)
{
argv[i]=NULL;
strcpy(output,argv[i+1]);
out=2;
}
}
//if '<' char was found in string inputted by user
if(in)
{
// fdo is file-descriptor
int fd0;
if ((fd0 = open(input, O_RDONLY, 0)) < 0) {
perror("Couldn't open input file");
exit(0);
}
// dup2() copies content of fdo in input of preceeding file
dup2(fd0, 0); // STDIN_FILENO here can be replaced by 0
close(fd0); // necessary
}
//if '>' char was found in string inputted by user
if (out)
{
int fd1 ;
if ((fd1 = creat(output , 0644)) < 0) {
perror("Couldn't open the output file");
exit(0);
}
dup2(fd1, STDOUT_FILENO); // 1 here can be replaced by STDOUT_FILENO
close(fd1);
}
execvp(*argv, argv);
perror("execvp");
_exit(1);
// another syntax
/* if (!(execvp(*argv, argv) >= 0)) { // execute the command
printf("*** ERROR: exec failed\n");
exit(1);
*/
}
else if((pid) < 0)
{
printf("fork() failed!\n");
exit(1);
}
else { /* for the parent: */
while (!(wait(&status) == pid)) ; // good coding to avoid race_conditions(errors)
}
}
Here's what's happening. After you call fork() there are two processes executing that are duplicates of the original process. The difference is in the return value of fork() which is stored in pid.
Then both processes (the shell and the child) redirect their stdin and stdout to the same files. I think you were trying to save the previous fd in current_out, but as Seth Robertson points out, this doesn't currently work, since the wrong file descriptor is being saved. The parent also restores its stdout, but not stdin.
You could fix this bug, but you can do better. You don't actually have to redirect parent's output, just the child's. So simply check pid first. Then there is also no need to restore any file descriptors.

How to use dup2/close correctly to connect these three processes?

I'm trying to properly connect three processes in order to allow inter-process communication between them. I have one process, scanner, which takes the parent's STDIN and then processes the words within the stream. If a word length is odd, it sends it to one process, if it is even, it sends it to another. These processes should take in these words via STDIN (I assume) and then output some info back to the scanner process via STDOUT. The STDOUT of even/odd should be redirected to scanner, which will then read (using read) and then output/process the words. It's an academic exercise, not a practical one. Here's what a picture of it would look like:
Here's what my code currently looks like. The problem is I'm not exactly sure what to dup and what to close. Once I figure that out I should be good to go! Any advice would be appreciated.
File descriptors:
int scannertoeven[2]; int scannertoodd[2];
int eventoscanner[2]; int oddtoscanner[2];
//Pipe stuff here (ommitted)
Code:
//Create the child processes
if ((scanner_pid = fork()) == 0) {
//We need the scanner pid so even and odd can send signals to it
char pidofparent[sizeof(getpid())];
sprintf(pidofparent, "%i", getpid());
//Even stuff
if ((even_pid = fork()) == 0) {
close(scannertoodd[0]); close(scannertoodd[1]);
close(oddtoscanner[0]); close(oddtoscanner[1]);
//Not sure which ones to close
close(scannertoeven[0]); close(scannertoeven[1]);
close(eventoscanner[0]); close(eventoscanner[1]);
//Correct?
close(STDIN_FILENO);
dup2(scannertoeven[0], STDIN_FILENO);
close(STDOUT_FILENO);
dup2(eventoscanner[1], STDOUT_FILENO);
if(execl("./evenodd", "even", pidofparent, NULL ) == -1) {
printf("execl Error!");
exit(1);
}
//Odd Stuff
} else if ((odd_pid = fork()) == 0){
close(scannertoeven[0]); close(scannertoeven[1]);
close(eventoscanner[0]); close(eventoscanner[1]);
//Not sure which ones to close
close(scannertoodd[0]); close(scannertoodd[1]);
close(oddtoscanner[0]); close(oddtoscanner[1]);
//Correct?
close(STDIN_FILENO);
dup2(scannertoodd[0], STDIN_FILENO);
close(STDOUT_FILENO);
dup2(oddtoscanner[1], STDOUT_FILENO);
if(execl("./evenodd", "odd", pidofparent, NULL ) == -1) {
printf("execl Error!");
exit(1);
}
//Actual Scanner
} else {
// Not sure which ones to close- this is very wrong
close(scannertoeven[0]); close(scannertoeven[1]);
close(eventoscanner[0]); close(eventoscanner[1]);
close(scannertoodd[0]); close(scannertoodd[1]);
close(oddtoscanner[0]); close(oddtoscanner[1]);
//Not sure what to dup either
dup2(scannertoodd[1], STDOUT_FILENO);
dup2(scannertoeven[1], STDOUT_FILENO);
if(execl("./scanner", "scanner", stoeven, stoodd, eventos, oddtos, NULL ) == -1) {
printf("execl Error!");
exit(1);
}
//Wait twice here, or three times in main?
waitpid(odd_pid, &status2, 0);
waitpid(even_pid, &status3, 0);
}
//Main
} else {
//Close Pipes
close(scannertoodd[0]); close(scannertoeven[0]); close(eventoscanner[0]); close(oddtoscanner[0]);
close(scannertoodd[1]); close(scannertoeven[1]); close(eventoscanner[1]); close(oddtoscanner[1]);
//Wait for children to finish
waitpid(scanner_pid, &status1, 0);
printf("Done\n");
}
Not sure about the logic. But the way you use dup2 is not right.
The following code in the "Even" process:
close(STDIN_FILENO);
dup2(scannertoeven[0], STDIN_FILENO);
close(STDOUT_FILENO);
dup2(eventoscanner[1], STDOUT_FILENO);
should be:
dup2(scannertoeven[0], STDIN_FILENO);
// You should close scannertoeven[0], not STDIN. After this dup2, the even
// process will receive input from scannertoeven[0]
close(scannertoeven[0]);
// Note the the scannertoeven[0] is not "really" closed, just that the file
// is "attached" to STDIN
dup2(eventoscanner[1], STDOUT_FILENO);
// Same as above. After this dup2, all the even process's output will go
// to eventoscanner[1]
close(eventoscanner[1]);
The same as the "Odd" process.
Here is an example of dup2, for your reference.

Resources