简体   繁体   English

尝试从 pipe 访问 void * 中的数据后收到总线错误

[英]Receiving Bus Error after trying to access data in void * from pipe

I am currently writing a program to calculate the Lucas Series, Hexagonal Series, and Harmonic Series given a number from an input file.我目前正在编写一个程序,根据输入文件中的数字计算卢卡斯级数、六角级数和谐波级数。 I am using a pipe and shared memory segment to share values across the programs.我正在使用 pipe 和共享 memory 段来跨程序共享值。

I start by creating my pipe我首先创建我的 pipe

int p[2];
if (pipe(p) < 0)
    fprintf(stderr, "Problem opening pipe!\n");

Then I run a child process to read the contents of the input file into my pipe where argv[1] is the name of the input file(maxPrime is used later but not here)然后我运行一个子进程将输入文件的内容读入我的 pipe 其中 argv[1] 是输入文件的名称(后面使用 maxPrime 但这里没有)

char tmpbuf[10];
char str[10];
sprintf(tmpbuf, "%d", p[1]);
pid = fork();
int maxPrime;
if (pid < 0) fprintf(stderr, "Error running child process\n");
else if (pid == 0) { // child
    close(p[0]);
    execlp("./Reader", "reader", argv[1], tmpbuf, NULL);
} else {
    wait(&status);
    close(p[1]);
    read(p[0], str, 10);
    printf("[Starter][%ld]: contents read from the read end pipe: %d\n", (long)prPid, atoi(str));
    n = atoi(str);
}

And finally, here is where I get the Bus error when trying to access a void pointer array called voidarr.最后,这是我在尝试访问名为 voidarr 的空指针数组时遇到总线错误的地方。 This part of the program is where I am forking to the child processes to get the sums of the numbers in their respective series and print them to the standard output.程序的这一部分是我分叉子进程以获取其各自系列中数字的总和并将它们打印到标准 output 的地方。

shms is an array of ints for file descriptors, voidarr is an array of void * for pointing to the shared memory, and pidArray is for storing the process ID of each child process shms是文件描述符的int数组,voidarr是void *数组,指向共享的memory,pidArray是存放各个子进程的进程ID

char *my_array[3] = {"lucas", "hexagonalseries", "harmonicseries"};
char *my_array2[3] = {"./lucas", "./hexagonalseries", "./harmonicseries"};
char *my_array3[3] = {"SHM_lucas", "SHM_hexagonalseries", "SHM_harmonicseries"};
char *my_array4[3] = {"Lucas.c", "Hexagonalseries.c", "Harmonicseries.c"};


for (int i = 0; i < 3; i++) {
    if ((shms[i] = shm_open(my_array3[i], O_CREAT | O_RDWR, 0666)) == -1) {
        fprintf(stderr, "Failed to open file descriptor!\n");
        return 1;
    }
    if ((voidarr[i] = mmap(NULL, 32, PROT_WRITE, MAP_SHARED, shms[i], 0)) == MAP_FAILED){
        fprintf(stderr, "Failed to map!\n");
        return 1;
    }
    pidArray[i] = fork();
    if (pidArray[i] < 0) {
        fprintf(stderr, "Error running child process %s", my_array[i]);
    } else if (pidArray[i] == 0) {
        execlp(my_array2[i], my_array4[i], my_array3[i], (char *) voidarr[i], NULL);
    } else { // parent
        //RECEIVING BUS ERROR ON LINE BELOW
        printf("[Starter]: %d and %s]\n", prPid, (char *)voidarr[i]);
        shm_unlink(my_array3[i]);
    }
}

This is what my output is这就是我的 output

[Starter][1106394] : Created Shared memory "SHM_lucas" with FD: 3
[Starter][1106394] : Created Shared memory "SHM_hexagonalseries" 
with FD: 5
[Starter][1106394] : Created Shared memory "SHM_harmonicseries" 
with FD: 6
[Starter][1106394]: contents read from the read end pipe: 0
Bus error (core dumped)

Additionally, I don't know if this is needed to answer this question but here is my Reader.c另外,我不知道是否需要回答这个问题,但这是我的 Reader.c

int main (int argc, char *argv[]) {
    if (argc != 3) fprintf(stderr, "[Reader]: Wrong number of arguments, usage requires 2, found %d", argc - 1);
    int pipe_ref = atoi(argv[2]);
    FILE *f = fopen(argv[1], "r");
    if (f == NULL) {
        fprintf(stderr, "File failed to open");
        return 1;
    }

    char buf[256];
    int run_sum = 0;
    int tmp = 0;
    printf("Hello from reader");
    while (fgets(buf, sizeof(buf), f)) {
        tmp = atoi(buf);
        run_sum += tmp;
    }
    sprintf(buf, "%d", run_sum);
    write (pipe_ref, buf, sizeof(buf));
    return 0;
}

Your problem is that after doing shm_open , doing fstat on the return value shows an st_size value of 0!您的问题是在执行shm_open之后,对返回值执行fstat显示st_size值为 0!

Initially, the descriptor from shm_open has zero size.最初,来自shm_open的描述符大小为零。 It must be explicitly given a size.必须明确地给它一个大小。 Note that just specifying 32 as a length to mmap does not cause the file to be expanded.请注意,仅将 32 指定为mmap的长度不会导致文件扩展。

This must be done with ftruncate .这必须使用ftruncate来完成。

After the shm_open call, you need to do:shm_open调用之后,您需要执行以下操作:

ftruncate(shms[i],32);

Here is a patch against your github repo.这是针对您的 github 存储库的补丁。 Even without that, it shows the changes needed:即使没有它,它也会显示所需的更改:

