I am trying to use pipes in C. I have two create two pipes between parent and child process.I have to read a file in chunks of 4096 bytes (or smaller if there is less) and I have to send through the pipes the amount of data that was read and how many times there have been readings. For example, to copy a 6KB file, the parent writes the first 4KB data of the file to the shared memory and send two integers, 1 and 4096, to the child via the pipe. The child receives these two numbers, copies 4096 bytes from the shared memory to the output file, and sends back 1 to the parent via the other pipe. After receiving 1, the parent copies the left 2KB data to the shared memory and send 2 and 2048 to the child. The child receives them from the pipe, copies 2048 bytes to the output file, and replies with 2 to the parent. The parent then send 0, 0 to the child. The child receives 0 and replies with a 0 and then exit. The parent receives 0 and exits too.
我试图在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;
}
The printfs are there to help me see where the program is during execution. It gets stuck in child process, it seems during
printfs可以帮助我查看程序在执行期间的位置。它似乎陷入了儿童过程中
len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
This is an assignment, so please do not post code as an answer but rather please hep me understand what I am doing wrong. Thanks
这是一项任务,所以请不要将代码作为答案发布,而是请让我理解我做错了什么。谢谢
2 个解决方案
#1
1
Synchronization mechanism between child and parent looks suspicious:
子和父之间的同步机制看起来很可疑:
while(inParent);
inChild = 1;
and
和
while(inChild);
inParent = 1;
Initial values for inChild
and inParent
is 0
. After child process created each process has it's own copy of variable values. When you change inChild = 1
and inParent = 1
, it's changed inside the current process only. Other process doesn't see new values and cannot wait for the input/output.
inChild和inParent的初始值为0.创建子进程后,每个进程都有自己的变量值副本。当您更改inChild = 1和inParent = 1时,它仅在当前进程内更改。其他进程没有看到新值,也无法等待输入/输出。
To fix it you should use better synchronization algorithm, e.g. processes semaphores. Read "5.2 Processes Semaphores" to get details.
要修复它,您应该使用更好的同步算法,例如处理信号量。阅读“5.2处理信号量”以获取详细信息。
#2
1
It gets stuck in child process, it seems during
它似乎陷入了儿童过程中
len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
Well yes, I imagine it does.
是的,我想是的。
You've been a bit too clever, I think, in setting up a single 4-element array for the pipe-end file descriptors. That's not inherently wrong, but it tends to obscure what's going on a bit.
我认为,在为管道端文件描述符设置单个4元素数组时,你有点太聪明了。这本身并不是错误的,但它往往会掩盖一些正在发生的事情。
Consider what the pipes are supposed to do for you: one process writes to the write end of a pipe, and the other reads what was written from the read end of that same pipe. Look carefully at which file descriptors each process is reading from and writing to.
考虑管道应该为您做什么:一个进程写入管道的写入端,另一个进程读取从同一个管道的读取端写入的内容。仔细查看每个进程正在读取和写入的文件描述符。
#1
1
Synchronization mechanism between child and parent looks suspicious:
子和父之间的同步机制看起来很可疑:
while(inParent);
inChild = 1;
and
和
while(inChild);
inParent = 1;
Initial values for inChild
and inParent
is 0
. After child process created each process has it's own copy of variable values. When you change inChild = 1
and inParent = 1
, it's changed inside the current process only. Other process doesn't see new values and cannot wait for the input/output.
inChild和inParent的初始值为0.创建子进程后,每个进程都有自己的变量值副本。当您更改inChild = 1和inParent = 1时,它仅在当前进程内更改。其他进程没有看到新值,也无法等待输入/输出。
To fix it you should use better synchronization algorithm, e.g. processes semaphores. Read "5.2 Processes Semaphores" to get details.
要修复它,您应该使用更好的同步算法,例如处理信号量。阅读“5.2处理信号量”以获取详细信息。
#2
1
It gets stuck in child process, it seems during
它似乎陷入了儿童过程中
len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
Well yes, I imagine it does.
是的,我想是的。
You've been a bit too clever, I think, in setting up a single 4-element array for the pipe-end file descriptors. That's not inherently wrong, but it tends to obscure what's going on a bit.
我认为,在为管道端文件描述符设置单个4元素数组时,你有点太聪明了。这本身并不是错误的,但它往往会掩盖一些正在发生的事情。
Consider what the pipes are supposed to do for you: one process writes to the write end of a pipe, and the other reads what was written from the read end of that same pipe. Look carefully at which file descriptors each process is reading from and writing to.
考虑管道应该为您做什么:一个进程写入管道的写入端,另一个进程读取从同一个管道的读取端写入的内容。仔细查看每个进程正在读取和写入的文件描述符。