String Concatenation and case flipping using Shared Memory
SHARED MEMORY – STRING CONCATENATION & CASE FLIPPING
1. AIM
To demonstrate interprocess communication using System V Shared Memory, where:
-
The first process writes three strings to shared memory.
-
The second process concatenates the strings with spaces and writes the result back.
-
The first process then reads the concatenated string and prints it in flipped case.
2. THEORY
Shared memory is the fastest IPC mechanism in UNIX/Linux because multiple processes can access the same memory segment directly.
System calls used:
| System Call | Purpose |
|---|---|
shmget() | Create/get shared memory segment |
shmat() | Attach shared memory to process |
shmdt() | Detach shared memory |
shmctl() | Control operations (remove segment) |
Shared memory requires synchronization, but for simple sequential read/write, explicit synchronization is often omitted for simplicity in lab experiments.
3. ALGORITHM
Process 1 (Sender & Receiver)
-
Create shared memory using
shmget(). -
Attach it using
shmat(). -
Read three strings from user.
-
Write them into shared memory.
-
Wait for Process 2 to update the concatenated string.
-
Read the concatenated string.
-
Flip the case of each alphabet.
-
Print the flipped-case output.
-
Remove the shared memory segment.
Process 2 (Concatenator)
-
Access the same shared memory using
shmget(). -
Attach it using
shmat(). -
Read the three strings from shared memory.
-
Concatenate strings with spaces.
-
Write the concatenated string back to shared memory.
4. PROGRAMS
File 1: process1.c (Process 1)
File 2: process2.c (Process 2)
5. COMPILATION & EXECUTION
Terminal 1 (Run Process 1 first):
Terminal 2 (Run Process 2):
6. SAMPLE OUTPUT
Process 1
Process 2
7. RESULT
Thus, interprocess communication using Shared Memory was successfully implemented.
The first process passed strings to the second, which concatenated them, and the first process printed the concatenated string in flipped case.
Comments
Post a Comment