One parent, multiple children pipe with fork()


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers One parent, multiple children pipe with fork()
# 1  
Old 05-23-2018
Linux One parent, multiple children pipe with fork()

The task I have to do is something along the lines "I receive some input and based on the first character I send it through pipe to one of the children to print".
The scheme it is based on is 1->2; 1->3; 1->4; 2 will print all the input that starts with a letter, 3 will print all the input that starts with a digit and 4 will print everything else. (Example: 12, Anne, **, *a will be printed by 3, 2, 4, 4)
I have quite understood how pipes work between one parent process and one child, but can't figure it out how to make this work, but I have tried, so I'mma attach the code down below. Any tip would be much appreciated.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{ int pipefd[2];
  int pipefe[2];
  int pipeff[2];
  pipe(pipefd);
  pipe(pipefe);
  pipe(pipeff);

  char arr[80];
  scanf("%s", arr);
  int f1 = fork();
  
  if(f1>0)
	{close(pipefd[0]);
	 close(pipefe[0]);
	 close(pipeff[0]);
	 if (arr[0]>='A'&&arr[0]<='z')
		{
         	write(pipefd[1], arr, strlen(arr)+1);
	 	close(pipefd[1]);
		}
	if(arr[0]>='0' && arr[0]<='9')
		{
		write(pipefe[1], arr, strlen(arr)+1);
	 	close(pipefe[1]);
		}
	else
	   {
	    write(pipeff[1], arr, strlen(arr)+1);
	    close(pipeff[1]);
	   }
         exit(0);
	}
   else if(fork()==0)
     { wait(NULL);
       close(pipefd[1]);
       read(pipefd[0], arr, strlen(arr)+1);
       printf("%s\n", arr);
       close(pipefd[0]);
       exit(0);
     }
   else if(fork()==0)
     { wait(NULL);
       close(pipefe[1]);
       read(pipefe[0], arr, strlen(arr)+1);
       printf("%s\n", arr);
       close(pipefe[0]);
       exit(0);
     }
   else if(fork()==0)
     { wait(NULL);
       close(pipeff[1]);
       read(pipeff[0], arr, strlen(arr)+1);
       printf("%s\n", arr);
       exit(0);
       return 0;
     }

}

# 2  
Old 05-23-2018
Code:
 else if(fork()==0)

You have three of these. No. any call to fork() creates a pid, you just lost the pid of the new process.

Code:
else  
{ mypid1 = fork();
  if (mypid!=0 ) 
  {
    // do something 
  }
}

And I don't see what your program logic should be using your current model. Get it working for one child process only. Set up pipes for that. As you have your code now there can be a race condition on pipes it seems to me.

If you have to have multiple children learn about IPC here, use a mutex on shared memory if you have to have this odd cascade of processes.

IPC: https://www.tldp.org/LDP/tlk/ipc/ipc.html
# 3  
Old 05-24-2018
In short, how fork() really works:

Code:
pid_t pid=fork();

if(pid < 0)
{
    perror("couldn't fork");
    exit(1);
}

if(pid == 0) {
        // Child code
        do_stuff();
        exit(0);
}

// Parent code

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

2. Programming

Multiple children and single pipe

Greetings everyone, I need a bit of help in solving the following problem: I'm given an array of numbers and I have to compute the sum of the array elements using n processes, and the inter process communication has to be done with pipes(one pipe, to be exact). I managed to solve the problem... (14 Replies)
Discussion started by: ephesos
14 Replies

3. Programming

Timed action after fork() in parent process

Assume you have such a piece of (more or less pseudo-)code: if(fork() == 0) {// childprocess chmod(someProgram, 00777); exec(someProgram); } else { // assume it never fails and this is the parent chmod(someProgram, 00000); // should be executed as soon as possible after the... (5 Replies)
Discussion started by: disaster
5 Replies

4. Programming

parent called more times - fork - C language

Hi gurus can you explain following lines of code ? #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv; switch(pid = fork()) { case -1: ... (7 Replies)
Discussion started by: wakatana
7 Replies

5. Shell Programming and Scripting

Creating a pipe using parent and child processes

Hello, I am trying to create a pipe that will direct stdout to in side of the pipe, and stdin to the out side of the pipe - I created two child processes to handle this. However, my pipe doesn't seem to be working correctly. Did I use execv() correctly? Command1 and command2 represent the two... (3 Replies)
Discussion started by: jre247
3 Replies

6. Programming

pipe-fork-execve

Hi everyone , after a pipe() system call i've forked and entred into the child process to execve a shell script .the problem here is that when the execve sys call fail , i want to send the error code (eg errno) to the parent process using the pipe writer side p , there is nothing received in the... (4 Replies)
Discussion started by: xtremejames183
4 Replies

7. UNIX for Advanced & Expert Users

Fork() 1 Parent 3 Children

Hi, as I understand fork(), it makes a copy of the parent which becomes a child. But is there anyway to make three children for that one parent. So in other words, if I look up the getppid() of the children, I want them to have the same value?? Thanks in advance to any help! (1 Reply)
Discussion started by: MS_CC
1 Replies

8. Programming

Problems with child comunicating with parent on fork()

Hello, I'm trying to implement a version of a bucketSort (kinda) server/client, but I'm having a VERY hard time on making the server behave correctly, when talking to the children, after it forks. The server is kinda big (300+ lines), so I won't post it here, but here's what I'm doing. 1)create a... (8 Replies)
Discussion started by: Zarnick
8 Replies

9. Programming

pipe read and write with many forked children

I know how to read and write if i have a forked process with only one child. However what is involved with reading and writing with many forked processes. Say one parent that forks 5 children, and needs to communicate with all 5 in half duplex. int temp, counter=0; do{ pipe(temp); ... (5 Replies)
Discussion started by: steveneliuk
5 Replies

10. Programming

how to creat 1 parent to call 3 children

hi there, im trying to produce this program that would run at first, and when it runs it will fork one child process to a program and then another forking to run this other program, and then another one . i cant seem to get it right can someone help me please here is what is got so far: int... (1 Reply)
Discussion started by: zmanultra
1 Replies
Login or Register to Ask a Question