# Need urgent help with fork()

**URL:** <https://community.unix.com/t/need-urgent-help-with-fork/160562>\
**Category:** Programming\
**Created:** [March 26, 2006, 11:51am UTC](https://community.unix.com/t/need-urgent-help-with-fork/160562 "2006-03-26T11:51:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidoff](https://community.unix.com/letter_avatar/davidoff/32/5_5575768a8748004e209b776fc1b2916d.png) [@davidoff](https://community.unix.com/u/davidoff)\
**Post date:** [March 26, 2006, 11:51am UTC](https://community.unix.com/t/need-urgent-help-with-fork/160562/1 "2006-03-26T11:51:16Z")

</div>

Hy!

I must wrote some code with fork() command. The thing is that i have a while statement which count till 10.

I must wrote a program that one child has only one parent. So one parent has only one child and one child has only one parent. Can you please help me with these code.

```nohighlight
int main()
{
int N,i,pid = 0;
scanf("%d",&N);
while(i <= N)
{
   pid = fork();
   if (pid == 0)
     {
       printf("child proc %d = %x\n", i, getpid());
       wait(NULL);
     }
    if(pid != 0)
    {
      printf("Parent proc %d = %x\n", i, getppid());
      exit(0);
    }
    i++;
}
}

```

What i'm doing wrong. Please help!

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [March 26, 2006, 1:04pm UTC](https://community.unix.com/t/need-urgent-help-with-fork/160562/2 "2006-03-26T13:04:02Z")

</div>

```nohighlight
int N,i,pid = 0;
/* should be */
int N=0,
    i=0;
pid_t pid=0;

```

---

<div class="post-metadata">

**Author:** ![davidoff](https://community.unix.com/letter_avatar/davidoff/32/5_5575768a8748004e209b776fc1b2916d.png) [@davidoff](https://community.unix.com/u/davidoff)\
**Post date:** [March 26, 2006, 1:20pm UTC](https://community.unix.com/t/need-urgent-help-with-fork/160562/3 "2006-03-26T13:20:21Z")

</div>

> [@jim mcnamara](#):
>
> ```plaintext
> int N,i,pid = 0;
> /* should be */
> int N=0,
> i=0;
> pid_t pid=0;
> 
> ```

Ok i have change that. But my code still don't do it wright, so please help me. I must get out one parent - one child.
