Using fork () to make 3 children from 1 parent in C (not C ++)

Hi, I worked on a program that develops children, and then will deploy more children from each child, but I do not need help. When I run my program (here it is a function, but it works the same way), I have to have one parent (PPID) giving birth to 3 children (PIDS = 1,2,3), but I get either the same PID and PPID 3 times (my current code) or before I got 3 parents with each parent having one child, and the PPIDS were different as well as the PIDS, but the PPIDs were the same as the previous child PIDs. In my last attempts, he never displays the message of the parent (father) over the child (son). It should look like this.

[dad] hi am I PID 1234 and I come from ####(dont care what this number is) [son] hi i am PID 1111 and I come from PPID 1234 [son] hi i am PID 1112 and I come from PPID 1234 [son] hi i am PID 1113 and I come from PPID 1234 

here is my code. I'm just looking for clues if possible, unless it's a stupid mistake I made, such as “just move the fork () to a child process” or something like that.

I also have child_count, so I can easily count them.

  int forking(null) { void about(char *); int i=0; int j=0; int child_count =0; about("dad"); for(i = 0; i < 3; i++ ){ pid_t child = 0; child = fork(); if (child < 0) { //unable to fork error perror ("Unable to fork"); exit(-1);} else if (child == 0){ //child process about ("son"); printf("I am child #%d \n",child_count); child_count++; exit(0);} else { //parent process (do nothing) } } for(j = 0; j < 3; j++ ){ wait(NULL);//wait for parent to acknowledge child process } return 0; } 
+5
source share
2 answers

Parent must - print message
- call fork three times
- wait for the three children to leave

Every child needs to - print a message
- log off

So the code is as simple as

 int main( void ) { printf( "[dad] pid %d\n", getpid() ); for ( int i = 0; i < 3; i++ ) if ( fork() == 0 ) { printf( "[son] pid %d from pid %d\n", getpid(), getppid() ); exit( 0 ); } for ( int i = 0; i < 3; i++ ) wait( NULL ); } 

which generates this output

 [dad] pid 1777 [son] pid 1778 from pid 1777 [son] pid 1779 from pid 1777 [son] pid 1780 from pid 1777 
+4
source

The only thing you should remember is that when you fork , the parent and child will continue to work with the code at this point.

So, if you do not correctly define the definition of child / parent, children will most likely launch their own children.

A good way to run three children without grandchildren is to use a counter in conjunction with the returned process id from the fork call according to the following lines:

 #include <stdio.h> #include <unistd.h> #define COUNT 3 int main(void) { # Desired and actual count. int count = COUNT, children = 0; // Force parent initially. pid_t retpid = 1; // Only fork if limit not reached AND is parent (children // will exit loop with retpid == 0). while (count-- > 0 && retpid > 0) // Adjust actual count if successful. if ((retpid = fork()) > 0) children++; // Detect parent, all forks returned non-zero. if (retpid != 0) { printf("Parent %d spawned %d/%d children\n", getpid(), children, COUNT); // Wait for children to finish. while (children-- > 0) wait(NULL); } else { // Otherwise you were one of the children. printf("Child %d, sired by %d\n", getpid(), getppid()); } return 0; } 

This displays what you think, although, due to the vagaries of planning, it is not necessary in this order:

 Parent 26210 successfully spawned 3/3 children Child 26212, sired by 26210 Child 26213, sired by 26210 Child 26211, sired by 26210 

Checking the returned PID code ensures that only the parent performs any fork, and the account will limit it to a certain value.


One thing you also need to keep an eye on is output buffering. When you use fork, you can get two processes with buffered output.

If the output device can be detected as a terminal, flushing will run normally on the output of a new line, so your printf calls will probably not duplicate the output for a normal run.

You just need to know that you can get interesting results if you redirect your output to a file, for example.

+2
source

Source: https://habr.com/ru/post/1232506/


All Articles