I am making a test
My task will be one child pipelining the output to the input of another another child.
I am using ps -ef as the first command(by using execlp()) and grep root as the second command(bu using execlp()).I am getting the desired output but I am blocked in the process that is executing the grep.I have used top command to verify the current running processes.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
int main(){
int status,i;
int fds[2];
pid_t pid;
//pid=fork();;
if(pipe(fds)==-1){
perror("Error:");
exit(0);
}
if(fork()>0){
printf("The pid of the parent is %d\n",getpid());
if(fork()==0){
printf("The PID of the 2nd child %d\n",getpid());
//printf("Inside 2nd child\n");
close(0);
dup(fds[0]);
close(fds[1]);
//fcntl(fds[0],F_SETFD,FD_CLOEXEC);
//printf("The PID of the 2nd child %d\n",getpid());
if(execlp("grep","grep","root",NULL)==-1){
perror("ERROR FROM SECOND CHILD: ");
exit(1);
}
}
else{
printf("The parent is waiting\n");
/*for(i=0;i<2;i++){
printf("%d The PID OF THE EXIT CHILD is %d\n",i,wait(&status));
}*/
while(wait(NULL)>0)
perror("Error From Main:\n");
//wait(&status);
printf("The parent exited from the waiting stage\n");
return 0;
}
//while(1);
}
else{
close(1);
dup(fds[1]);
close(fds[0]);
//close(1);
printf("The pid of the First child is %d\n",getpid());
if(execlp("ps","ps","-ef",NULL)==-1){
perror("Error:");
exit(1);
}
}
}
please help me out.As I am on the process making a shell.The execution of this task is necessary.
grepto see the end-of-file and terminate.close(fds[1]). You forgot one.