Pipeline multiple children from same parent - c

Good morning, I've been "fighting" with this method for a long time and finally I decided to ask for help because I don't get what am I doing wrong. I am trying to create multiple children from the same parent and make the STDOUT of "child 1" the STDIN of "child 2" that way until there are no more children like a pipeline.
My actual code
void filter(void) {
if(Number_cmd != 0) {
int p,i;
int fd[2];
for(i=0;i<Number_cmd;i++)
pipe(fd);
for(p=(Number_cmd-1); p>=0; p--){
switch(fork()) {
case -1:
perror("fork");
exit(1);
case 0:
/* Child */
close(fd[1]);
close(0);
dup(fd[0]);
close(fd[0]);
execlp(filter[p], filter[p], NULL);
perror("exec");
exit(1);
default:
/* Father */
close(fd[0]);
close(1);
dup(fd[1]);
close(fd[1]);
break;
}
}
}
}
void directory(char* directory_name) {
DIR* dir = NULL;
struct dirent* ent;
char fich[1024];
char buff[4096];
int fd, reading;
struct stat sdata;
dir = opendir(directory_name);
while((ent=readdir(dir))!=NULL) {
if(ent->d_name[0]=='.')
continue;
fich[0]='\0';
strcat(fich, directory_name);
strcat(fich, "/");
strcat(fich, ent->d_name);
stat(fich,&sdata);
if(S_ISDIR(sdata.st_mode))
continue;
fd = open(fich, O_RDONLY);
while((reading= read(fd, buff, 4096)) > 0){
(write(1, buff, reading) < reading);
continue;
}
close(fd);
}
closedir(dir);
}
The problem is when im trying to call the method with more than one command, it looks like ii doesnt do anything, but when i run it with one command is working ok.
Thank everyone in advance. (Sorry for my English; it's not my native language)
EDIT
This is the main method:
char** cmd;
int Number_cmd;
int main(int argc, char* argv[]){
cmd = &(argv[2]); /*list of the commands*/
Number_cmd = argc-2; /* number of commands*/
filter();
directory(argv[1]);
return 0;
}

Problem 1:
for(i=0;i<Number_cmd;i++)
pipe(fd);
Unless the variable Number_cmd is 1 (or smaller), you leak pipe descriptors like crazy. You need some sort of array of file descriptors:
int fds[Number_cmd][2];
for (int i = 0; i < Number_cmd; i++)
if (pipe(fd[i]) != 0)
…report error and abandon ship (remembering to close any opened pipes)…
Problem 2:
This is mainly a consequence of Problem 1 — but you don't close enough file descriptors. Essentially, if you have N pipes open, your child will end up closing 2*N file descriptors, after duplicating two to standard input and standard output. The first and last children will be different; they don't override standard input and standard output respectively.
There are probably other issues, but these two spring to mind at once from a fairly quick look at the code.

Related

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.

C: redirecting stdin, stdout to pipes

I'm in the process of properly understanding pipes and FDs and I'm trying to program the following thing:
The program basically compresses stuff like gzip does with the options -cf.
The basic idea is:
I create two pipes in the parent process, then I fork it twice so that I'll have two children. In the first child, I redirect the first pipe's read end to stdin, and the second pipe's write end to stdout. Then I exec gzip with the -cf options so that it'll write to stdout (now the writing end of pipe2)
In the second child, I read from pipe2 and either output it directly or save it to a file.
The problem is, however, that no data arrives at the second child and I'm not really sure why. Here's the code:
int main(int argc, char **argv) {
char *file;
int out = 0;
if(argc == 2) {
file = argv[1];
out = 1;
} else if (argc > 2) {
exit(EXIT_FAILURE);
}
int c1pipe[2];
int c2pipe[2];
pipe(c1pipe);
pipe(c2pipe);
int f;
for(int i = 0; i < 2; i++) {
switch(f = fork()) {
case 0: //child
if(i == 0) { //first loop iteration, child 1
close(c1pipe[1]);
dup2(c1pipe[0], fileno(stdin));
close(c1pipe[0]);
close(c2pipe[0]);
dup2(c2pipe[1], fileno(stdout));
close(c2pipe[1]);
execlp("gzip", "gzip", "-cf", (char *) NULL);
} else if (i == 1) { //second loop iteration, child2
close(c1pipe[0]);
close(c1pipe[1]);
close(c2pipe[1]);
FILE *read = fdopen(c2pipe[0], "r");
char buffer[1024];
if(out == 0) { //output to stdout
while(fgets(buffer, 1024, read) != NULL) {
fprintf(stdout, "%s", buffer);
fflush(stdout);
}
} else { //write to specified file
FILE *writeto = fopen(file, "w");
while(fread(buffer, sizeof(char), strlen(buffer)+1, read) > 0) {
fwrite(buffer, sizeof(char), strlen(buffer)+1, writeto);
fflush(writeto);
}
fclose(writeto);
}
close(c2pipe[0]);
fclose(read);
}
break;
case -1: //err
//not implemented
break;
default: //parent
if(i == 0) {
close(c2pipe[0]);
close(c2pipe[1]);
close(c1pipe[0]);
FILE *writer;
writer = fdopen(c1pipe[1], "w");
char buffer[1024];
while(fgets(buffer, sizeof buffer, stdin) != NULL) {
fwrite(buffer, sizeof (char), strlen(buffer)+1, writer);
}
close(c1pipe[1]);
fclose(writer);
}
break;
}
}
return 0;
}
Please excuse the missing error handling as I wanted to create a quick-and-dirty version.
Any help is appreciated.
In the parent process, you are closing both ends of c2pipe before you have forked the second child.
You'd probably have figured this out already if you had put any error handling in on any of the read/write calls. In fact, if you checked for an error on the dup2 calls and then looked at errno, you probably would have found that it was EBADF (bad file descriptor).
Another issue is that your parent process exits before it knows that both child processes have finished. This means that the child processes will receive a signal and will themselves be terminated. The parent needs to call one of the variants of wait() to make sure both children have gone.

Pipelining between an unknown amount of processes

According to my understanding, I don't need to know the number of pipelines ahead (although I can, I implement it using a linked list). I have a structure called cmdLine:
typedef struct cmdLine
{
char* const arguments[MAX_ARGUMENTS];
int argCount;
char const *inputRedirect;
char const *outputRedirect;
char blocking;
int idx;
struct cmdLine *next;
} cmdLine;
And here's the function to execute (only the part of the code that matters):
void execute(cmdLine *pCmdLine)
{
int status = 0;
int fd[2];
pid_t id;
if(pCmdLine->next)
{
if(pipe(fd) == -1)
{
perror("Error opening pipe.\n");
exit(1);
}
if(pCmdLine->idx == 0)
{
dup2(fd[1], 1);
close(1);
close(fd[0]);
}
else
{
dup2(fd[0], 0);
close(0);
dup2(fd[1], 1);
close(1);
}
execute(pCmdLine->next);
close(fd[0]);
close(fd[1]);
waitpid(id, &status, 0);
}
id = fork();
if(id == 0)
{
if(execvp(pCmdLine->arguments[0], pCmdLine->arguments) == -1)
{
perror("execvp failed.\n");
exit(1);
}
close(fd[0]);
close(fd[1]);
exit(0);
}
if(pCmdLine->blocking)
{
waitpid(id, &status, 0);
}
if(pCmdLine->next)
{
execute(pCmdLine->next);
close(fd[0]);
close(fd[1]);
}
}
Basically, if it's the first command I close the input pipe (as it can't get any input) and replace fd[1] with the stdout so when I activate execvp() the next time it gets the input from the previous command executed and the output goes to the next up until it's the last command - when it only gets input and closes output pipe. I don't understand where the problem is, when I type something like ls -l | tail -n 2 it prints the long-listing of the WHOLE folder and then it is stuck (as if getting input - although I enter lots of input and nothing happens as preparing for segmentation fault).
What is the problem? How do I fix this? I think I'm not in the right direction at all, and I really need help with this.

