Notes on using shared memory

Development Platform

Operating system: Kylin 3.3 (CentOS 7 kernel)
IDE: VS Code (remote development and debugging)
Language: C/C++

Cause of the problem

Due to project requirements, two C++ programs (hereinafter referred to as A and B) need to be modified. Inter-process communication uses shared memory. However, when compiling the code, two of the definitions were commented out because of constant redefinition, which eventually led to inconsistent data structure sizes, and then caused the following two weird problems:

  1. The same KEY of program A cannot open the shared memory created by program B. Restarting the virtual machine several times will work, and the probability of occurrence is low.
  2. The data written by program A is different from that read by program B.

Troubleshooting process

Under the guidance of the boss, the following investigations were done

  1. Determine whether the structure is aligned (#pragma pack directive)
  2. sizeof confirms whether the size of the structure is consistent

fix

Restore the commented constants and recompile.

Summarize

When you haven't fully read other people's code, don't change it randomly.

reference link

structure alignment

Guess you like

Origin blog.csdn.net/qq_38189542/article/details/116261020