Memory segments

Below are two codes. First, a process is created that creates a common segment and opens access to it for everyone.

Second - reads data from the shared segment

Using the digSum function, I get the result 1 (which is correct according to the assignment). As I understand it, I have to transfer this result to the second program, but only "Hello world" is passed. What did I get wrong?

1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <fstream>
#include <iostream>

#define SHMEM_SIZE 4096
#define SH_MESSAGE "Hello World!\n"


int digSum(int n) {
       int sum = 0;
       while(n > 0 || sum > 9) {
            if(n == 0) {
              n = sum;
              sum = 0;
            }
            sum += n % 10;
            n /= 10;
        }
 return sum;
}

int main () {
        int shm_id;
        char * shm_buf;
        int shm_size;
        struct shmid_ds ds;

        shm_id = shmget (IPC_PRIVATE, SHMEM_SIZE, IPC_CREAT | IPC_EXCL | 0600);
        if (shm_id == -1) {
        std::cerr << "shmget() error" << std::endl;
        return 1;
        }

        shm_buf = (char *) shmat (shm_id, NULL, 0);
        if (shm_buf == (char *) -1) {
           std::cerr << "shmat() error" << std::endl;
           return 1;
        }

        shmctl (shm_id, IPC_STAT, &ds);
        shm_size = ds.shm_segsz;
        if (shm_size < strlen (SH_MESSAGE)) {
           std::cerr << "Error: segsize= " << shm_size << std::endl;
           return 1;
        }

        strcpy (shm_buf, SH_MESSAGE);
        std::cout << "ID: " << shm_id << std::endl;

        int n = 1234;
        std::cout << "Number: " << n << std::endl;
        std::cout << "Answer: " << digSum(n) << std::endl;

        std::cout << "Press <Enter> to exit";
        fgetc (stdin);

        shmdt (shm_buf);
        shmctl (shm_id, IPC_RMID, NULL);
        return 0;
}


2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int main(int argc, char** argv) {
        int shm_id;
        char* shm_buf;

        if (argc < 2) {
           std::cerr << "Too few arguments" << std::endl;
           return 1;
        }

        shm_id = atoi (argv[1]);
        shm_buf = (char*) shmat (shm_id, 0, 0);

        if (shm_buf == (char*)-1) {
        std::cerr << "shmat() error" << std::endl;
        return 1;
        }

        std::cout << "Message: " <<  shm_buf << std::endl;
        shmdt (shm_buf);
        return 0;
}
Last edited on
All you copy to the shared memory is "Hello World!\n" and nothing else. You compute an integer value and display it on the first program's screen, but never put it in the shared memory.
You compute an integer value and display it on the first program's screen, but never put it in the shared memory

How to fix it?
Topic archived. No new replies allowed.