Adding system calls in Linux 0.01 for Semaphore using C ++

I am trying to understand how to implement a set of system calls that provide a semaphore module (without busy) in Linux 0.01. I want these system calls to allow a process to request a fresh semaphore and use it to synchronize the process.

I want to write them in C ++, but I have problems with their work. Anyone have a simple solution?

  • (system call # 110) int sema_request (int value): this function returns a fresh semaphore if it succeeds, and -1 otherwise. A fresh semaphore is initialized to a value. An implementation must support at least 10 different semaphores throughout the OS.
  • (system call # 111) int sema_wait (int s): this function implements the "wait" operation on semaphore s. It returns 0 if successful, and -1 otherwise.
  • (system call # 112) int sema_signal (int s): this function performs the signal operation on semaphore s. It returns 0 if successful, and -1 otherwise.
+4
source share
2 answers

You won’t learn much if someone else does your work for you, but here is the semaphore implementation in user mode that I wrote:

https://github.com/vinniefalco/VFLib/blob/master/modules/vf_core/threads/vf_Semaphore.h https://github.com/vinniefalco/VFLib/blob/master/modules/vf_core/threads/vf_Semaphore.cpp

Of course, you will need to map WaitableEvent, SpinLock, and Atomic to their respective counterparts based on your environment (conditional variable, mutex, and atomic integer operation, respectively). You also need to provide a simple stack, or you can adapt the locking implementation from my library. Please note that if you decide to go without blocking, you will have to keep the β€œdeleted list” hanging or you will get an ABA problem.

0
source

Your API design looks fine and should work well with it.

You can initialize 10 kernel level semaphores when loading the driver. When sema_request calls return an index of 0 ~ 9 if the request is valid. When calling sema_wait (index) will invoke the corresponding wait semaphore and vice versa.

The kernel level semphore has implemented all the functions you need (blocking, waiting queues, waking up), and the main goal of your driver code is to write good driver interfaces and print something message if there is a problem

Link: https://www.kernel.org/pub/linux/kernel/people/rusty/kernel-locking/c93.html

0
source

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


All Articles