[英]C Using pipes to transfer data to write to file using shared memory
我正在嘗試在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;
}
printfs可以幫助我查看程序在執行過程中的位置。 似乎卡在子進程中
len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
這是一項作業,因此請不要將代碼作為答案,而請幫助我了解我在做什么錯。 謝謝
子與父之間的同步機制看起來很可疑:
while(inParent);
inChild = 1;
和
while(inChild);
inParent = 1;
inChild
和inParent
初始值為0
。 在子進程創建之后,每個進程都有自己的變量值副本。 當您更改inChild = 1
和inParent = 1
,僅在當前進程內部進行了更改。 其他進程看不到新值,因此無法等待輸入/輸出。
要修復它,您應該使用更好的同步算法,例如處理信號量。 閱讀“ 5.2處理信號量”以獲取詳細信息。
似乎卡在子進程中
len = read(pipes[C_READ], &cBytes, sizeof(cBytes));
好吧,我想是的。
我認為您在為管道端文件描述符設置單個4元素數組時有點太聰明了。 這並不是天生的錯誤,但是它會掩蓋正在發生的事情。
考慮一下管道應該為您做什么:一個進程將數據寫入管道的寫入端,而另一個進程將從同一管道的讀取端讀取已寫入的內容。 仔細查看每個進程正在讀取和寫入的文件描述符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.