C使用管道传输数据以使用共享内存写入文件

标签 c pipe shared-memory race-condition

我正在尝试在 C 中使用管道。我有两个在父进程和子进程之间创建两个管道。我必须以 4096 字节的 block 读取文件(如果字节少则更小)并且我必须通过管道读取的数据量和读取次数。比如复制一个6KB 文件,父进程将文件的前 4KB 数据写入共享内存,并通过管道将 1 和 4096 两个整数发送给子进程。 child 收到这两个数字,从共享内存复制 4096 字节到输出文件,并通过另一个管道将 1 发送回父。收到1后, parent 将剩下的 2KB 数据复制到共享内存中,并将 2 和 2048 发送给 child。子进程从管道接收它们,将 2048 个字节复制到输出文件,并用 2 回复父进程。然后 parent 将 0, 0 发送给 child 。 child 收到 0 并回复 0,然后退出。 parent 收到 0 并退出。

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

#define SIZE 4096
#define NUM_OF_PIPES 2
#define P_READ 0
#define P_WRITE 1
#define C_READ 2
#define C_WRITE 3

int main(int argv, char *argc[]) {


  /*Check if program is called correctly*/
  if(argv != 3) {
    printf("Please call program appropriately\n");
    exit(EXIT_FAILURE);
  }

  FILE *r, *w, *check;
  void *sharedMem;
  int pipes[4];
  int shm;
  char userInput[5];
  char *name = "dm11ad_cop4610";
  int inChild = 0;
  int inParent = 0;
  r = fopen(argc[1], "rb");
  check = fopen(argc[2], "rb");

  /*Check if read file can open*/
  if(r == NULL)  {
    perror("Error opening read file");
    exit(EXIT_FAILURE);
  }
  /*Check if write file can open*/
  if(check == NULL) {
    perror("Error with write file");
    exit(EXIT_FAILURE);
  }
  else {
    fseek(check, 0, SEEK_END);
    int writeLen = ftell(check);
    if(writeLen > 0) {
      rewind(check);
      printf("Would you like to overwrite file (yes/no): ");
      scanf("%s", userInput);
      if(!strcmp(userInput, "yes")) {
        printf("Overwriting file...\n");
        w = fopen(argc[2], "wb");
      }
      else if(!strcmp(userInput, "no")) {
        printf("Will not overwrite\n");
        exit(EXIT_FAILURE);
      }
      else {
        printf("User input not accepted\n");
        exit(EXIT_FAILURE);
      }
    }
  }

  for (int i = 0; i < NUM_OF_PIPES; i++) {
    if (pipe(pipes+(i*2)) < 0) {
        perror("Pipe");
        exit(EXIT_FAILURE);
    }
  }

  /*Check if forking process is successful*/
  pid_t pid = fork();
  if(pid < 0) {
    perror("Fork");
    exit(EXIT_FAILURE);
  }

  shm = shm_open(name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if(shm == -1) {
    perror("Shared memory");
    exit(EXIT_FAILURE);
  }
  if(ftruncate(shm, SIZE) == -1) {
    perror("Shared Memory");
    exit(EXIT_FAILURE);
  }

  sharedMem = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0);
  if(sharedMem == MAP_FAILED) {
    perror("Mapping shared memory");
    exit(EXIT_FAILURE);
  }

  if(pid == 0) {
    while(inParent); 
    inChild = 1; 
    printf("I am in child\n");
    close(pipes[P_READ]);
    close(pipes[P_WRITE]);
    printf("Closed P pipes\n");
    int cBytes, len;
    printf("Im stuck\n");
    len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
    printf("There are %i bytes\n", len);
    if(len < 0) {
      perror("Failed to read from pipe");
      exit(EXIT_FAILURE);
    }
    else if(len == 0) {
      printf("End of fle reached\n");
    }
    else {
        printf("Writing to file\n");
        fwrite(sharedMem, 1, sizeof(sharedMem), w);
      }

    printf("Closing C pipes\n");
    close(pipes[C_READ]);
    close(pipes[C_WRITE]);
    printf("Exiting Child\n");
    inChild = 0;
  }
  else {
  while(inChild);
  inParent = 1;
  close(pipes[C_READ]);
  close(pipes[C_WRITE]);
  int pBytes;

  int P2SHM = fread(sharedMem, 1, SIZE, r);
  if(P2SHM < 0) {
    perror("Could not store to shared memory");
    exit(EXIT_FAILURE);
  }

  if(write(pipes[P_WRITE], &P2SHM, sizeof(int)) < 0) {
    perror("Failed to write to pipe");
    exit(EXIT_FAILURE);
  }

  int C2P = read(pipes[P_READ], &pBytes, sizeof(int));
  if(C2P < 0) {
    perror("Failed to read value from pipe");
    exit(EXIT_FAILURE);
  }
  else if(C2P == 0) {
    printf("End of file reached\n");
  }
  else {
    printf("Received succesfully\n");
  }

  close(pipes[P_READ]);
  close(pipes[P_WRITE]);
  inParent = 0;
  printf("Waiting for child\n");
  wait(NULL);
  }
  return 0;
}

printfs 可以帮助我查看程序在执行过程中的位置。它卡在子进程中,似乎在

期间
 len = read(pipes[C_READ], &cBytes, sizeof(cBytes));

这是一项作业,所以请不要发布代码作为答案,而是请帮助我理解我做错了什么。谢谢

最佳答案

child 和 parent 之间的同步机制看起来很可疑:

while(inParent); 
inChild = 1; 

while(inChild);
inParent = 1;

inChildinParent 的初始值为 0。创建子进程后,每个进程都有自己的变量值副本。当您更改 inChild = 1inParent = 1 时,它仅在当前进程内更改。其他进程看不到新值,无法等待输入/输出。

要修复它,您应该使用更好的同步算法,例如处理信号量。阅读"5.2 Processes Semaphores"获取详细信息。

关于C使用管道传输数据以使用共享内存写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40184960/

相关文章:

python - python-如何将C函数实现为可等待的(协程)

Clang-Tidy : 'scanf' used to convert a string to an integer value, 但函数不会报转换错误;考虑改用 'strtol' (C)

go - 使用 golang io.pipe 到 tar 文件的错误

linux - shm_open() 不创建共享内存段 ipcs

我可以使用 getchar() 清除 scanf 和 fgets 之间的缓冲区吗?

c - 我怎么知道指针中有多少空闲位?

linux - 如何在 Linux 中创建一个大小的 FIFO 缓冲区?

c - 无法将消息从子进程发送回父进程

c - 如何将多个结构实例存储在共享内存中?

c - 为子进程分配内存并与父进程共享