diff --git a/Makefile b/Makefile
index 9ea673f..a478bf6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,29 +1,38 @@
 CC=gcc
 CFLAGS=-Wall -lrt
+CFLAGS += -g

-build: Starter HarmonicSeries HexagonalSeries Lucas Reader
+TARGETS = Starter HarmonicSeries HexagonalSeries Lucas Reader
+
+build: $(TARGETS)

 Starter: Starter.c
-   $(CC) -o Starter $(CFLAGS) Starter.c
+   $(CC) -o $@ $(CFLAGS) Starter.c

 HarmonicSeries: HarmonicSeries.c
-   $(CC) -o harmonicseries $(CFLAGS) HarmonicSeries.c
+   $(CC) -o $@ $(CFLAGS) HarmonicSeries.c

 HexagonalSeries: HexagonalSeries.c
-   $(CC) -o hexagonalseries $(CFLAGS) HexagonalSeries.c
+   $(CC) -o $@ $(CFLAGS) HexagonalSeries.c

 Lucas: Lucas.c
-   $(CC) -o lucas $(CFLAGS) Lucas.c
+   $(CC) -o $@ $(CFLAGS) Lucas.c

 Reader: Reader.c
-   $(CC) -o reader $(CFLAGS) Reader.c
+   $(CC) -o $@ $(CFLAGS) Reader.c

 clean:
-   rm -rf *.o Starter HarmonicSeries HexagonalSeries Lucas
+   rm -rf *.o $(TARGETS) gdbcmds

 test:
    ./Starter file_01.in

+gdb: gdbcmds
+   gdb -x gdbcmds ./Starter
+
+gdbcmds:
+   echo "set args file_01.in" > $@
+
 tar:
    tar -cvzf Anon-HW3.tar Starter.c Lucas.c HexagonalSeries.c HarmonicSeries.c Makefile README.txt

diff --git a/Starter.c b/Starter.c
index 31feae5..6ebfc35 100644
--- a/Starter.c
+++ b/Starter.c
@@ -6,6 +6,7 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/shm.h>
+#include <sys/stat.h>

 //for testing
 #include <inttypes.h>
@@ -38,7 +39,7 @@ int main(int argc, char *argv[]) {
         n = atoi(str);
     }
     for (int i = 0; i < n; i++) {
-        for (int j = 0; j < i/2; j++) {
+        for (int j = 1; j < i/2; j++) {
             if (i % j == 0) continue;
             if (j == i/2-1) {
                 maxPrime = i;
@@ -63,7 +64,45 @@ int main(int argc, char *argv[]) {

     for (int i = 0; i < 3; i++) {
         shms[i] = shm_open(my_array3[i], O_CREAT | O_RDWR, 0666);
+#if 1
+       if (shms[i] < 0) {
+           perror("shm_open");
+           exit(1);
+       }
+#endif
+
+#if 1
+       struct stat st;
+       fstat(shms[i],&st);
+       printf("DEBUG i=%d st_size=%zu\n",i,st.st_size);
+#endif
+
+// NOTE/FIX: at this point, st_size is zero!
+// need to provide some space
+#if 1
+       ftruncate(shms[i],32);
+       fstat(shms[i],&st);
+       printf("DEBUG i=%d st_size=%zu\n",i,st.st_size);
+#endif
+
+#if 0
         voidarr[i] = mmap(NULL, 32, PROT_WRITE, MAP_SHARED, shms[i], 0);
+#else
+        voidarr[i] = mmap(NULL, 32, PROT_READ | PROT_WRITE, MAP_SHARED, shms[i], 0);
+       if (voidarr[i] == MAP_FAILED) {
+           perror("mmap");
+           exit(1);
+       }
+#endif
+
+       printf("DEBUG: i=%d shms=%d voidarr=%p my_array3='%s'\n",
+           i,shms[i],voidarr[i],my_array3[i]);
+
+// NOTE/BUG: this produces SIGBUS!!!
+       int probe = *(char *) voidarr[i];
+
+       printf("DEBUG/PROBE: probe=%d\n",probe);
+
         pidArray[i] = fork();
         if (pidArray[i] < 0) {
             fprintf(stderr, "Error running child process %s", my_array[i]);

Here is the output of the modified program:这是修改后的程序的 output:

./Starter file_01.in
Hello from reader[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=1 st_size=0
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=2 st_size=0
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6
[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
DEBUG i=1 st_size=32
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=2 st_size=32
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6
[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=1 st_size=0
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
DEBUG i=2 st_size=32
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6
[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
DEBUG i=1 st_size=32
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
DEBUG i=2 st_size=0
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6
[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
DEBUG i=1 st_size=32
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
DEBUG i=2 st_size=0
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6
[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=1 st_size=0
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
DEBUG i=2 st_size=32
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6
[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
DEBUG i=1 st_size=32
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=2 st_size=32
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6
[Starter][422287]: contents read from the read end pipe: 10
DEBUG i=0 st_size=0
DEBUG i=0 st_size=32
DEBUG: i=0 shms=4 voidarr=0x7f98e1e54000 my_array3='SHM_lucas'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=1 st_size=0
DEBUG i=1 st_size=32
DEBUG: i=1 shms=5 voidarr=0x7f98e1e53000 my_array3='SHM_hexagonalseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
DEBUG i=2 st_size=0
DEBUG i=2 st_size=32
DEBUG: i=2 shms=6 voidarr=0x7f98e1e52000 my_array3='SHM_harmonicseries'
DEBUG/PROBE: probe=0
[Starter]: 422287 and ]
[Starter][422287] : Created Shared memory "SHM_lucas" with FD: 4
[Starter][422287] : Created Shared memory "SHM_hexagonalseries" with FD: 5
[Starter][422287] : Created Shared memory "SHM_harmonicseries" with FD: 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM