help in C of fork() problem

i am a beginner of C, and i tired to fork multiple child and all of them belongs to same parents and each of child responsible for printing individual data.
but i don't have any idea how to do......
Can any body help? thanks a lot really.

What have you tried?

I have tried using for loop to create multiple childs
and when using pid==0 and the child process to output the data, but how to set the data for different child and how to make them print out each individual data?

Each child gets its own copy of the parent's variables. You can use the value of the for-loop to tell which child it is. Substitute your actual variable for 'n'.

if(pid == 0)
{
        fprintf(stderr, "This is child %d\n", n);

        switch(n)
        {
        case 0:
                fprintf(stderr, "This happens only in child zero\n");
                break;
        case 3:
                fprintf(stderr, "This happens only in child three\n);
                break;

        default:
                fprintf(stderr, "Any other child just runs this\n");
                break;
        }

        exit(0); // So you don't accidentally continue the for-loop in the child
}

i got what u mean. i am trying now. i hope it can be success.
thank you very much really.

---------- Post updated at 03:43 PM ---------- Previous update was at 03:39 PM ----------

by the way, this is my orginally program to fork(). but why it can't print out the child number although i use child++ ??

#include <stdio.h>  
#include<stdlib.h>
int main()
{
int parent=0, child=0, i=0, result, pid;
 pid = fork(); /* creates a new process, you only want the parent to spawn children? */
  for (i; i < 20; i++)
  {
	switch (pid)
   {
		 case -1:
			/* fork error */
			printf("Error occoured with fork()\n");
			exit(99);
		 case 0:
			/* child process */
			printf("child %d %d %d\n",getpid(), child++,getppid()); exit(0); 
		 default:
			 /* parent process*/
			{
			printf("parent %d %d\n", parent++, child);
			pid = fork(); /* fork new children here */
			wait();
			}
   }
  }
}

Each child gets its own independent "child" variable containing the value it had in the parent: Zero. Variables aren't shared, just copied. Each child also gets an independent copy of i, which will be 0 in the first child, 1 in the second child, etc, etc. because that's the value it had when the parent called fork().

i still have some difficulties about the program.
in fact, i want to use argv allow user to input n children what they want, and many numbers (which need to sort and let child to count how many value numbers they got later).

but i want ask after creating fork(),
how can i pass the argv value from parent to children, i want first child to read argv first number and second child read second,etc. how can i do that?
i hope some one can give me some idea how to do that.

---------- Post updated at 10:59 AM ---------- Previous update was at 02:57 AM ----------

also how to save pid into array? i tried to use int for it data type, but it seems not successful.

They already have it. They get a copy of it just like they get a copy of everything else.

Like I've been saying all along, use the value of i in the child. It gets a copy from the instant the parent called fork(), so child 0 gets 0, child 1 gets 1, child 2 gets 2, child 3 gets 3, child 4 gets for, and so forth.

You may be overthinking this. All your question amounts to is "how do I use an array" and the answer is array[index]. for most purposes argv starts at 1, not 0, so argv[i\+1]. Be careful not to exceed the value of argc or you will crash!

pid_t should work, but int should also work, so I suspect your program failed for other reasons. Not having psychic powers, I can't see from here why it's not working. Post your code!