The below code provides a few sample atomic operation functions used in the Linux Kernel. An interested reader can open the header file – “asm/atomic.h” for the specific architecture that his device is using (e.g x86/ ARM etc) to understand a lot more of the atomic APIs present.
The below code is based on linux kernel 4.15.0-36.
The above code can be compiled via a simple Makefile and executing “make” command from the terminal. The Makefile and the C code needs to present in the same folder. The Makefile is provided below
Insert the module using the “sudo insmod first_code.ko” command on the terminal window.
The output of the module prints would be present in the “dmesg” output. Remove the module using “sudo rmmod first_code.ko” after executing the code once.
The next article looks at a few Linux kernel calls to modify bits in an atomic fashion.
Linux Kernel – atomic operations – bit operations – sample code
Pingback: Linux Kernel – atomic operations | Hitch Hiker's Guide to Learning