I'm new in c and now I'm studying how the process signals work. I was trying to create a program where the main process create N processes ( N=argc ) and when created, they send a signal to their father. Each time this signal is received a counter (oki++) is increased.
My output is a mess!!! Something is missing. I tought it would be the wait() but is not.
pubuntu@pubuntu:~$ ./teste 1 2 3
process created number 6438
fathers pid 6438
Oki is 1
process created number 6439
fathers pid 6439
process created number 6440
Oki is 1
fathers pid 6440
Oki is 1
process created number 6441
fathers pid 6441
done
Oki is 1
done
process created number 6440
fathers pid 6440
Oki is 2
done
done
process created number 6439
fathers pid 6439
Oki is 2
process created number 6444
fathers pid 6444
Oki is 2
done
done
process created number 6439
fathers pid 6439
Oki is 3
done
done
process created number 6438
fathers pid 6438
Oki is 2
process created number 6447
fathers pid 6447
process created number 6448
Oki is 2
fathers pid 6448
done
Oki is 2
done
process created number 6447
fathers pid 6447
Oki is 3
done
done
process created number 6438
fathers pid 6438
Oki is 3
process created number 6451
fathers pid 6451
done
Oki is 3
done
process created number 6438
fathers pid 6438
Oki is 4
done
done
That's far from what I was expecting. I was expecting something like:
"
process created number 6451
fathers pid 6450
oki is 1
process created number 6452
fathers pid 6450
oki is 2
.
.
.
process created number 6454
fathers pid 6450
oki is 4
done
If fork() returns 0 then you are in the child process, so the parent will be the creating process.
If fork() returns > 0 then you are still in the parent process. The returned value is the pid of the created child.
So you can see that you are printing the parent's pid twice.
Also bear in mind that the output to the console will not necessarily be in consecutive order. This is because you will have multiple processes running and they will be all be fighting to get their output to the console simultaneously.