From ee85f8f3fec85fb7cd4527875e7e9c650387e3bf Mon Sep 17 00:00:00 2001 From: lokesh1829 Date: Mon, 16 Feb 2026 04:03:09 +0000 Subject: [PATCH 1/3] Add C program for shared memory communication Added C program demonstrating inter-process communication using shared memory. --- README.md | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/README.md b/README.md index 62ea96bd..e94ddea8 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,116 @@ Execute the C Program for the desired output. # PROGRAM: ## Write a C program that illustrates two processes communicating using shared memory. +~~~ +//sem.c +#include +#include +#include +#include +#include +#include +#include + +#define TEXT_SZ 2048 // Shared memory size + +struct shared_use_st { + int written; + char some_text[TEXT_SZ]; +}; + +int main() { + int shmid; + void *shared_memory = (void *)0; + struct shared_use_st *shared_stuff; + + // Create shared memory + shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT); + if (shmid == -1) { + fprintf(stderr, "shmget failed\n"); + exit(EXIT_FAILURE); + } + + // Print the shared memory ID in a predictable format + printf("Shared memory id = %d\n", shmid); + + // Attach to shared memory + shared_memory = shmat(shmid, (void *)0, 0); + if (shared_memory == (void *)-1) { + fprintf(stderr, "shmat failed\n"); + exit(EXIT_FAILURE); + } + printf("Memory attached at %p\n", shared_memory); + + shared_stuff = (struct shared_use_st *)shared_memory; + shared_stuff->written = 0; + + pid_t pid = fork(); + + if (pid < 0) { + fprintf(stderr, "Fork failed\n"); + exit(EXIT_FAILURE); + } + + if (pid == 0) { // Child process (Consumer) + while (1) { + while (shared_stuff->written == 0) { + sleep(1); // Wait for producer + } + + printf("Consumer received: %s", shared_stuff->some_text); + + if (strncmp(shared_stuff->some_text, "end", 3) == 0) { + break; + } + + shared_stuff->written = 0; // Reset for producer + } + + // Detach shared memory + if (shmdt(shared_memory) == -1) { + fprintf(stderr, "shmdt failed\n"); + exit(EXIT_FAILURE); + } + exit(EXIT_SUCCESS); + } else { // Parent process (Producer) + char buffer[TEXT_SZ]; + + while (1) { + printf("Enter Some Text: "); + fgets(buffer, TEXT_SZ, stdin); + + strncpy(shared_stuff->some_text, buffer, TEXT_SZ); + shared_stuff->written = 1; + printf(shared_stuff->some_text); + + if (strncmp(buffer, "end", 3) == 0) { + break; + } + + while (shared_stuff->written == 1) { + sleep(1); // Wait for consumer + } + } + + // Wait for child process (consumer) to finish + wait(NULL); + + // Detach and remove shared memory + if (shmdt(shared_memory) == -1) { + fprintf(stderr, "shmdt failed\n"); + exit(EXIT_FAILURE); + } + + if (shmctl(shmid, IPC_RMID, 0) == -1) { + fprintf(stderr, "shmctl failed\n"); + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); + } +} + +~~~ From 9da84740f4423cae4a7107c3894e0ee95137defc Mon Sep 17 00:00:00 2001 From: lokesh1829 Date: Mon, 16 Feb 2026 04:05:12 +0000 Subject: [PATCH 2/3] Add output section to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e94ddea8..e5319ecd 100644 --- a/README.md +++ b/README.md @@ -139,5 +139,6 @@ int main() { ## OUTPUT + # RESULT: The program is executed successfully. From f38379c6941a0790c60d624c6b342a62c86f1086 Mon Sep 17 00:00:00 2001 From: lokesh1829 Date: Mon, 16 Feb 2026 09:39:58 +0530 Subject: [PATCH 3/3] Add output images to README Added output images to the README. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5319ecd..1fc45127 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,8 @@ int main() { ## OUTPUT - +![IMG-20260216-WA0001](https://github.com/user-attachments/assets/ee0cd6ac-6dc0-4efb-8a24-ab066f393e97) +![IMG-20260216-WA0002](https://github.com/user-attachments/assets/291dadd6-ae1c-473d-a5f5-ae2b2118e658) # RESULT: