memory region, with the child process writing a value to the shared memory and the parent process reading that value.  This is my code and I wont run. Can you help:  #include #include #include #include #include #include

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Description:
The project I am working on needs to demonstrate how two processes (parent and child) can communicate
through a shared memory region, with the child process writing a value to the shared memory
and the parent process reading that value. 

This is my code and I wont run. Can you help: 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

#define SHM_SIZE 1024  /* shared memory size */

void error_exit(const char *msg) {
    perror(msg);
    exit(EXIT_FAILURE);
}

int main() {
    key_t key;
    int shmid;
    char *data;

    // Create a unique key for shared memory
    if ((key = ftok("shared_memory.c", 'R')) == -1) {
        error_exit("ftok");
    }

    // Create the shared memory segment
    if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
        error_exit("shmget");
    }

    // Attach the shared memory segment to our data space
    data = shmat(shmid, (void *)0, 0);
    if (data == (char *)(-1)) {
        error_exit("shmat");
    }

    // Fork a child process
    pid_t pid = fork();
    if (pid < 0) {
        error_exit("fork");
    }
    else if (pid == 0) {  // Child process
        *data = 0xDEADBEEF;  // Write to shared memory
    }
    else {  // Parent process
        wait(NULL);  // Wait for the child to finish

        // Read from the shared memory
        if (*data == 0xDEADBEEF) {
            printf("The child wrote 0xDEADBEEF\n");
        }
    }

    // Detach from the shared memory
    if (shmdt(data) == -1) {
        error_exit("shmdt");
    }

    // Clean up shared memory
    if (shmctl(shmid, IPC_RMID, NULL) == -1) {
        error_exit("shmctl");
    }

    return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Types of System Design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education