How to loop through stdin & pipe output to a child execl command in C?

I have been trying to figure out how to loop through stdin from a file, then send it to a child process who sorts int using execl(). The code below works in that it takes the file & sorts the lines, but I am not seeing the "end of sentence" debug string I have added. Somehow this part of the code is being bypassed. I could use some help understanding the flow of data as it comes in from the file, then gets printed out to the screen.
int main(int argc, char *argv[])
{
pid_t p;
int status;
int fds[2];
FILE *writeToChild;
char word[50];
if(pipe(fds) == -1) {
perror("Error creating pipes");
exit(EXIT_FAILURE);
}
switch(p = fork()) {
case 0: //this is the child process
close(fds[1]); //close the write end of the pipe
execl("/usr/bin/sort", "sort", (char *) 0);
break;
case -1: //failure to fork case
perror("Could not create child");
exit(EXIT_FAILURE);
default: //this is the parent process
close(fds[0]); //close the read end of the pipe
writeToChild = fdopen(fds[1], "w");
wait(&status);
break;
}
while (fscanf(stdin, "%s", word) != EOF) {
//the below isn't being printed. Why?
fprintf(writeToChild, "%s end of sentence\n", word);
}
return 0;
}
Your primary problem is that you have the wait() in the wrong place. You wait for the child to die before you've written anything to it. You also have a secondary problem that don't redirect the read end of the pipe to the sort process's standard input.
You're not closing fds[0] in the child; cleanliness suggests that you should. You do need to fclose(writeToChild) before waiting; the sort won't stop until the parent has closed the pipe to the child.
These changes (and a few other ones) lead to:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
pid_t p;
int status;
int fds[2];
FILE *writeToChild;
char word[50];
if (pipe(fds) == -1)
{
perror("Error creating pipes");
exit(EXIT_FAILURE);
}
switch (p = fork())
{
case 0: //this is the child process
close(fds[1]); //close the write end of the pipe
dup2(fds[0], 0);
close(fds[0]);
execl("/usr/bin/sort", "sort", (char *) 0);
fprintf(stderr, "Failed to exec sort\n");
exit(EXIT_FAILURE);
case -1: //failure to fork case
perror("Could not create child");
exit(EXIT_FAILURE);
default: //this is the parent process
close(fds[0]); //close the read end of the pipe
writeToChild = fdopen(fds[1], "w");
break;
}
if (writeToChild != 0)
{
while (fscanf(stdin, "%49s", word) != EOF)
{
//the below isn't being printed. Why?
fprintf(writeToChild, "%s end of sentence\n", word);
}
fclose(writeToChild);
}
wait(&status);
return 0;
}

