Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C使用管道传输数据,并使用共享内存写入文件_C_Pipe_Shared Memory_Race Condition - Fatal编程技术网

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

C使用管道传输数据,并使用共享内存写入文件,c,pipe,shared-memory,race-condition,C,Pipe,Shared Memory,Race Condition,我尝试在C中使用管道。我在父进程和子进程之间创建了两个管道。我必须读取4096字节的文件(如果有更少字节,则更小),并且必须通过管道发送读取的数据量和读取的次数。例如,要复制6KB的 文件,父级将文件的第一个4KB数据写入共享内存,并通过管道将两个整数1和4096发送给子级。子级接收这两个数字,将4096字节从共享内存复制到输出文件,并通过另一个管道将1发送回父级。在收到1之后, 父级将剩余的2KB数据复制到共享内存中,并将2和2048发送给子级。子级从管道接收它们,将2048个字节复制到输出文

我尝试在C中使用管道。我在父进程和子进程之间创建了两个管道。我必须读取4096字节的文件(如果有更少字节,则更小),并且必须通过管道发送读取的数据量和读取的次数。例如,要复制6KB的 文件,父级将文件的第一个4KB数据写入共享内存,并通过管道将两个整数1和4096发送给子级。子级接收这两个数字,将4096字节从共享内存复制到输出文件,并通过另一个管道将1发送回父级。在收到1之后, 父级将剩余的2KB数据复制到共享内存中,并将2和2048发送给子级。子级从管道接收它们,将2048个字节复制到输出文件,并向父级回复2个字节。然后父级将0、0发送给子级。该子级收到0并以0进行答复,然后退出。父母 接收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;
}

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

子级和父级之间的同步机制看起来可疑:

while(inParent); 
inChild = 1; 

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


要解决这个问题,您应该使用更好的同步算法,例如处理信号量。阅读以获取详细信息。

子级和父级之间的同步机制看起来可疑:

while(inParent); 
inChild = 1; 

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

要解决这个问题,您应该使用更好的同步算法,例如处理信号量。阅读以获取详细信息

它被卡在子进程中,似乎是在

len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
是的,我想是的

我认为,在为管道端文件描述符设置单个4元素数组时,您有点太聪明了。这并不是天生的错误,但它往往掩盖了正在发生的事情

考虑管道应该为您做什么:一个进程写入管道的写入端,另一个进程读取从同一管道的读取端写入的内容。仔细查看每个进程读取和写入的文件描述符

它被卡在子进程中,似乎是在

len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
是的,我想是的

我认为,在为管道端文件描述符设置单个4元素数组时,您有点太聪明了。这并不是天生的错误,但它往往掩盖了正在发生的事情


考虑管道应该为您做什么:一个进程写入管道的写入端,另一个进程读取从同一管道的读取端写入的内容。仔细查看每个进程正在读取和写入哪些文件描述符。

这看起来确实可疑,实际上是无效的。但代码实际上根本不依赖它。是管道上的I/O提供了同步(或者无论如何都是为了实现同步)。@JohnBollinger对,管道的同步IO提供了同步,但正如我们在代码中看到的,这并不是作者所期望的。我同意OP可能有不正确的期望,但这些都与OP询问的程序不当行为无关。这看起来确实可疑,事实上是无效的。但代码实际上根本不依赖它。是管道上的I/O提供了同步(或者无论如何都是为了实现同步)。@JohnBollinger对,管道的同步IO提供了同步,但正如我们在代码中看到的,这并不是作者所期望的。我同意OP可能有不正确的期望,但是这些都与OP询问的程序错误行为无关。好吧,所以我适当地重新定义了管道末端。现在,当它写入文件时,它只写入少量文件。我认为这与我的fwrite(sharedMem,1,sizeof(sharedMem),w)有关,但我不确定。@David,这将是一个单独的问题,但您似乎已经确定了问题的根源。由于
sharedMem
是一个
void*
sizeof(sharedMem)
计算为
void*
的大小,该大小与指针指向的对象(如果有)的大小无关。您可能需要4或8个字节。您无法通过这种方式测量已分配对象的大小。实际上,您根本无法测量动态分配对象的大小。现在,当它写入文件时,它只写入少量文件。我认为这与我的fwrite(sharedMem,1,sizeof(sharedMem),w)有关,但我不确定。@David,这将是一个单独的问题,但您似乎已经确定了问题的根源。由于
sharedMem
是一个
void*
sizeof(sharedMem)
计算为
void*
的大小,该大小与指针指向的对象(如果有)的大小无关。您可能需要4或8个字节。您无法通过这种方式测量已分配对象的大小。实际上,您根本无法测量动态分配对象的大小。