This repository has three parts:
-
Slightly modifying and solving the famous Dining philosophers problem using mutex locks and semaphores.
-
Demonstrating Interprocess communication between 2 processes using 3 mechanisms:
- FIFOs
- Shared Memory
- UNIX domain sockets
-
Writing own small linux kernel module using C language.
Detailed explaination of all the parts:
The dining philosophers problem contains five philosophers sitting on a round
table can perform only one among two actions – eat and think. For eating, each
of them requires two forks, one kept beside each person. Typically, allowing
unrestricted access to the forks may result in a deadlock.
Inside the folder modifiedDiningPhilosophers
, there are 4 programs to simulate the philosophers using threads, and the forks using global variables. The deadlock has been resolved using the following techniques: \
- Strict ordering of resource requests (Using thread locks)
- Utilization of semaphores to access the resources.
singleBowl-Mutex.c
resolves the problem of 1 saucebowl with 5 philosophers using mutex locks.singleBowl-Semaphores.c
resolves the problem of 1 saucebowl with 5 philosophers using OS semaphores.twoBowls-Mutex.c
resolves the problem of 2 saucebowl with 5 philosophers using mutex locks.twoBowls-Semaphores.c
resolves the problem of 2 saucebowl with 5 philosophers using semaphores.
To run the programs, run the following commands:
git clone https://github.com/Ashutosh-Gera/LinuxIPC-KernelModule
cd modifiedDiningPhilosophers
sudo make
sudo ./singleBowl-Mutex
sudo ./singleBowl-Semaphores
sudo ./twoBowls-Mutex
sudo ./twoBowls-Semaphores
Resources used:
- Prof. Arani Bhattacharya (IIIT Delhi) 's lecture notes
- Prof. Mythili Vykuru (IIT Bombay)'s notes
- Wikipedia
- Manpage - mutex lock
- Manpage - mutex destroy
- Scalar article
- Semaphore init - Manpage
- Semaphore wait - Manpage
- Mutex lock - Manpage
- Little Book of semaphores
In this section, we have demonstrated interprocess communication in Linux using 3 mechanisms:
- Unix domain sockets
- FiFo
- Shared Memory
For each of these mechanisms, I have written 2 programs i.e
sender
receiver
NOTE: You can modify the data that needs to be sent/read however you need, I have just sent some test data.
To run the programs, run the below commands [I have shown for shared memory, you can do for FiFo and/or socket similarily]:
# If you haven't cloned this repository yet
git clone https://github.com/Ashutosh-Gera/LinuxIPC-KernelModule /
# common steps
cd <your-path>/Interprocess-Communications
cd sharedMem/
make
Open 2 terminals side by side (or use tmux)
#On first terminal
./Server
#On second terminal
./Client
NOTE: Run these programs simultaneously together.
Below are the images for these processes working for reference:
Resources used:
- Beej's guide to UNIX IPC [book]
- IBM documentation for sockets
- Linux Manpages
In this part, I have written my own small kernel module. I have implemented a kernel system call as a module. The task of the system call
is to read the entries of the process task_struct
corresponding to any
given process (supplied as input via command line argument) and prints the values of the following field:
- pid (process id)
- user_id
- process group id (pgid)
- command path
The system call is implemented in the kernel and is functional only when the module is loaded.
To run this module, you should run the following commands:
NOTE: This module is written for Artix Runit Kernel which is a very raw arch linux kernel. It may not work for other operating systems, thus, to test it, first you'd need to install and compile this kernel (preferably on a Virtual machine) and then proceed with the commands.
# If you have not cloned this repo yet
git clone https://github.com/Ashutosh-Gera/LinuxIPC-KernelModule
cd <your-path>/CustomKernelModule
sudo make
This compiles this module and generates a .ko (loadable kernel file), among others.
# Now to load our kernel module and use it
insmod myModule.ko arg=10 #inserts (loads) the module
dmesg # to check the output for the given argument (10 in this case)
rmmod myModule # to unload our module
Resources used:
That's it for today! Thank you for coming here. Hope you learnt something today (Do star the repo if you did 😉)