Sponsored Content
Top Forums UNIX for Beginners Questions & Answers One parent, multiple children pipe with fork() Post 303017883 by Ildiko on Wednesday 23rd of May 2018 07:11:54 PM
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;
     }

}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
explain_pipe_or_die(3)					     Library Functions Manual					    explain_pipe_or_die(3)

NAME
explain_pipe_or_die - create pipe and report errors SYNOPSIS
#include <libexplain/pipe.h> void explain_pipe_or_die(int *pipefd); DESCRIPTION
The explain_pipe_or_die function is used to call the pipe(2) system call. On failure an explanation will be printed to stderr, obtained from explain_pipe(3), and then the process terminates by calling exit(EXIT_FAILURE). This function is intended to be used in a fashion similar to the following example: explain_pipe_or_die(pipefd); pipefd The pipefd, exactly as to be passed to the pipe(2) system call. Returns: This function only returns on success. On failure, prints an explanation and exits. SEE ALSO
pipe(2) create pipe explain_pipe(3) explain pipe(2) errors exit(2) terminate the calling process COPYRIGHT
libexplain version 0.52 Copyright (C) 2009 Peter Miller explain_pipe_or_die(3)
All times are GMT -4. The time now is 04:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy