我确信我错过了一些简单的事情,但是我已经将我的头撞到了众所周知的墙上几天,并且在读了几次mmap并谷歌搜索之后仍然没有接近。
我有一个(精简的)程序,其中共享内存空间用于在程序运行时尝试启用调试/跟踪,但我处于尝试将mmap指针返回主程序的第一阶段担心来电者。
主例程在另一个例程(sharedmemoryv.c)中调用POSIX共享内存函数,并且似乎已成功返回shm_open,ftruncate和mmap,但是当控件恢复为主例程时,mmap的返回值为NULL(* debug_flag)导致SEGV错误。
非常感谢任何推动或指导。
运行应用程序的输出如下:
./STACKOVERFLOW.o
PError is: : Success
IN SHARED MEMORY FNT1 0
IN SHARED MEMORY FNT2 1
debug flag is null
Segmentation fault
GDB显示它确实与debug_flag有关,而之前它没有分配给它。
debug flag is null
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a4c in print_debug (debug_flag=0x0, title_string=0x400cad "debug prog", first_string=0x400c9e "finishing main", second_string=0x400c9d "") at debug.c:8
8 if(DEBUG_ON == *debug_flag)
main.c中:
1
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "debug.h"
6 #include "sharedmemoryv.h"
7 #include "sharedmemory.h"
8
9
10 int *debug_flag = NULL;
11
12 int main(void)
13 {
14 enum data_type { TYPE_INT_SIGNED, TYPE_LONG_LONG_SIGNED, TYPE_CHAR_SIGNED };
15
16 enum data_type type_for_debug = TYPE_INT_SIGNED;
17
18 shared_memory_variable(type_for_debug, debug_flag, DEBUG_FLAG_1);
19
20 if(debug_flag == NULL)
21 {
22 printf("%s\n", "debug flag is null");
23 }
24 print_debug(debug_flag, "debug prog", "finishing main", BLANK_STRING);
25
26 return 0;
27 }
sharedmemoryv.c:
1 #include <stdio.h>
2 #include <sys/mman.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <errno.h>
10
11 #include "debug.h"
12
13 int shared_memory_variable(int type, void *variable, char *shared_memory_reference)
14 {
15 enum data_type { TYPE_INT_SIGNED, TYPE_LONG_LONG_SIGNED, TYPE_CHAR_SIGNED };
16 int shared_memory_fd = 0;
17
18 shm_unlink(shared_memory_reference);
19
20 if(-1 == (shared_memory_fd = shm_open(shared_memory_reference, O_CREAT|O_RDWR|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO)))
21 {
22 fprintf(stdout, "%s%s\n", "Could not create a shared memory segment for ", shared_memory_reference);
23 return -1;
24 }
25
26 if(0 != ftruncate(shared_memory_fd, sizeof(int)))
27 {
28 printf("%s\n", "ftruncate has an error");
29 perror("ftruncate error is ");
30 }
31 if(MAP_FAILED == (variable = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED, shared_memory_fd, 0)))
32 {
33 printf("%s\n", "POINTER ALLOCATION FAILED FOR MMAP");
34 }
35 perror("PError is: ");
36 printf("%s%d\n", "IN SHARED MEMORY FNT1 ", *(int *) variable);
37 *(int *) variable = DEBUG_ON;
38 printf("%s%d\n", "IN SHARED MEMORY FNT2 ", *(int *) variable);
39
40 close(shared_memory_fd);
41 return 0;
42 }
debug.c
1 #include <stdio.h>
2
3 #define DEBUG_ON 1
4 #define DEBUG_OFF 0
5
6 int print_debug(int *debug_flag, const char *title_string, const char *first_string, const char *second_string)
7 {
8 if(DEBUG_ON == *debug_flag)
9 {
10 fprintf(stdout, "%s%s%s%s%s\n", title_string, ": ", first_string," ", second_string);
11
12 }
13 return 0;
14 }
为完整起见,头文件如下所示:
debug.h
1 extern int *debug_flag;
2
3 int print_debug(int *debug_flag, const char *title_string, const char *first_string, const char *second_string);
4
5 #define BLANK_STRING ""
6 #define DEBUG_ON 1
7 #define DEBUG_OFF 0
8
sharedmemory.h
1 #define DEBUG_FLAG_1 "/debug_flag_1"
sharedmemoryv.h
1 int shared_memory_variable(int type, void *variable, char *shared_memory_reference);
答案 0 :(得分:3)
现在,您通过debug_flag
参数将shared_memory_variable
中的NULL传递给void *variable
。由于您已按值传递,因此该函数无法修改它!
您需要将指针传递给debug_flag
到shared_memory_variable
:
int shared_memory_variable(int type, void **variable, char *shared_memory_reference)
{
// ...
if(MAP_FAILED == (*variable = mmap
和
int *debug_flag = NULL;
int main(void)
{
// ...
shared_memory_variable(type_for_debug, &debug_flag, DEBUG_FLAG_1);
答案 1 :(得分:1)
您永远不会将debug_flag
设置为NULL
以外的任何内容。
C是按值传递的,因此当您将debug_flag
传递给shared_memory_variable
时,其值将复制到参数variable
。稍后设置variable
的值时,此更改不会传播到debug_flag
。在函数内部,您打印的值为variable
,而不是debug_flag
的值。