Shared storage area communication instance and analysis

  

When reading apue, the shared storage area is not used for instance. For this reason, I wrote a small program, the program roughly creates the child process, and the child process receives the string from the terminal. In the shared area, the parent process reads the string from the shared area and outputs it. At the same time, both the parent and child processes output the interval range of the respective shared area.

#include "apue.h"

2. #include <sys/shm.h>

3. #define SHM_SIZE 100000//Shared Storage Area Length

4. #define SHM_MODE 0600//Shared Storage Default Access Permission

5. int

6. main(void)

7. {

8. int shmid, pid;

9. char* shmptr;

10. key_t key;

11. if(key=ftok( "sharem.c",1)==-1) //Use ftok to create the corresponding key

12. printf("ftok error\ ");

13. if ((shmid=shmget(key,SHM_SIZE,IPC_CREAT| SHM_MODE))==-1)

14. printf("shmget error\ ");

15. if((pid=fork())<0)

16. printf("fork error\ ");

17. else if(pid==0)//subprocess, receive string from terminal and store it in shared storage area

18. {

19. if((shmptr=shmat(shmid,0,0))==(void*)-1)

20. printf(" Child shmat error\ ");

21. printf("child share memory attached from %lx to %lx\ ",(unsigned long)shmptr,(unsigned long)shmptr+SHM_SIZE);//output shared memory area address range in the child process

22. printf("child input:");

23. scanf("%s",shmptr);

24. exit(0);

25. }

26. else//parent, read string output from shared storage area to terminal

27. {

28. sleep(5);

29. if((shmptr=shmat(shmid,0,0))==(void*)-1)

30. printf("parent shmat error\ ");

31. printf("parent share memory attached from %lx to %lx\ ",(unsigned long) Shmptr, (unsigned long) shmptr+SHM_SIZE); //output shared storage area Address range in the parent process

32. printf("parent output:");

33. printf("%s\ ",shmptr);

34. }

35. exit(0);

36. }

37. //Note: This program is not used in order to be simple. The semaphore or record lock guarantees synchronous access to the shared area. Instead, the parent process waits for 5 seconds to ensure that the child process executes first.


Running results: Analysis: A total of results can be found, the shared sub-area address of the parent and child processes are the same, according to the apue description, here may use the "anonymous storage mapping" technology, However, you cannot think that the shared storage area has the same address in different processes, which is only in the parent and child processes. In different processes, the shared area may be connected to a different address, so when the pointer type is stored in the shared area, the actual physical address is never stored. Instead, the pointer value is set to the offset of another object in the segment. The offset is the actual address of the object being referenced minus the starting address of the shared memory area. In addition: by executing the ipcs command, you can find that when the program ends, the shared storage area is still there.

Copyright © Windows knowledge All Rights Reserved