Multi-Process Applications
You can append the -em/--enable-multiprocess parameter to the devkit advisor dr-check command to enable dynamic memory consistency check for multi-process applications running in shared memory mode.
Procedure
- Compile the source file to generate an executable binary file.
1g++ test_1.cpp -o test_1 -g -lpthread
Content of the test_1.cpp file:
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 // Shared memory structure struct ShmData { int counter; // Shared variable used by multiple processes }; // Global variables in a process (shared among threads) int thread_shared_counter = 0; // Thread function (modifying variables in a process) void thread_func() { for (int i = 0; i < ITERATIONS; i++) { thread_shared_counter++; // No lock protection } } // Function of creating and running threads void run_threads() { std::thread t1(thread_func); std::thread t2(thread_func); t1.join(); t2.join(); } // Function of modifying the shared memory void modify_shared_memory(ShmData* shm) { for (int i = 0; i < ITERATIONS; i++) { shm->counter++; // No semaphore protection } } // Working function of subprocesses void child_process_work(ShmData* shm) { // Create and run a thread. run_threads(); // Modify the shared memory. modify_shared_memory(shm); std::cout << "Child Process: thread_shared_counter=" << thread_shared_counter << ", shm->counter=" << shm->counter << std::endl; shmdt(shm); exit(0); } // Working function of the parent process void parent_process_work(ShmData* shm, int shmid) { // Create and run a thread. run_threads(); // Modify the shared memory. modify_shared_memory(shm); wait(nullptr); // Wait for the child process to end. std::cout << "Parent Process: thread_shared_counter=" << thread_shared_counter << ", shm->counter=" << shm->counter << std::endl; // Clear resources. shmdt(shm); shmctl(shmid, IPC_RMID, nullptr); // Delete the shared memory. } int main() { // Create a shared memory. int shmid = shmget(IPC_PRIVATE, sizeof(ShmData), IPC_CREAT | 0666); ShmData* shm = static_cast<ShmData*>(shmat(shmid, nullptr, 0)); shm->counter = 0; // Create a subprocess. pid_t pid = fork(); if (pid == 0) { child_process_work(shm); } else { parent_process_work(shm, shmid); } return 0; }
- Perform dynamic memory consistency check.
devkit advisor dr-check -f /home/demo/test_1 -o /home/demo/output -i /home/demo -em true
The following information in bold indicates that four lines of code need to be modified. The multi-thread races report is a multi-thread report. Inter-thread reports of multiple processes are exported to the same file. The multi-process races report is a multi-process report.
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 file path.
- /home/demo/output: Scan report path.
- /home/demo: Path to the source code folder corresponding to the ELF file.
- View the names of the functions that conflict with each other in the multi-thread and multi-process reports.Figure 1 Multi-thread report
According to the multi-thread report, the conflicting function is thread_func.
Figure 2 Multi-process report
According to the multi-process report, the conflicting function is modify_shared_memory.
- You can run the nm command to view the names of the reloaded thread_func and modify_shared_memory functions.
1nm test_1Command output:
1 2
0000000000401004 T _Z11thread_funcv 00000000004010d0 T _Z20modify_shared_memoryP7ShmData
- Create a safe_functions.cfg file and write the following content to the file:
1 2
_Z11thread_funcv _Z20modify_shared_memoryP7ShmData
- Perform dynamic memory consistency check again.
devkit advisor dr-check -f /home/demo/test_1 -o /home/demo/output -i /home/demo -s /home/demo/safe_functions.cfg
The following information in bold indicates that no lines of code need to be modified.
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