Reputation: 10036
I'm using the list_for_each
macro and the sibling
field of the current
task_struct
to print out the number of siblings current
has.
Here's the code (inf.num_siblings
tracks the number of siblings):
struct list_head *curr_node;
....
list_for_each(curr_node, &(current->sibling)) {
inf.num_siblings++;
}
...
//print inf.num_siblings
current->sibling
points to a node in a circularly linked list, so I think the code above should count the number of siblings/node in said list correctly.
This code seems to mostly work. If I run a program in the background that just loops forever before running the "count siblings" code, then the number of siblings for the current
process increases by one compared with the count that existed before I ran the loop code. However, if I run a process that calls fork()
once before starting to loop, rather than seeing the sibling count increase by two as I would expect, it only increases by one. Why is that?
Here is the code that forks then loops:
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
fork();
while(1){
sleep(3);
}
return 0;
}
Upvotes: 1
Views: 819
Reputation: 11648
When you call fork
you create a child
process not a sibling
. When you fork you'll increase the current->children
count not the current->sibling
count :)
Upvotes: 2
Reputation: 419
From: http://man7.org/linux/man-pages/man2/fork.2.html
When you called fork() the new process you created is a child and not a sibling of the process in which you called fork(). The sibling is correctly not getting incremented in this case.
Now, if you call fork() a bunch of times, and track the sibling count for the first child process, you will see the sibling count go up.
Upvotes: 0