From 06c3b4e2ac514a3c1880b77b2d3ddca6ccf6fbe9 Mon Sep 17 00:00:00 2001 From: ItsMAX0112 Date: Tue, 22 Oct 2024 02:14:43 +0530 Subject: [PATCH] still working in shm --- .vscode/settings.json | 4 +- lab4/src/makefile | 2 +- lab4/src/shared.cpp | 260 ++++++++++++++++++++++++++++++++++++++++++ lab4/src/shm1.cpp | 2 +- 4 files changed, 265 insertions(+), 3 deletions(-) create mode 100644 lab4/src/shared.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index a0a2e09..521fcd2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,8 @@ "ostream": "cpp", "chrono": "cpp", "iostream": "cpp", - "iosfwd": "cpp" + "iosfwd": "cpp", + "fstream": "cpp", + "xlocmes": "cpp" } } \ No newline at end of file diff --git a/lab4/src/makefile b/lab4/src/makefile index 2743f87..bd2a5e8 100644 --- a/lab4/src/makefile +++ b/lab4/src/makefile @@ -15,4 +15,4 @@ run-coms: ./coms.out clean: rm a.out coms.out -.PHONY: run-coms \ No newline at end of file +.PHONY: run-coms clean \ No newline at end of file diff --git a/lab4/src/shared.cpp b/lab4/src/shared.cpp new file mode 100644 index 0000000..268fbf3 --- /dev/null +++ b/lab4/src/shared.cpp @@ -0,0 +1,260 @@ +#include "important.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main(int argc, char *argv[]) { + + // if(argc != 3) + // { + // cout << "usage: ./a.out \n\n"; + // exit(0); + // } + + + // struct image_t *input_image = read_ppm_file(argv[1]); + // struct image_t *smoothened_image = S1_smoothen(input_image); + char buf; + pid_t CHILD; + + + CHILD = fork(); + if (CHILD == -1) { + perror("fork failed!"); + exit(EXIT_FAILURE); + } + + // Parent Process + if(CHILD != 0){ + + const char *name = "/parent-child"; + int shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); + if (shm_fd == -1) { + perror("shm_open"); + return 1; + } + + // Set the size of the shared memory region + // size_t size = smoothened_image->height * smoothened_image->width * 3; + size_t size = 1024; + + // Resize the shared memory object to the desired size + if (ftruncate(shm_fd, size) == -1) { + perror("ftruncate"); + return 1; + } + + // Map the shared memory object into the process address space + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (ptr == MAP_FAILED) { + perror("mmap"); + return 1; + } + + // Write data to the shared memory + sprintf((char *)ptr, "...PACKET..."); + + // Unmap the shared memory object + if (munmap(ptr, size) == -1) { + perror("munmap"); + return 1; + } + + // Close the shared memory file descriptor + if (close(shm_fd) == -1) { + perror("close"); + return 1; + } + + // Unlink the shared memory object + // if (shm_unlink(name) == -1) { + // perror("shm_unlink"); + // return 1; + // } + + + printf("parent writing to shm\n"); + wait(NULL); /* Wait for child */ + } + + // Child process + else if (CHILD == 0) { /* Child reads from pipe */ + + + pid_t GRAND_CHILD = fork(); + + if (GRAND_CHILD == -1) { + perror("fork failed!"); + exit(EXIT_FAILURE); + } + + // Child Process + if(GRAND_CHILD != 0){ + // close(pipe1[1]); /* Close unused write end for s1_s2 pipe */ + // close(pipe2[0]); /* Close unused read end for s2_s3 pipe */ + // char to_print[] = "C : x\n"; + // while (read(pipe1[0], &buf, 1) > 0) + // { + // printf("child reading from pipe\n"); + // to_print[4] = buf; + // write(STDOUT_FILENO, &to_print, strlen(to_print)); + // } + + printf("Child reading from shm...\n"); + + const char *name = "/parent-child"; + int shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); + if (shm_fd == -1) { + perror("shm_open"); + return 1; + } + + // Set the size of the shared memory region + size_t size = 1024; + + // Resize the shared memory object to the desired size + if (ftruncate(shm_fd, size) == -1) { + perror("ftruncate"); + return 1; + } + + // Map the shared memory object into the process address space + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (ptr == MAP_FAILED) { + perror("mmap"); + return 1; + } + + sleep(2); + + // Print the contents of the shared memory + char *packet = (char *)ptr; + printf("Shared memory contents: %s\n", packet); + + char buffer[1024]; + strncpy(buffer, packet, 1024); + + // Unmap the shared memory object + if (munmap(ptr, size) == -1) { + perror("munmap"); + return 1; + } + + // Close the shared memory file descriptor + if (close(shm_fd) == -1) { + perror("close"); + return 1; + } + + printf("child writing to shm\n"); + + const char *name2 = "/child-grandchild"; + int shm2_fd = shm_open(name2, O_CREAT | O_RDWR, 0666); + if (shm2_fd == -1) { + perror("shm_open"); + return 1; + } + + // Set the size of the shared memory region + // size_t size = smoothened_image->height * smoothened_image->width * 3; + // size_t size = 1024; + + // Resize the shared memory object to the desired size + if (ftruncate(shm2_fd, size) == -1) { + perror("ftruncate"); + return 1; + } + + // Map the shared memory object into the process address space + void *ptr2 = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm2_fd, 0); + if (ptr2 == MAP_FAILED) { + perror("mmap"); + return 1; + } + + // Write data to the shared memory + sprintf((char *)ptr2, buffer, 1024); + + // Unmap the shared memory object + if (munmap(ptr2, size) == -1) { + perror("munmap"); + return 1; + } + + // Close the shared memory file descriptor + if (close(shm2_fd) == -1) { + perror("close"); + return 1; + } + + + wait(NULL); + exit(EXIT_SUCCESS); + } + + + // GrandChild Process + else if (GRAND_CHILD == 0) { /* GrandChild reads from pipe */ + + printf("Grandchild reading from shm...\n"); + + const char *name = "/child-grandchild"; + int shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); + if (shm_fd == -1) { + perror("shm_open"); + return 1; + } + + // Set the size of the shared memory region + size_t size = 1024; + + // Resize the shared memory object to the desired size + if (ftruncate(shm_fd, size) == -1) { + perror("ftruncate"); + return 1; + } + + // Map the shared memory object into the process address space + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (ptr == MAP_FAILED) { + perror("mmap"); + return 1; + } + + sleep(2); + + // Print the contents of the shared memory + printf("Shared memory contents: %s\n", (char *)ptr); + + // Unmap the shared memory object + if (munmap(ptr, size) == -1) { + perror("munmap"); + return 1; + } + + // Close the shared memory file descriptor + if (close(shm_fd) == -1) { + perror("close"); + return 1; + } + + exit(EXIT_SUCCESS); + + } + } + + cout << "success" << endl; + return 0; +} \ No newline at end of file diff --git a/lab4/src/shm1.cpp b/lab4/src/shm1.cpp index fa004a1..977185a 100644 --- a/lab4/src/shm1.cpp +++ b/lab4/src/shm1.cpp @@ -29,7 +29,7 @@ int main() { } // Write data to the shared memory - sprintf(ptr, "Hello, shared memory!"); + sprintf((char *)ptr, "Hello, shared memory!"); // Unmap the shared memory object if (munmap(ptr, size) == -1) {