Running process in the background

Hi,

I have this simple c program that creates duplicate process with fork():

#include <sys/types.h>
main()
{
if (fork() == 0)
while(1);
else
while(1);
}

I tried running it in the background

gcc -o test first.c
test &

And I got this list of running process:

ps -l
F S   UID    PID   PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  46202  46194  0  80   0 -  5782 wait   pts/0    00:00:01 bash
0 T  1000  47701  46202  0  80   0 -  1998 signal pts/0    00:00:00 awk
0 T  1000  47702  46202  0  80   0 -  1998 signal pts/0    00:00:00 awk
0 R  1000  54295  46202  0  80   0 -  8370 -      pts/0    00:00:00 ps
[3]   Exit 1                  test

Why can`t I see the PID of the two process (father and son) created with the fork() ?

You can't see the processes because they finished when you issued ps:

[3]   Exit 1                  test

My c knowledge is maybe a bit rusted, but you keep using the cpu all the time with your infinite loops and may have hit some limitation which caused your processes to abort. Maybe use sleep inside your loops to avoid overusing the cpu?

Thanks,

but it ended up the same way

ps -el

lists all the processes.

I think the problem is much simpler than you might think. Your choice of name for your code ( test ) is the name of a shell built-in in most shells and the name of a utility probably located in /bin or /usr/bin on your system and, therefore, likely will be found in your search path before a utility named test located in your current working directory.

You will probably see exactly what you were expecting to see (two copies of your test utility running) if you invoke it with:

./test &

instead of with:

test &