0

Im just trying to share an integer between two processes, but the memory segment is initialized in program 1 and is used in program 4. Here is the initialization in program 1:

shmid = shmget(key, sizeof(int*), 0666 | IPC_CREAT);
int *data = (int *)shmat(shmid, (void*)0,0);

Here I get a warning of "cast to pointer from integer of different size". Argh.

Simple, I'm assuming, but I'm a big time noob with IPC. And many other things in c....

Then I pass it to another program:

snprintf(shmarg, sizeof(shmarg), "%n", data);
pid_t pid3 = run_cmd4("/home/tropix/hw11-4", shmarg, semarg, pipe_from_p2_2, pipe_to_p5_2);

Not sure how to access it on the other side though. How can I get the int back in Program 4?

2 Answers 2

4

Pretty sure you're missing the header that defines shmat() and thus the compiler is guessing that shmat returns an int (not a pointer).

Sign up to request clarification or add additional context in comments.

Comments

1

Here I get a warning of "cast to pointer from integer of different size". Argh.

Try using NULL instead of (void*)0.

Then I pass it to another program:

snprintf(shmarg, sizeof(shmarg), "%n", data);
pid_t pid3 = run_cmd4("/home/tropix/hw11-4", shmarg,
                      semarg, pipe_from_p2_2, pipe_to_p5_2); 

Not sure how to access it on the other side though. How can I get the int back in Program 4?

The pointer itself isn't useful unless the shared memory area happens to be loaded at the exact same virtual memory address in the other program: you probably don't even want to try to arrange that - instead, let the other program (hw11-4) load the shared memory segment and let the OS choose the virtual memory address, then simply look at that address for the int. So, hw11-4 needs to be passed the same shared memory key (e.g. as a command line argument) and itself use shmget to open and get the key for, then shmat to map, the shared memory segment into memory....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.