ddd
ddd

Reputation: 285

Changing the Fork() system call

Hi I am trying create a system call that will count the number of forks that were called. I was going to change the fork system call so that it has a counter that will keep track of the number of times fork() was invoked. I was planning on adding a static variable to fork.h and then increment that everytime fork.c is called. I just don't understand what is going on in fork.c at all. Is this even the right approach?

Upvotes: 5

Views: 2172

Answers (2)

Matthew Slattery
Matthew Slattery

Reputation: 47038

The Linux kernel already maintains a count of the total number of forks in the system as a whole.

One of the tasks performed by copy_process(), which does a lot of the work involved in forking, is to increment the total_forks counter.

This counter is exposed to userland as the processes line in /proc/stat (by the code here).

Upvotes: 12

Peyman
Peyman

Reputation: 447

The source code for fork can be found at <linux kernel source tree>/kernel/fork.c file. The function is do_fork. You can add your code right before the else statement which returns errors. Remember that you would have to compile and reboot with this new kernel.

Upvotes: 1

Related Questions