Pipes and Forks

The goal of this project is to use pipes and forks to execute a line-count utility already written in a multi-process manner (one process per argument). I'm currently working on getting a single process working before expanding to handle multiple args.
Given two executables, lc1 and lc2, I want lc2 to establish a pipe to the stdout file descriptor of lc1, so that when execlp("lc1", argv[1], NULL) is called, the output will be read in by
while ((c= read(pipefd[0], readin, SIZE)) > 0)
According to my Unix book, I should use the open, dup2, close method for redirecting stdout to stdin, and here's my code:
int pid, c, i;
char *readin= (char *)malloc(sizeof(SIZE));
if (pipe(pipefd)== -1)
perror("Can't open a pipe\n");
for (i=1; i< argc; i++){
if ((pid= fork())==-1)
perror("Can't fork\n");
run(argv[i]);
}
//close pipe
close(1);
if (dup2(pipefd[0], 0)==-1)
perror("Can't redirect stdin");
close(pipefd[1]);
for (i=1; i< argc; i++){
if ((wait(NULL))== -1)
perror("Wait error");
while ((c= read(pipefd[0], readin, SIZE)) > 0){;
//print buf count
total += atoi(readin);
}
}
The run function is
void run(char *f){
int fp;
if ((fp= open(f, O_RDONLY)) == -1)
perror("Can't open the file");
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
execlp("ls1", f, NULL);
}
When I try to execute this code, I get a stdin redirect error saying bad file descriptor. Why is this happening, and would appreciate any hints to for fixing.
run(argv[i]) is executed by both parent and child because are not assigning the functionality based on the returned PID, so one close after the other may have closed.
See below code, can he handy, I will use the code sample for situations like this. :
int main()
{
int pipe_fd[2] = {0};
int pid = -1;
int status = -1;
int ret_value = INVALID_CMD;
int cmd_output_len = -1;
status = pipe(pipe_fd);
if(status<0)
{
perror("pipe create err");
}
else
{
pid = fork();
if(pid<0)
{
}
else if (pid == 0)
{
/*Child functionality*/
child_func(pipe_fd, cmd);
}
else
{
/*Parent functionality*/
cmd_output_len = parent_fun(pid, pipe_fd);
}
}
return ret_value;
}
int child_func(int pipe_fd[], const char * cmd)
{
int status = 5;
int read_fd = pipe_fd[0]; /*read file descriptor*/
int write_fd = pipe_fd[1]; /*write file descriptor*/
int exit_status = 0;
/*close read fd*/
close(read_fd);
/*dup2 stdout to write fd*/
//status = dup2(1, write_fd);
status = dup2(write_fd, 1);
if(status<0)
{
exit(-1);
}
else
{
system(cmd);
exit(0);
}
}
int parent_fun(int child_id, int pipe_fd[])
{
int status = -1;
int len = 0;
bool_e break_loop = FALSE;
int read_fd = pipe_fd[0]; /*read file descriptor*/
int write_fd = pipe_fd[1]; /*write file descriptor*/
/*close write fd*/
close(write_fd);
while(1)
{
sleep(1);
status = waitpid(child_id, &status, WNOHANG);
switch(status)
{
case 0:
/*Child is still active*/
printf("No process waiting to exit..\n");
len = do_ur_fun(read_fd);
write(1, output, len);
break;
/*case EINTR:
case ECHILD:
case EINVAL:
perror("waitpid error");
break_loop = TRUE;
break;*/
default:
if(status<0)
{
perror("waitpid error");
break_loop = TRUE;
len = -1;
}
else if(child_id == status)
{
/*Valid staus from child*/
len = read_output(read_fd, output);
//write(1, output, len);
break_loop = TRUE;
}
else
{
}
break;
}
if(TRUE == break_loop)
{
break;
}
}
return len;
}
int do_ur_fun (int read_fd)
{
/*Do your exec*/
}
MaheshGupta024 identified a very important problem in your code; I'm assuming you will fix that.
One of the other problem areas is:
close(1);
if (dup2(pipefd[0], 0)==-1)
perror("Can't redirect stdin");
close(pipefd[1]);
for (i=1; i< argc; i++){
if ((wait(NULL))== -1)
perror("Wait error");
while ((c= read(pipefd[0], readin, SIZE)) > 0){;
//print buf count
total += atoi(readin);
}
}
The first close closes the process's standard output; this is seldom a good idea. The next line duplicates the read end of the pipe to standard input - which is fine. As noted in a comment above, perror() does not exit. You then close the write end of the pipe - that's correct; but you should presumably close the read end of the pipe too since you have set it to come from the pipe.
Your loop starts OK; you have redundant parentheses in the wait() line. You read from pipefd[0] instead of standard input - so maybe you didn't want to close pipefd[0] but neither did you need to duplicate it to standard input. You then have a nested loop that reads on the pipe while there's more data to be read from a child - you don't absolutely need the wait() code with its loop since the inner while won't terminate until all the children are dead. On the other hand, there's no great harm in it - after the first child dies, you'll read the data from all the other children, then go into the outer loop and wait for each other child, with the inner loop terminating immediately since there is no data left to read.
So:
Don't close stdout.
Don't dup the pipe read to stdin.
Decide whether you want to clean up the loop - it will work, but could be cleaner.
The run() function is:
void run(char *f){
int fp;
if ((fp= open(f, O_RDONLY)) == -1)
perror("Can't open the file");
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
execlp("ls1", f, NULL);
}
The argument should be const char *f (or use name or file instead of f). I would also pass the pipefd array to the function rather than use a global variable
.
Do not call a file descriptor fp; that name conventionally indicates a variable of type FILE *, not int.
However, you don't need to open the file in the first place - unless you want the calling program to do the error reporting instead of the invoked program. However, if you do want the calling program to do the error reporting, you should close the file descriptor before proceeding. (I've already commented on perror() returning).
It would be a good idea to print an error message after execlp(); the only time the function returns is when it fails, so there is no need to test its return value. You might want to exit too - rather than have the failed function go through the rest of the main program after the call to run().
Good points: you did close both the pipe file descriptors.
Hence:
void run(const char *file, int *pipefd)
{
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
execlp("ls1", f, NULL);
perror("Failed to exec ls1");
exit(EXIT_FAILURE);
}

Resources