Changing the Fork () System Call

Hi, I am trying to create a system call that will count the number of forks being called. I was going to modify the fork system call so that it has a counter that will track the number of times fork () was called. I planned to add an old variable to fork.h and then increment it when fork.c. is called I just don't understand what happens in fork.c in general. Is this even the right approach?

+4
source share
2 answers

The Linux kernel already supports counting the total number of forks in the system as a whole.

One of the tasks that copy_process() does, which does most of the work related to forking, is to increment the total_forks counter .

This counter is displayed in userland as the processes line in /proc/stat (see code here ).

+9
source

The source code for fork can be found in the <linux kernel source tree>/kernel/fork.c file. do_fork function. You can add your code right before the else , which returns errors. Remember that you will need to compile and reboot this new kernel.

+1
source

Source: https://habr.com/ru/post/1339110/


All Articles