簡體   English   中英

IPC 通過共享內存和管道給出分段錯誤:C 中的 11

[英]IPC through shared memory and pipe gives segmentation fault: 11 in C

我正在嘗試在父進程和子進程之間共享文件。 父級通過管道發送文件,子級將這些行寫入共享內存,以便父級可以通過共享內存讀取和打印文件。 但是,我遇到了分段錯誤: 11. 此外,我做了類似下面代碼的操作,但那次我無法獲得正確的內容,甚至每次調用時都得到不同的結果。

我不確定增加指針部分。 但是,最好多注意代碼。

編輯:我將 char* 更正為 char[] 並且分段錯誤現在消失了。 但是,每次運行時我都會得到不同的結果,在輸出中會看到一些額外的字符。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>

#define SHM_NAME_1 "Child 1"

int main(){

    pid_t pid;
    FILE *file;
    char *infile = "in.txt";

    pid = fork();

    if(pid < 0){
        fprintf(stderr, "Fork failed\n");
        return 1;
    }

    if(pid > 0){ // parent

        file = fopen(infile, "r");

        if(file == 0){
            fprintf(stderr, "File failed\n");
            return 1;
        }
        // close read end of pipe
        mknod("FIFO", S_IFIFO | 0666, 0);
        int fd = open("FIFO", O_WRONLY);

        char str[300];
        while(fgets(str, sizeof(str), file) > 0)
        {
            // write all lines of file
            write(fd, str, strlen(str));
        }
        // close file and pipe
        close(fd);
        fclose(file);
        // wait for child to write to shared memory
        wait(NULL);

        // open shared segment
        int shm_first = shm_open(SHM_NAME_1, O_RDONLY, 0666);

        if (shm_first == -1) {
            fprintf(stderr, "Failed: Shared Memory 1");
            exit(-1);
        }

        // create memory pointer
        void *ptr = mmap(0,4096, PROT_READ, MAP_SHARED, shm_first, 0);

        if (ptr == MAP_FAILED) {
            printf("Map failed 1\n");
            return -1;
        }
        // print out result and unlibk shared segment
        fprintf(stdout, "Normal input: \n%s\n", ptr);
        shm_unlink(SHM_NAME_1);

    } else { // child

        // create the shared segment for the first time
        int shm_child_1 = shm_open(SHM_NAME_1, O_CREAT | O_RDWR, 0666);

        // configure the size of the shared memory segment 
        ftruncate(shm_child_1,4096);
        // map the pointer to the segment
        void *ptr_child_1 = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, shm_child_1, 0);

        if (ptr_child_1 == MAP_FAILED) 
        {
            printf("Map failed in first child\n");
            return -1;
        }


        mknod("FIFO", S_IFIFO | 0666, 0);
        int fd = open("FIFO", O_RDONLY);

        int num;
        char s[300];
        while((num = read(fd, s, sizeof(s)))> 0)
        {     
            sprintf(ptr_child_1, "%s", s);
            ptr_child_1 += num;
        }

        close(fd);
        exit(0);

    }

    return 0;
}

一個快速的觀察。

在以下代碼中,您有一個未初始化為指向任何內容的 char 指針。 這導致 fgets 將從file讀取的內容復制到內存中的任意位置。

    char *str;
    while(fgets(str, 100, file) > 0)

現在緩沖區問題解決了,下面表達式中的大括號也有問題

    while((num = read(fd, s, sizeof(s)) > 0))

num將是 1 或 0,而不是讀取的字節數或 eof 的 0。 這應該是

    while((num = read(fd, s, sizeof(s))) > 0)

一旦獲得讀取的字節數,就需要零終止緩沖區。 因為您正在使用sprintf ,它期望%s的參數是一個以零結尾的字符串。

    while((num = read(fd, s, sizeof(s)))> 0)
    {
        s[num] = '\0';  // Terminate the string to the number of bytes read
        sprintf(ptr_child_1, "%s", s);
        ptr_child_1 += num;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM