#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

main()
{
    int test, pid, numberChild,fd[2],pidNummer=0;
    pipe(fd);
    printf("how many childprocesses do you want to make?: ");
    scanf("%d",&numberChild);
    if(!fork())     //b etta
    {
        test = close(0); assert(test == 0);
        test = dup(fd[0]); assert(test == 0);
//        read(fd[1],&pidNummer);
        test = close(fd[0]); assert(test == 0);
        test = close(fd[1]); assert(test == 0);
        sleep(4+numberChild);
    }
    system("ps -o pid,ppid,pgid,comm");
    if(!fork())     //g
    {
        int mypid = getpid();
        write(fd[0],&mypid,sizeof(int));
        test = close(fd[0]); assert(test == 0);
        test = close(fd[1]); assert(test == 0);
        sleep(2);
    }
//    test = close(fd[0]); assert(test == 0);
  //  test = close(fd[1]); assert(test == 0);
    system("ps -o pid,ppid,pgid,comm");
    if(numberChild > 0)
    {
        int x;
        for(x = 0; x += 1 ;0 > numberChild)
        {
            numberChild--;
            if(!fork())
                {
                    int mypid = getpid();
                    write(fd[1],&mypid,sizeof(int));
                    test = close(fd[0]); assert(test == 0);
                    test = close(fd[1]); assert(test == 0);
                    sleep(2);
                    wait(0);
                }
            system("ps -o pid,ppid,pgid,comm");
            wait(0);
      //      printf("I am greater then zero I am %d\n",numberChild);
            if(numberChild == 0)
            {
                break;
            }
        }
        //printf("I am greater then zero I am %d",numberChild);
    }
                        test = close(fd[0]); assert(test == 0);
                    test = close(fd[1]); assert(test == 0);
}
