Shared Memory With Two Processes In C?

后端 未结 4 1503
鱼传尺愫
鱼传尺愫 2021-01-03 07:42

I want to do the following:

Parent process creates a child process. Then the child process reads n int\'s from the user and store them in a shared memory. The parent

4条回答
  •  轮回少年
    2021-01-03 08:21

    One problem is that the child process is attempting to use get the shared memory before it has been created by the parent. The parent has a wait() call before creating the shared memory, so it won't exist when the client tries to retrieve the id. Even if the wait() call is moved, it may not work because there is a race condition. The call to shmget may need to precede the fork call (or use some synchronization to make sure it actually exists before retrieving it in the child process).

    And (as others have already pointed out), the child is attempting to write integers to the memory while the reading (printing of it) tries to treat it as a character string.

提交回复
热议问题