Compare commits

...

2 Commits

Author SHA1 Message Date
ItsMAX0112 032891872f Merge branch 'main' of https://gitea.iitdh.ac.in/220010001/OS-Labs 2024-11-03 19:31:07 +05:30
ItsMAX0112 7b85f8155a cant think of a good commit msg 2024-11-03 19:30:43 +05:30
9 changed files with 143 additions and 202 deletions

View File

@ -5,6 +5,7 @@
"iostream": "cpp", "iostream": "cpp",
"iosfwd": "cpp", "iosfwd": "cpp",
"fstream": "cpp", "fstream": "cpp",
"xlocmes": "cpp" "xlocmes": "cpp",
"utility": "cpp"
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +1,14 @@
SHMDIR = sharedMemory
shm_build: $(SHMDIR)/shared.cpp
g++ -g $(SHMDIR)/shared.cpp $(SHMDIR)/important.cpp -o $(SHMDIR)/shared.out
shm_run: shm_build
$(SHMDIR)/shared.out ../images/$(INPUT).ppm ../images/$(INPUT)_out.ppm
# ----- OLD MAKEFILE CMDs -----------------
build-sharpen: ./a.out build-sharpen: ./a.out
./a.out: image_sharpener.cpp libppm.cpp ./a.out: image_sharpener.cpp libppm.cpp

View File

