still working in shm

This commit is contained in:
ItsMAX0112 2024-10-22 02:14:43 +05:30
parent 7fe01720af
commit 06c3b4e2ac
4 changed files with 265 additions and 3 deletions

View File

@ -3,6 +3,8 @@
"ostream": "cpp",
"chrono": "cpp",
"iostream": "cpp",
"iosfwd": "cpp"
"iosfwd": "cpp",
"fstream": "cpp",
"xlocmes": "cpp"
}
}

View File

@ -15,4 +15,4 @@ run-coms: ./coms.out
clean:
rm a.out coms.out
.PHONY: run-coms
.PHONY: run-coms clean

260
lab4/src/shared.cpp Normal file
View File

@ -0,0 +1,260 @@
#include "important.h"
#include <iostream>
#include <cstdint>
#include <chrono>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<fcntl.h>
using namespace std;
int main(int argc, char *argv[]) {
// if(argc != 3)
// {
// cout << "usage: ./a.out <path-to-original-image> <path-to-transformed-image>\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;
}

View File

@ -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) {