Troubles with a pipe and a fork - c

I'm making a program that search files and sends it's results to other commands, like a pipe. ls | sort
When I run the program nothing happens.The problem I think is that the child's waits for the parent to stop writting in the SO buffer for starting the reading.
This is what it sends to stdout and what the pipe should send to the other command.
troneras#troneras-VirtualBox:~/Escritorio/busca.2012$ ./busca . -n . -print
./permisos.txt
./busca2.c
./mmap.pdf
./busca3.c~
./cuadernoso4.2011b.pdf
./busca.c~
./busca.c
./busca2.c~
./busca3.c
I don't understand what the problem is.
if(!strcmp(argv[4],"-pipe"))
{
int pipefd[2];
int pid,dummi;
if (pipe(pipefd)<0){
perror("pipe");
exit(1);
}
pid = fork();
if (pid<0){
perror("fork");
exit(1);
}
if (pid == 0){//Child process
close(pipefd[1]);//The child is only reading from the pipe
if(dup2(pipefd[0],0)!=0){perror("dup2");exit(1);}
close(pipefd[0]);
char *argumentos[argc-4];
int j;
for (j=5;j<argc;j++){
argumentos[j-5]=argv[j];
}
argumentos[j-5]= NULL;
execvp(argv[5],argumentos);
perror("execve: ");
}else{ //parent
close(pipefd[0]);
if(dup2(pipefd[1],1)!=1){perror("dup2");exit(1);}
close(pipefd[1]);
while(count--){
if(strcmp(files[count]->d_name,".") && strcmp(files[count]->d_name,"..")){
printf("%s/%s\n",argv[1],files[count]->d_name);
free(files[count]);
}
wait(&dummi);
}
}//end pipe
free(files);

BTW There is no reason to duplicate the argv[] array. Instead of
char *argumentos[argc-4];
int j;
for (j=5;j<argc;j++){
argumentos[j-5]=argv[j];
}
argumentos[j-5]= NULL;
execvp(argv[5],argumentos);
You could just as well do
execvp(argv[5],argv+5);

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

how make cat and grep work in the first and the second pipe in c writing like heredoc in bash <<

I am working to make a shell like bash, but i have trouble solving heredoc << so i made a test code as simple as possible for this question.
void pipeline()
{
int i = 0;
int fd[2];
pid_t pid;
int fdd = 0;
while (i < 2)
{
pipe(fd);
pid = fork();
if (pid == 0)
{
//dup2(fd[1],1); if i dup in the first pipe cat dont finalize
if (i == 0)
dup2(fd[0],0);
write(fd[1], "hello\nhow\nare\nyou\n", 17);
close(fd[0]);
close(fd[1]);
dup2(fdd, 0);
if (i == 0)
execlp("cat", "cat", NULL);
else
execlp("grep", "grep", "you" , NULL);
perror("error");
exit(1);
}
else
{
close(fd[1]);
fdd = fd[0];
wait(NULL);
i++;
}
}
}
int main(int *argc, char **argv, char **env)
{
pipeline();
}
I know that cat and grep need an EOF to run; what I'm doing is writing in stdin and running cat, but my question is: how do I save stdout for grep without duping stdout on the first pipe?
If I dup on dup2(fd[1],1) cat does not work in the first pipe, could someone help me out to make this code work? And make it as similar to bash heredoc as well if possible.
how do I save stdout for grep without duping stdout on the first pipe?
I'd rearrange the creation of the child processes from rightmost to leftmost - then grep is created first and can output to the initial output descriptor. A necessary change is to run all child processes before waiting on one as well as before writing, so that there's no deadlock even if the pipe buffer wouldn't suffice for the heredoc.
void pipeline()
{
int i = 2; // create children from last to first
int fd[2];
pid_t pid;
int fdd = 1; // output of last child is STDOUT
while (i--)
{
pipe(fd);
pid = fork();
if (pid == 0)
{
dup2(fdd, 1); // child's output
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
if (i == 0)
execlp("cat", "cat", "-A", NULL);
else
execlp("grep", "grep", "you" , NULL);
perror("error");
exit(1);
}
if (fdd != 1) close(fdd); // close if a pipe write end
fdd = fd[1]; // preceding child's output is pipe write end
close(fd[0]);
}
write(fd[1], "hello\nhow\nare\nyou\n", 17);
close(fd[1]); // signal EOF to child
while (wait(NULL) > 0) ; // wait for all children
}

Linux C - child process thinks the pipe is empty

I have a problem with this homework exercise. I am learning the Linux C so I am a beginner.
Now the exercise is simple: I have to create a child process. Now the parent process needs to read a text file (e.g. a.txt) and sends through a pipe. The child process reads from pipe and prints the content of the pipe to the terminal. But I don't understand that the child process doesn't read the pipe because it thinks the pipe is empty.
I post the code what I did so far:
#include "myinclude.h" //a separate file which contains all needed headers to run the program.
#define MERET 80
int main(int argc,char *argv[]){
int pfd[2];
int status;
char buffer[MERET];
pid_t pid;
FILE *fp1,*fp2;
if(argc != 2){
printf("Nincs eleg argumentum");
}
if(pipe(pfd) < 0){
syserr("pipe");
}
if((pid = fork()) < 0){
syserr("fork");
}
if(pid == 0){
close(pfd[1]);
if ((fp1 = fdopen (pfd[0],"r")) <0){
syserr("fdopen");
}
printf("mukodsz");
while(fgets(buffer,MERET,fp1) != NULL){//something here is not good
printf("%s",buffer);
fprintf(stdout,"Siker");
}
close(pfd[0]);
exit(0);
}
close(pfd[0]);
if ((fp1 = fdopen (pfd[1],"w")) == NULL){
syserr("fdopen");
}
if((fp2 = fopen(argv[1],"r")) < 0){
syserr("fopen");
}
while(fgets(buffer,MERET,fp2) != NULL){
fprintf(fp1,"%s",buffer);
//fprintf(stdout,"Siker\n");
}
close(pfd[1]);
wait(&status);
//fprintf(stdout,"Siker");
exit(0);
}
In my language "siker" means Success. I used it to debug the program but while loop of the child process is not printing anything.
When you fdopen. you must fclose.
If you close the original file descriptor instead, all not-yet-written data in buffers associated with the FILE* get lost.

Pipe not getting EOF

I am trying to write a test program that uses pipes to pass information between 3 linux commands. The bash equivalent of "ls | wc | wc". Below is my code
The only output i am getting is . The program is stuck there without exiting.
./a.out
starting main
creating pipe first
The expected output in bash is something like
ls | wc | wc
1 3 24
Edit:On running strace i could see that main process is on wait4, the two wc processes are stuck on read(0). As i guessed, its because wc is not getting the EOF. Why is this so?.Can someone help me to identify the issue?
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int p[2];
int p1[2];
int r=0;
int fork1(void)
{
int pid;
pid = fork();
if(pid == -1) {
perror("fork");
exit(1);
}
return pid;
}
int main (int argc, char const* argv[])
{
printf("starting main\n");
printf("creating pipe first\n");
if(pipe(p)<0) {
perror("pipe");
exit(1);
}
if(pipe(p1)<0) {
perror("pipe");
exit(1);
}
if(fork1()==0) {
//send output to p
close(1);
dup(p[1]);
close(p[0]); close(p[1]);
execlp("ls","ls",NULL);
exit(0);
}
if(fork1() == 0) {
close(1);//write to p1
dup(p1[1]);
close(p1[1]); close(p1[0]);
close(0);//read from p
dup(p[0]);
close(p[0]); close(p[1]);
execlp("wc","wc",NULL);
exit(0);
}
if(fork1() == 0)
{
close(0);//read from p1
dup(p1[0]);
close(p1[0]); close(p1[1]);
execlp("wc","wc",NULL);
exit(0);
}
close(p[0]); close(p[1]); close(p1[0]); close(p1[1]);
wait(&r);wait(&r);wait(&r);
printf("parent done\n");
return 0;
}
The problem was that i was not closing all the pipes in each branch.
I had to close p,p1 in all the three forks and the parent. Once i added those, it was working

How do I use two pipes in Unix C?

I have a homework to do that says the following:
Write a program in C that creates a child who will also create a child, make a pipe between the three processes, the fist process(father) will connect the second(child) and the child will connect with the third (child of the child). Our program should display the total number of system users who use bash as default shell. The result of the program should be identical to the "cat / etc / passwd | grep" / bin / bash $ "| wc-l"
I am confused with the first child and the method that we close the first pipe and open the second in the same time. If you reply me with the right code I 'll undestand it right once.
Thank you.
Here is what I 've wrote so far:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main()
{
int pid, pid2;
int fd[2];
int fd2[2];
char *arg[3];
char *arg2[3];
char *arg3[3];
if (pipe(fd) == -1)
{
perror("pipe");
exit(1);
}
pid = fork();
if (pid == -1)
{
perror("fork");
exit(2);
}
if (pid == 0)
{
if (pipe(fd2) == -1)
{
perror("pipe");
exit(11);
}
pid2=fork();
if(pid2 == -1)
{
perror("fork 2");
exit(22);
}
if (pid2 == 0)
{
//i am child 2 (child of the child)
close (fd2[1]);
dup2 (fd2[0],0);
close (fd2[0]);
arg3[0] = "wc";
arg3[1] = "-l";
arg3[2] = NULL;
execvp("wc", arg3);
perror("execvp second child");
}
else
{
//i am child 1
close (fd[1]);
dup2(fd[0],0);
close (fd[0]);
close (fd2[0]);
dup2(fd2[1],1);
close (fd2[1]);
arg2[0] = "grep";
arg2[1] = "/bin/bash$";
arg2[2] = NULL;
execvp("grep", arg2);
perror("execvp first child");
}
}
else
{
//i 'm the father
close (fd[0]);
dup2(fd[1],1);
close (fd[1]);
arg[0] = "cat";
arg[1] = "/etc/passwd";
arg[2] = NULL;
execvp("cat", arg);
perror("execvp father");
}
}
Your program very nearly works. What's missing is
//i am child 2 (child of the child)
close (fd[1]);
close (fd[0]);
The pipe you called fd is for communicating between 'cat' and 'grep'. What's happening in your current code is that cat dumps the file and exits, closing its output. Grep reads all of that and waits for the EOF on its input. Since "child 2" still has the input side of the pipe open (it inherited it via fork), grep waits forever. If run your program and then type ps you should see a grep and a wc hanging around waiting to finish.
The other thing you would normally do when constructing a pipeline like this is arrange it so that the final task (in this case wc) is the one that the shell is waiting for. As written, when your program is run from the shell it will appear to finish when cat finishes, and the output of wc will print as if from a background task. If you arrange the pipe so that wc is under "i am child 1" then the shell will be waiting for wc instead.
Alternatively you could fork all of the three processes off and "child 1" would invoke wait() to wait for all of them before exiting. That waiting process would be like your own tiny shell.

Resources