Inter Process Communication through shared memory is a concept where two or more process can access the common memory.the communication is done via this shared memory where changes made by one process can be viewed by anther process. So, we can use one stack as a shared memory, where users(processes) sharing the stack (stacks) can push or pop elements. They can create one new shared stack and can also delete them.
Now, it is time for header file. If there is one header (.h extension) and one library file then all the processes can use those library functions so they don’t need to write the code for each function. So, there is 3 programs.
1. stacklib.h– The header file.
2. sharedstacklib.c– Library file for stacklib.h.
3. main.c – The users code.
We can use 4 functions for the shared stack operation.
1.shstackpush()– For push the elements.
2.shstackpop()– For pop out the last element.
3.shstackget()– User will enter one key and a shared stack will be created with that key. All the push, pop or remove function will be done by that particular key for particular stack. If any user enter the same key it will share the stack with the user/ users who previously created the stack with that key.
4.shstackrm()– For removing the stack.
Some IPC system calls has been used-
shmget()– Creates shared memory segment.
shmat(), shmdt()– Attaches and detaches with the shared memory.
shmctl()– Removes the shared memory segment.
semget()– Creates semaphore.
semctl()– Controls semaphore operations.
1. stacklib.h – Header file. Code is given below
2. sharedstacklib.c – Code is given below
3. main1.c -Process no 1 call1 the functions.
Code is given below
3. main2.c -Process no 2 calls the functions.
Code is given below
You can compile the code using
gcc main1.c sharedstacklib.c
gcc main2.c sharedstacklib.c
After executing main1.c
After executing main2.c
- IPC through shared memory
- Working with Shared Libraries | Set 2
- Working with Shared Libraries | Set 1
- Difference between Static and Shared libraries
- Concurrent Merge Sort in Shared Memory
- fork() and memory shared b/w processes created using it
- Communication between two process using signals in C
- Get the stack size and set the stack size of thread attribute in C
- Stack vs Heap Memory Allocation
- C Program to find direction of growth of stack
- Heap overflow and Stack overflow
- tgamma() method in C/C++ with Examples
- Modulo Operator (%) in C/C++ with Examples
- fpclassify() method in C/C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai