IPC-using fork() in a loop

I need to write a program which creates some n number of processes first and also creates a fifo for each of them, then it connects to the server and..

I tried creating these processes with fork() in a for loop but it doesn't print out what i write inside the child..

   for(int count = 0; count < nIntClientCount; count++)
   {
      pid_t pid;

      if((pid = fork()) < 0){
          perror("fork failed\n");
        exit(1);
      }else if(pid == 0)
      {   
         if((pid = fork()) < 0)
         {
                  perror("inner fork failed\n");
                    exit(1);
         }
         else if(pid > 0)
         exit(0);
          //second child body

            printf("I am CHILD %d", count);
      }
     if(waitpid(pid, NULL, 0) != pid) 
    perror("wait pid didn't match\n");
   }


   

  sleep(2);
  printf("parent exited..\n");

  return 0;

//--------------------------
i also tried the code without using 2 forks, but it also didnt print anything, plz help me

Try to add

fflush(stdout);

After each call of printf()

I did but again i get the same result, printf does not print anything and i don't know what happens to the childs...

[root@localhost Randomized Dissemination]# ./a.out
downloader peers randomly selected
parent exited..

After add

exit(0);

after

printf("I am CHILD %d\n", count);

It prints this on my computer

I am CHILD 0
I am CHILD 1
I am CHILD 2
parent exited..

What system you've got?

I tried it both on CentOS 5.2 and Ubuntu

plz have a look at my code again

   for(int count = 0; count < nIntClientCount; count++)
   {
      pid_t pid;

      if((pid = fork()) < 0){
          perror("fork failed\n");
            exit(1);
      }else if(pid == 0)
      {
         if((pid = fork()) < 0)
         {
                          perror("inner fork failed\n");
                                exit(1);
         }
         else if(pid > 0)
                exit(0);
          //second child body

            printf("I am CHILD %d\n", count);
            fflush(stdout);
      }
     if(waitpid(pid, NULL, 0) != pid)
        perror("wait pid didn't match\n");
   }

  printf("parent exited..\n");

  return 0;
}

Moderators have had to edit you for code tags so many times you've got negative bits. The formatting on your code is also all ruined because you didn't use code tags. PLEASE USE CODE TAGS. IT'S NOT HARD.

You haven't posted the complete program, either, I'm having to write a large part of it myself to make it work.

Your program, with nIntClientCount=3, gives this output:

I am CHILD 0
wait pid didn't match
: No child processes
I am CHILD 1
I am CHILD 1
wait pid didn't match
: No child processes
wait pid didn't match
: No child processes
I am CHILD 2
I am CHILD 2
wait pid didn't match
: No child processes
wait pid didn't match
: No child processes
parent exited..
parent exited..
parent exited..
parent exited..
I am CHILD 2
wait pid didn't match
: No child processes
parent exited..
I am CHILD 2
parent exited..
wait pid didn't match
: No child processes
parent exited..
parent exited..

I just want to create n number of childs and after each child would start executing some instruction, but when i execute the program i don't receive anything, I wonder how you get the result.
my linux system is

CentOS 5.2

do u know how i can solve the problem?

Try this

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

int main()
{
    int i;
    int nproc = 3;
    pid_t pid;

    for (i = 0;i < nproc;i++) {
        switch ((pid = fork())) {
            case -1:
                printf("fork() error\n");
                break;

            case 0:
                 switch ((pid = fork())) {
                     case -1:
                        printf("second fork() error\n");
                        exit(0);
                     case 0:
                        printf("Child nr. %d\n", i);
                        exit(0);
                     default:
                        waitpid(pid, NULL, 0);
                        exit(0);
                 }
                 exit(0);

            default:
                waitpid(pid, NULL, 0);
                break;
        }
    }

    return 0;
}