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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- fork() and memory shared b/w processes created using it
- Working with Shared Libraries | Set 1
- Working with Shared Libraries | Set 2
- Concurrent Merge Sort in Shared Memory
- IPC through shared memory
- Difference between Static and Shared libraries
- Communication between two process using signals in C
- Get the stack size and set the stack size of thread attribute in C
- C Program to find direction of growth of stack
- Heap overflow and Stack overflow
- Stack vs Heap Memory Allocation
- Count number of unique Triangles using STL | Set 1 (Using set)
- How will you print numbers from 1 to 100 without using loop?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- How to print % using printf()?
- Print "Even" or "Odd" without using conditional statement
- How to deallocate memory without using free() in C?
- To find sum of two numbers without using any operator
- Can we access private data members of a class without using a member or a friend function?
- Print a long int in C using putchar() only
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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