多进程应用使用场景
用户可以通过在内存一致性动态检查命令devkit advisor dr-check之后,添加-em/--enable-multiprocess参数的方式,实现对共享内存模式的多进程应用的检测。
多进程应用使用示例
- 编译源码文件,生成可执行二进制文件。
1g++ test_1.cpp -o test_1 -g -lpthread
test_1.cpp文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include <iostream> #include <cstdlib> #include <unistd.h> #include <thread> #include <sys/shm.h> #include <sys/wait.h> #define SHM_SIZE 1024 #define ITERATIONS 1000 // 共享内存结构 struct ShmData { int counter; // 多进程竞争的共享变量 }; // 进程内的全局变量(线程间竞争) int thread_shared_counter = 0; // 线程函数(修改进程内变量) void thread_func() { for (int i = 0; i < ITERATIONS; i++) { thread_shared_counter++; // 无锁保护 } } // 创建并运行线程的函数 void run_threads() { std::thread t1(thread_func); std::thread t2(thread_func); t1.join(); t2.join(); } // 修改共享内存的函数 void modify_shared_memory(ShmData* shm) { for (int i = 0; i < ITERATIONS; i++) { shm->counter++; // 无信号量保护 } } // 子进程的工作函数 void child_process_work(ShmData* shm) { // 创建并运行线程 run_threads(); // 修改共享内存 modify_shared_memory(shm); std::cout << "Child Process: thread_shared_counter=" << thread_shared_counter << ", shm->counter=" << shm->counter << std::endl; shmdt(shm); exit(0); } // 父进程的工作函数 void parent_process_work(ShmData* shm, int shmid) { // 创建并运行线程 run_threads(); // 修改共享内存 modify_shared_memory(shm); wait(nullptr); // 等待子进程结束 std::cout << "Parent Process: thread_shared_counter=" << thread_shared_counter << ", shm->counter=" << shm->counter << std::endl; // 清理资源 shmdt(shm); shmctl(shmid, IPC_RMID, nullptr); // 删除共享内存 } int main() { // 创建共享内存 int shmid = shmget(IPC_PRIVATE, sizeof(ShmData), IPC_CREAT | 0666); ShmData* shm = static_cast<ShmData*>(shmat(shmid, nullptr, 0)); shm->counter = 0; // 创建子进程 pid_t pid = fork(); if (pid == 0) { child_process_work(shm); } else { parent_process_work(shm, shmid); } return 0; }
- 执行内存一致性动态检查。
devkit advisor dr-check -f /home/demo/test_1 -o /home/demo/output -i /home/demo -em true
返回信息如下,从返回信息中的加粗信息可以看出有4行代码推荐修改。其中multi-thread races为多线程报告,多进程的线程间报告会输出到同一份文件中;multi-process races为多进程报告。
Executing dynamic memory consistency check task, please wait... The pid of the memtracer process is 2996270 Scanned time: 2025/11/14 02:47:37 Configuration: ELF filepath: /home/demo/test_1 Scan source code path: /home/demo Generate report path: /home/demo/output Generate report type: all Task Timeout Interval: The timeout period is not set. Log level: info Summary: There are 4 recommended code lines to modify. For the detailed information on multi-thread races, please check: /home/demo/output/dr-check_test_1_20251114024737_5f2d.json /home/demo/output/dr-check_test_1_20251114024737_5f2d.html /home/demo/output/dr-check_test_1_20251114024737_5f2d.csv For the detailed information on multi-process races, please check: /home/demo/output/dr-check-mp_20251114024737_c342.html
- /home/demo/test_1:ELF文件路径。
- /home/demo/output:扫描报告的存放路径。
- /home/demo:ELF文件对应的源码文件夹路径。
- 查看多线程和多进程报告中存在冲突的函数名称。图1 多线程报告
从多线程报告中可以看出,存在冲突的函数为thread_func。
图2 多进程报告
从多进程报告中可以看出,存在冲突的函数为modify_shared_memory。
- 通过nm命令可查看冲突函数thread_func和modify_shared_memory函数重载后名称。
1nm test_1回显信息如下:
1 2
0000000000401004 T _Z11thread_funcv 00000000004010d0 T _Z20modify_shared_memoryP7ShmData
- 创建配置文件safe_functions.cfg,写入内容。
1 2
_Z11thread_funcv _Z20modify_shared_memoryP7ShmData
- 再次执行内存一致性动态检查。
devkit advisor dr-check -f /home/demo/test_1 -o /home/demo/output -i /home/demo -s /home/demo/safe_functions.cfg
返回信息如下,从返回信息中的加粗信息可以看出无代码行需要修改。
Executing dynamic memory consistency check task, please wait... The pid of the memtracer process is 3000609 Scanned time: 2025/11/14 02:59:33 Configuration: ELF filepath: /home/demo/test_1 Scan source code path: /home/demo Generate report path: /home/demo/output Generate report type: all Task Timeout Interval: The timeout period is not set. Log level: info Summary: There are 0 recommended code lines to modify. For the detailed information on multi-thread races, please check: /home/demo/output/dr-check_test_1_20251114025933_abd9.json /home/demo/output/dr-check_test_1_20251114025933_abd9.html /home/demo/output/dr-check_test_1_20251114025933_abd9.csv
父主题: 内存一致性动态检查