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.
Running live55 under linux shows the figure: not getting ip: ip is 0.0.0.0 live555 does not know th
Today I made a simple ftp server for the company, and it was the first time I actually did the serv
Record your favorite Linux system commands for easy reference later, and find that memory is getting
I. Manually compile and install 1, download the installation package, wget https://ohse.de/uwe/rele
Create users under Centos5 and add sudo privileges to users
Three steps to teach you how to create a Linux flash drive
File system loading during Linux boot process
Problem with installing vmware tool on Fedora Core6
Shell regular expressions use detailed explanation
Linux mount command system mount and image processing
Linux firewall configuration - basic articles
How to view and modify file read and write permissions under Linux system
Cron plan task configuration method in linux (centos)
Preparation before Linux system installation
Linux station group rsync synchronization basic tutorial
I don't know how to choose the advantages and disadvantages of various Linux distributions
Four ways to safely remove Windows XP SP2
Win8 uses the computer management function tutorial
Win8 system allocation access restrictions setting skills
How to access the disk space of Win8.1 Update with one click?
Windows system is not subject to virus violations. How to set
Win10 preview version 10122 update content Daquan
How to set the screen saver under Win10 (referred to as screen saver)
Win8 application sergeants said that it was time to go to
Novice Academy: Installing two hard drives for the Linux operating system
How to partition Win10 disk? Steps for Win10 Hard Disk Partition