Forking Multiple Children

Split from necro'd topic

Page 1 of 1

9 Replies - 25803 Views - Last Post: 26 April 2011 - 05:43 AM Rate Topic: -----

#1 metttin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 22-April 11

Forking Multiple Children

Posted 22 April 2011 - 10:11 AM

hi

this is a wonderful thread helped me a lot
but what if I wanted to control the tasks of each child

for example spawning 2 children and giving each a different task
would adding a new case in switch suffice?

thx in advance

MOD EDIT: Split from old topic, as seen in post #4.

This post has been edited by JackOfAllTrades: 22 April 2011 - 10:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Forking Multiple Children

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Forking Multiple Children

Posted 22 April 2011 - 10:24 AM

View Postmetttin, on 22 April 2011 - 05:11 PM, said:

hi

this is a wonderful thread helped me a lot
but what if I wanted to control the tasks of each child

for example spawning 2 children and giving each a different task
would adding a new case in switch suffice?

thx in advance

yes, after fork() the child is a seperate processes and can do different things depending on the switch.
for example, calling fork() again to create another child process or calling execlp() to run a completly different process
http://linux.die.net/man/3/execlp
Was This Post Helpful? 0
  • +
  • -

#3 metttin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 22-April 11

Re: Forking Multiple Children

Posted 22 April 2011 - 10:33 AM

View Posthorace, on 22 April 2011 - 10:24 AM, said:

View Postmetttin, on 22 April 2011 - 05:11 PM, said:

hi

this is a wonderful thread helped me a lot
but what if I wanted to control the tasks of each child

for example spawning 2 children and giving each a different task
would adding a new case in switch suffice?

thx in advance

yes, after fork() the child is a seperate processes and can do different things depending on the switch.
for example, calling fork() again to create another child process or calling execlp() to run a completly different process
http://linux.die.net/man/3/execlp


thx for the fast reply
I tried this and it seemed to work
but just to be sure im posting it
any comment is welcome

childpid = fork();

  if(childpid==0){
	printf("im the 1st child \n");
	}

  else {

        printf("im the parent \n");
	childpid=fork();

	if(childpid==0){
		printf("im the 2nd child \n");
	}

	}



Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Forking Multiple Children

Posted 22 April 2011 - 10:44 AM

Split from this old topic.

Thanks for searching for a solution to your problem, but please start a new topic and link to the older one should you have additional questions.
Was This Post Helpful? 0
  • +
  • -

#5 metttin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 22-April 11

Re: Forking Multiple Children

Posted 22 April 2011 - 11:01 AM

View PostJackOfAllTrades, on 22 April 2011 - 10:44 AM, said:

Split from this old topic.

Thanks for searching for a solution to your problem, but please start a new topic and link to the older one should you have additional questions.


actually this was my last question in this topic :D
Was This Post Helpful? 0
  • +
  • -

#6 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Forking Multiple Children

Posted 22 April 2011 - 01:38 PM

try this program
// 26 child processes write characters to file - parent reads the file

#include<stdio.h>
#include <sys/types.h>
#include<stdio.h>


int main()
{
    FILE *f;
    char c;
    int a = 'A';
    int C = 97; /*'a' = 97*/
    int i;
    int r, pid;
    remove("f.txt");
    for ( i = 1; i <= 26; i++ ){
         if ((pid = fork()) != 0){
                /*parent process  pid != 0*/
                /* wait for chil to terminate */
                waitpid(pid);
                a++;            // increment character
               }
         else
         {
             /*child process pid = 0*/
             /*write next character to file*/
             f = fopen("f.txt", "a");
                 fprintf(f, "%c", a);
                 fclose(f);
                 exit(1);
          }
    }
    // now open resultant file and print it
    f = fopen("f.txt", "r");
    while (1){
       fscanf(f, "%c", &c);
       if ( feof(f))break;
       printf("%c", c);
       }
    fclose(f);
    printf("\n");
}


see what the file f.txt holds after the run ??
Was This Post Helpful? 0
  • +
  • -

#7 metttin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 22-April 11

Re: Forking Multiple Children

Posted 22 April 2011 - 03:07 PM

View Posthorace, on 22 April 2011 - 01:38 PM, said:

try this program
// 26 child processes write characters to file - parent reads the file

#include<stdio.h>
#include <sys/types.h>
#include<stdio.h>


int main()
{
    FILE *f;
    char c;
    int a = 'A';
    int C = 97; /*'a' = 97*/
    int i;
    int r, pid;
    remove("f.txt");
    for ( i = 1; i <= 26; i++ ){
         if ((pid = fork()) != 0){
                /*parent process  pid != 0*/
                /* wait for chil to terminate */
                waitpid(pid);
                a++;            // increment character
               }
         else
         {
             /*child process pid = 0*/
             /*write next character to file*/
             f = fopen("f.txt", "a");
                 fprintf(f, "%c", a);
                 fclose(f);
                 exit(1);
          }
    }
    // now open resultant file and print it
    f = fopen("f.txt", "r");
    while (1){
       fscanf(f, "%c", &c);
       if ( feof(f))break;
       printf("%c", c);
       }
    fclose(f);
    printf("\n");
}


see what the file f.txt holds after the run ??


this so cool,much more than I asked for

are there other ways of synchronising processes

Quote

actually this was my last question in this topic :D

i stand corrected
Was This Post Helpful? 1
  • +
  • -

#8 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Forking Multiple Children

Posted 22 April 2011 - 10:31 PM

you can use pipe to send data between processes, e.g.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
          int pid, p[2]; /* pipes are identified by integers */
          char buffer2[] = "hello ", buffer1[100], N = 6;
          if (pipe(p) < 0)
             {
              perror("pipe error");
              exit(1);
             }
          pid = fork();
          if (pid == 0)
             { /* Child process */
              printf("\nchild");
              read(p[0], buffer1, N);  /* read pipe */
              printf("%s", buffer1);
             }
          if ( pid > 0)
             { /* Parent process */
              printf("\nparent ");
              write(p[1], buffer2, N); /* write into pipe */
             }
          return 0;
  }


have a look at interprocess communications
http://tldp.org/LDP/lpg/node7.html
http://programmingex...-communications
Was This Post Helpful? 0
  • +
  • -

#9 metttin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 22-April 11

Re: Forking Multiple Children

Posted 23 April 2011 - 01:19 AM

View Posthorace, on 22 April 2011 - 10:31 PM, said:

you can use pipe to send data between processes, e.g.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
          int pid, p[2]; /* pipes are identified by integers */
          char buffer2[] = "hello ", buffer1[100], N = 6;
          if (pipe(p) < 0)
             {
              perror("pipe error");
              exit(1);
             }
          pid = fork();
          if (pid == 0)
             { /* Child process */
              printf("\nchild");
              read(p[0], buffer1, N);  /* read pipe */
              printf("%s", buffer1);
             }
          if ( pid > 0)
             { /* Parent process */
              printf("\nparent ");
              write(p[1], buffer2, N); /* write into pipe */
             }
          return 0;
  }


have a look at interprocess communications
http://tldp.org/LDP/lpg/node7.html
http://programmingex...-communications


yes i know but dont forget to close one end of the pipe
when you write/read

 if (pid == 0)
             { /* Child process */
              printf("\nchild");
              close(p[1]);
              read(p[0], buffer1, N);  /* read pipe */
              printf("%s", buffer1);
             }
          if ( pid > 0)
             { /* Parent process */
              close(p[0]);
              printf("\nparent ");
              write(p[1], buffer2, N); /* write into pipe */
             }



im gonna check the links u gave me
c ya later
Was This Post Helpful? 0
  • +
  • -

#10 metttin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 22-April 11

Re: Forking Multiple Children

Posted 26 April 2011 - 05:43 AM

so I have a new challenge

I have to fork 2 child processes
and 2 pipes

first child will launch gzip with execlp
parent process will write sth through the first pipe to gzip process
then the second child should read from the 2nd pipe

im guessing it will look sth like this

childpid = fork();

  if(childpid==0){
	execlp("gzip","gzip","-cf",NULL);
	}

  else {

        /*read from stdin with fgets or sth like that
          write to first pipe*/

	childpid=fork();

	if(childpid==0){

		/*read from second pipe*/
                
	}

	}



can someone give me some pointers
like how do i interact with gzip process

thx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1