0

I am trying to understand fork, sleep... commmands.

I want to do 4 children and one parent operation exactly in this order. Parent> child1> child4> child2> child3. The tasks of these processes are as I wrote in the code below. In this code I have 1 parent, 3 child and 1 grandson(child4 is a grandson). How can I make transactions in this order? I tried to put a sleep on each if's but the program finished without waiting for input in child2.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(void) 
{ 
int n1 = fork();
int n2 = fork(); 
int input;



if (n1 > 0 && n2 > 0) { 


int n3=fork();

if(n3==0)
{ //3th child

 printf("3th child process id is %d  (parent: %d) \n", getpid(),getppid());

    if(input == getppid()) {
        printf("matched!\n"); }
    else {
        printf("not matched!\n"); }
printf("program ended\n");
}

else {

    printf("parent process id is %d  (parent: %d)\n", getpid(),getppid());
sleep(1);
}

} 
else if (n1 == 0 && n2 > 0) 
{ 
    printf("1th child process id is %d (parent: %d)\n",getpid(),getppid());
FILE * fp;
fp = fopen ("xx.txt", "w+");

printf("file was created...\n");
 sleep(1);


} 
else if (n1 > 0 && n2 == 0) 
{ 

printf("2th child process id is %d (parent: %d) \n", getpid(),getppid());
printf("Enter a key: \n");
scanf("%d",&input);

FILE * fp;
fp = fopen ("xx.txt", "w+");
fprintf(fp, "%d", input);
printf("input has been written to the txt file!\n");

} 
else { 
//4th child

    printf("4th child grandson process id is %d  (parent: %d)\n", getpid(),getppid());
printf("say me password!\n");


} 

return (0); 

}

jww
  • 97,681
  • 90
  • 411
  • 885
cagdas xx
  • 15
  • 3
  • I just figure out that I maybe did not understood really what you asked: do you want 4 children with the same father or do you want each son to be the father of the next child? – OznOg Oct 21 '18 at 07:51
  • Thanks for your answer. I want 3 child from same parent and 1 child from a child that is grandson. – cagdas xx Oct 21 '18 at 08:15
  • ok, then i can remove my answer.. – OznOg Oct 21 '18 at 08:24
  • Possible duplicate of [Can the order of execution of fork() be determined?](https://stackoverflow.com/q/6696959/608639) – jww Mar 25 '19 at 11:49

1 Answers1

1

You could try something like:

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(void)
{
    const size_t nb_child = 3;
    size_t children_count = nb_child;

    while (children_count > 0) {

        int pid = fork();

        if (pid == 0) {

            printf("child process id is %d  (parent: %d) \n", getpid(),getppid());
            if (children_count == 1) {
                int pid2 = fork();
                if (pid2 == 0) {
                    printf("child process id is %d  (parent: %d) \n", getpid(),getppid());
                    sleep(2);
                } else
                    wait(NULL);
            }
            return 0;
        } else {
            //father
        }
        children_count--;
    }

    while (children_count != nb_child) {
        wait(NULL);
        children_count++;
    }
    return 0;
}

The "difficult" part is to wait for all children before exiting

OznOg
  • 4,440
  • 2
  • 26
  • 35
  • Yes it works there is 3 child and 1 grandson, thank you; this code is more professional than mine. But another issue is that all children wait for each other.Can we produce a solution to this? – cagdas xx Oct 21 '18 at 09:09
  • what do you mean by "wait for each other"? – OznOg Oct 22 '18 at 18:53