@ -6,95 +6,76 @@
using namespace std; using namespace std;
struct image_t *S1_smoothen(struct image_t *input_image) float *S1_smoothen(struct image_t *input_image)
{ {
// cout << input_image->width << input_image->height << " " << input_image->image_pixels << endl;
int width = input_image->width; int width = input_image->width;
int height = input_image->height; int height = input_image->height;
// memory allocation // memory allocation
struct image_t *smoother = new struct image_t; float *smoother = (float *)malloc(height * width * 3 * sizeof(float));
smoother->height = height; long ind = 0;
smoother->width = width;
smoother->image_pixels = new uint8_t **[height];
for (int i = 0; i < height; i++) for (int i = 0; i < height; i++)
{ {
smoother->image_pixels[i] = new uint8_t *[width];
for (int j = 0; j < width; j++) for (int j = 0; j < width; j++)
smoother->image_pixels[i][j] = new uint8_t[3];
}
for (int i = 1; i < height - 1; i++)
{
for (int j = 1; j < width - 1; j++)
{ {
for (int k = 0; k < 3; k++) for (int k = 0; k < 3; k++)
{ {
// image->image_pixels[i][j][k] = val;
// edge cases // edge cases
if (i == 0 && j == 0) if (i == 0 && j == 0)
{ // i-1 and j - 1 doesnt exist { // i-1 and j - 1 doesnt exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4 + input_image->image_pixels[i + 1][j + 1][k] / 4); smoother[ind] = (float)(input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4 + input_image->image_pixels[i + 1][j + 1][k] / 4);
} }
else if (i == height - 1 && j == 0) else if (i == height - 1 && j == 0)
{ // i+1 and j-1 does not exist { // i+1 and j-1 does not exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i - 1][j + 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4); smoother[ind] = (float)(input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i - 1][j + 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i][j + 1][k] / 4);
} }
else if (i == 0 && j == width - 1) else if (i == 0 && j == width - 1)
{ // i-1 and j+1 does not exist { // i-1 and j+1 does not exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i + 1][j - 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4); smoother[ind] = (float)(input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4 + input_image->image_pixels[i + 1][j - 1][k] / 4 + input_image->image_pixels[i + 1][j][k] / 4);
} }
else if (i == height - 1 && j == width - 1) else if (i == height - 1 && j == width - 1)
{ // i+1 and j+1 does not exist { // i+1 and j+1 does not exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 4 + input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4); smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 4 + input_image->image_pixels[i - 1][j][k] / 4 + input_image->image_pixels[i][j - 1][k] / 4 + input_image->image_pixels[i][j][k] / 4);
} }
else if (i == 0) else if (i == 0)
{ // i - 1 does not exist { // i - 1 does not exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6); smoother[ind] = (float)(input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6);
} }
else if (j == 0) else if (j == 0)
{ // j -1 does not exist { // j -1 does not exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6); smoother[ind] = (float)(input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6 + input_image->image_pixels[i + 1][j + 1][k] / 6);
} }
else if (i == height - 1) else if (i == height - 1)
{ // i+1 does not exist { // i+1 does not exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6); smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i - 1][j + 1][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i][j + 1][k] / 6);
} }
else if (j == width - 1) else if (j == width - 1)
{ // j + 1 does not exist { // j + 1 does not exist
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6); smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 6 + input_image->image_pixels[i - 1][j][k] / 6 + input_image->image_pixels[i][j - 1][k] / 6 + input_image->image_pixels[i][j][k] / 6 + input_image->image_pixels[i + 1][j - 1][k] / 6 + input_image->image_pixels[i + 1][j][k] / 6);
} }
else else
{ {
smoother->image_pixels[i][j][k] = (input_image->image_pixels[i - 1][j - 1][k] / 9 + input_image->image_pixels[i - 1][j][k] / 9 + input_image->image_pixels[i - 1][j + 1][k] / 9 + input_image->image_pixels[i][j - 1][k] / 9 + input_image->image_pixels[i][j][k] / 9 + input_image->image_pixels[i][j + 1][k] / 9 + input_image->image_pixels[i + 1][j - 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9 + input_image->image_pixels[i + 1][j + 1][k] / 9); smoother[ind] = (float)(input_image->image_pixels[i - 1][j - 1][k] / 9 + input_image->image_pixels[i - 1][j][k] / 9 + input_image->image_pixels[i - 1][j + 1][k] / 9 + input_image->image_pixels[i][j - 1][k] / 9 + input_image->image_pixels[i][j][k] / 9 + input_image->image_pixels[i][j + 1][k] / 9 + input_image->image_pixels[i + 1][j - 1][k] / 9 + input_image->image_pixels[i + 1][j][k] / 9 + input_image->image_pixels[i + 1][j + 1][k] / 9);
} }
ind++;
} }
} }
} }
return smoother; return smoother;
} }
struct image_t *S2_find_details(struct image_t *input_image, struct image_t *smoothened_image) float *S2_find_details(struct image_t *input_image, float *smoothened_image)
{ {
// TODO // TODO
int width = input_image->width; int width = input_image->width;
int height = input_image->height; int height = input_image->height;
struct image_t *details = new struct image_t; float *details = (float *)malloc(height * width * 3 * sizeof(float));
details->height = height; long ind = 0;
details->width = width;
details->image_pixels = new uint8_t **[height];
for (int i = 0; i < height; i++)
{
details->image_pixels[i] = new uint8_t *[width];
for (int j = 0; j < width; j++)
details->image_pixels[i][j] = new uint8_t[3];
}
for (int i = 1; i < height - 1; i++) for (int i = 1; i < height - 1; i++)
{ {
@ -102,7 +83,8 @@ struct image_t *S2_find_details(struct image_t *input_image, struct image_t *smo
{ {
for (int k = 0; k < 3; k++) for (int k = 0; k < 3; k++)
{ {
details->image_pixels[i][j][k] = max(0, input_image->image_pixels[i][j][k] - smoothened_image->image_pixels[i][j][k]); details[ind] = max((float)0, (float)input_image->image_pixels[i][j][k] - smoothened_image[ind]);
ind++;
} }
} }
} }
@ -110,33 +92,25 @@ struct image_t *S2_find_details(struct image_t *input_image, struct image_t *smo
return details; return details;
} }
struct image_t *S3_sharpen(struct image_t *input_image, struct image_t *details_image) float *S3_sharpen(struct image_t *input_image, float *details_image)
{ {
// TODO // TODO
int width = input_image->width; int width = input_image->width;
int height = input_image->height; int height = input_image->height;
int a = 1; float a = 0.8;
struct image_t *sharp = new struct image_t; float *sharp = (float *)malloc(height * width * 3 * sizeof(float));
sharp->height = height;
sharp->width = width;
sharp->image_pixels = new uint8_t **[height];
for (int i = 0; i < height; i++)
{
sharp->image_pixels[i] = new uint8_t *[width];
for (int j = 0; j < width; j++)
sharp->image_pixels[i][j] = new uint8_t[3];
}
long ind = 0;
for (int i = 1; i < height - 1; i++) for (int i = 1; i < height - 1; i++)
{ {
for (int j = 1; j < width - 1; j++) for (int j = 1; j < width - 1; j++)
{ {
for (int k = 0; k < 3; k++) for (int k = 0; k < 3; k++)
{ {
sharp->image_pixels[i][j][k] = min(255, input_image->image_pixels[i][j][k] + details_image->image_pixels[i][j][k]) * a; sharp[ind] = min((float)255, (float)input_image->image_pixels[i][j][k] + a * details_image[ind]);
ind++;
} }
} }
} }

View File

@ -9,9 +9,9 @@ struct image_t
uint8_t*** image_pixels; uint8_t*** image_pixels;
}; };
struct image_t* S1_smoothen(struct image_t *input_image); float* S1_smoothen(struct image_t *input_image);
struct image_t* S2_find_details(struct image_t *input_image, struct image_t *smoothened_image); float* S2_find_details(struct image_t *input_image, float *smoothened_image);
struct image_t* S3_sharpen(struct image_t *input_image, struct image_t *details_image); float* S3_sharpen(struct image_t *input_image, float *details_image);
struct image_t* read_ppm_file(char* path_to_input_file); struct image_t* read_ppm_file(char* path_to_input_file);
void write_ppm_file(char* path_to_output_file, struct image_t* image); void write_ppm_file(char* path_to_output_file, struct image_t* image);

View File

@ -18,19 +18,40 @@ using namespace std;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// if(argc != 3) if(argc != 3)
// { {
// cout << "usage: ./a.out <path-to-original-image> <path-to-transformed-image>\n\n"; cout << "usage: ./a.out <path-to-original-image> <path-to-transformed-image>\n\n";
// exit(0); exit(0);
// } }
// struct image_t *input_image = read_ppm_file(argv[1]); struct image_t *input_image = read_ppm_file(argv[1]);
int height = input_image->height;
int width = input_image->width;
struct image_t *sharpened_image = new struct image_t;
sharpened_image->height = height;
sharpened_image->width = width;
sharpened_image->image_pixels = new uint8_t**[sharpened_image->height];
for(int i = 0; i < sharpened_image->height; i++){
sharpened_image->image_pixels[i] = new uint8_t*[sharpened_image->width];
for(int j = 0; j < sharpened_image->width; j++){
sharpened_image->image_pixels[i][j] = new uint8_t[3];
}
}
float *sharp;
const char *shm1_name = "/shm_parent_child"; const char *shm1_name = "/shm_parent_child";
const char *shm2_name = "/shm_child_grandchild"; const char *shm2_name = "/shm_child_grandchild";
const char *sem1_name = "/sem_parent_child"; const char *sem1_name = "/sem_parent_child";
const char *sem2_name = "/sem_child_grandchild"; const char *sem2_name = "/sem_child_grandchild";
size_t size = input_image->height * input_image->width * 3 * sizeof(float);
// size_t size = 1024;
int reps=1000;
// ---------------------------------------------------------------------------------
int shm_fd = shm_open(shm1_name, O_CREAT | O_RDWR, 0666); int shm_fd = shm_open(shm1_name, O_CREAT | O_RDWR, 0666);
if (shm_fd == -1) { if (shm_fd == -1) {
perror("shm_open"); perror("shm_open");
@ -44,12 +65,33 @@ int main(int argc, char *argv[]) {
} }
// Map the shared memory object into the process address space // Map the shared memory object into the process address space
void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); float *ptr = (float*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) { if (ptr == MAP_FAILED) {
perror("mmap"); perror("mmap");
return 1; return 1;
} }
// -----------------------------------------------------------------------------------
int shm2_fd = shm_open(shm2_name, O_CREAT | O_RDWR, 0666);
if (shm2_fd == -1) {
perror("shm_open");
return 1;
}
// 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
float *ptr2 = (float*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm2_fd, 0);
if (ptr2 == MAP_FAILED) {
perror("mmap");
return 1;
}
// --------------------------------------------------------------------------------------
sem_t *sem_parent_child = sem_open(sem1_name, O_CREAT, 0666, 0); // Initially locked (0) sem_t *sem_parent_child = sem_open(sem1_name, O_CREAT, 0666, 0); // Initially locked (0)
sem_t *sem_child_grandchild = sem_open(sem2_name, O_CREAT, 0666, 0); // Initially locked (0) sem_t *sem_child_grandchild = sem_open(sem2_name, O_CREAT, 0666, 0); // Initially locked (0)
@ -59,9 +101,6 @@ int main(int argc, char *argv[]) {
} }
// size_t size = input_image->height * input_image->width * 3 * sizeof(uint8_t);
size_t size = 1024;
pid_t CHILD; pid_t CHILD;
CHILD = fork(); CHILD = fork();
if (CHILD == -1) { if (CHILD == -1) {
@ -71,37 +110,14 @@ int main(int argc, char *argv[]) {
// Parent Process // Parent Process
if(CHILD != 0){ if(CHILD != 0){
char buffer[1024] = {'p', 'a', 'c', 'k', 'e', 't', '\0'};
buffer[7] = '\0';
// Write data to the shared memory // Write data to the shared memory
for(int i = 1; i < 5; i++){ for(int i = 0; i < reps; i++){
// printf("parent writing to shm\n");
buffer[6] = '0' + i;
memcpy(ptr, buffer, 1024); // Copy 1024 bytes to shared memory
// Optional: Print what is being written to shared memory for verification float* smoothImage = S1_smoothen(input_image);
printf("P - Writing to shared memory: %s\n", (char *)ptr); memcpy(ptr, smoothImage, size);
sem_post(sem_parent_child); sem_post(sem_parent_child);
sem_wait(sem_child_grandchild);
}
// 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;
}
sem_close(sem_parent_child);
sem_close(sem_child_grandchild);
wait(NULL); /* Wait for child */
} }
// Child process // Child process
@ -115,135 +131,74 @@ int main(int argc, char *argv[]) {
// Child Process // Child Process
if(GRAND_CHILD != 0){ if(GRAND_CHILD != 0){
int shm_fd = shm_open(shm1_name, O_CREAT | O_RDWR, 0666);
if (shm_fd == -1) {
perror("shm_open");
return 1;
}
// Resize the shared memory object to the desired size for(int i = 0; i < reps; i++){
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;
}
int shm2_fd = shm_open(shm2_name, O_CREAT | O_RDWR, 0666);
if (shm2_fd == -1) {
perror("shm_open");
return 1;
}
// 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;
}
for(int i=0; i<5; i++){
// printf("Child reading from shm1 and writing to shm2...\n");
sem_wait(sem_parent_child); sem_wait(sem_parent_child);
// Print the contents of the shared memory float *details = S2_find_details(input_image, ptr);
char *packet = (char *)ptr; memcpy(ptr2, details, 1024);
printf("C - Shared memory contents: %s\n", packet); sem_post(sem_child_grandchild); // Signal grandchild
// char buffer[1024];
// strncpy(buffer, (char *)ptr, 1024);
memcpy(ptr2, ptr, 1024);
sem_post(sem_child_grandchild); // Signal parent
}
// Unmap the shared memory object
if (munmap(ptr, size) == -1) {
perror("munmap");
return 1;
} }
exit(EXIT_SUCCESS);
// Close the shared memory file descriptor
if (close(shm_fd) == -1) {
perror("close");
return 1;
}
// printf("child writing to shm\n");
// 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;
}
shm_unlink(shm1_name);
sem_close(sem_parent_child);
sem_close(sem_child_grandchild);
wait(NULL);
exit(EXIT_SUCCESS);
} }
// GrandChild Process // GrandChild Process
else if (GRAND_CHILD == 0) { /* GrandChild reads from pipe */ else if (GRAND_CHILD == 0) {
int shm2_fd = shm_open(shm2_name, O_CREAT | O_RDWR, 0666);
if (shm2_fd == -1) {
perror("shm_open");
return 1;
}
// Resize the shared memory object to the desired size printf("GC reading...\n");
if (ftruncate(shm2_fd, size) == -1) { for(int i = 0; i < reps; i++){
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;
}
for(int i=0; i<5; i++){
sem_wait(sem_child_grandchild); sem_wait(sem_child_grandchild);
// Print the contents of the shared memory sharp = S3_sharpen(input_image, ptr2);
// printf("Grandchild reading from shm...\n");
printf("GC - Shared memory contents: %s\n", (char *)ptr2);
}
// Unmap the shared memory object
if (munmap(ptr2, size) == -1) {
perror("munmap");
return 1;
} }
printf("GC reading done...\n");
// Close the shared memory file descriptor
if (close(shm2_fd) == -1) {
perror("close");
return 1;
}
shm_unlink(shm2_name);
sem_close(sem_parent_child);
sem_close(sem_child_grandchild);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
} }
wait(NULL);
wait(NULL);
int ind=0;
for(int i = 0; i < sharpened_image->height; i++){
for(int j = 0; j < sharpened_image->width; j++){
for(int k = 0; k < 3; k++){
sharpened_image->image_pixels[i][j][k] = sharp[ind++];
}
}
}
printf("Writing output file...\n");
write_ppm_file(argv[2], sharpened_image);
// 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;
}
// 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;
}
shm_unlink(shm1_name);
shm_unlink(shm2_name);
sem_close(sem_parent_child); sem_close(sem_parent_child);
sem_close(sem_child_grandchild); sem_close(sem_child_grandchild);
sem_unlink(sem1_name); sem_unlink(sem1_name);