child 进程之一无法从管道读取
One of the child processes can't read from pipe
我正在尝试用 C 语言实现一个小型 IPC,但我遇到了无法解决的问题。
我的主进程有两个children,childA和childB; Child 首先创建 A。
我有两个管道将信息传递给这些进程,pipefd
和 pipefd2
.
首先,parent 进程读取文件内容,并将这两个值写入两个管道:
1-length of the string
2-string itself
一旦他们得到字符串的长度,他们就会创建一个具有给定长度的字符数组,然后将字符串读入这个数组。
然后 children 都从他们的管道中读取数据。 Child B 可以从管道中正确获取这些值,但是 child A 获取 0 作为大小。
这是我的代码,感谢您的宝贵时间!
输出:
Child A here, input read, size is: 0
Size is 45169child reads input, size : 45169
Child B here, input read, size is: 45169
//
// Created by Dilara on 16.02.2020.
//
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MESSAGESIZE 10
#define READ_END 0
#define WRITE_END 1
int fsize(FILE *fp){
int prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz=ftell(fp);
fseek(fp,prev,SEEK_SET); //go back to where we were
return sz;
}
void writeToPipe(int fd[2], char string[], long stell){
close(fd[READ_END]);
write(fd[WRITE_END], stell, sizeof(stell));
write(fd[WRITE_END], string, strlen(string)+1);
close(fd[WRITE_END]);
}
static void readFromPipe(int pipefd[2], char input[],long size){
// wait(NULL);
close(pipefd[WRITE_END]);
read(pipefd[READ_END], input, size);
printf("child reads input, size : %d \n",size);
close(pipefd[READ_END]);
}
static long readSize(int pipefd[2]){
long size;
close(pipefd[WRITE_END]);
read(pipefd[READ_END], size, sizeof(size));
printf("Size is %d",size);
close(pipefd[READ_END]);
return size;
}
int main()
{
int pipefd[2];
int pipefd2[2];
int childA_pid;
int childB_pid;
if (pipe(pipefd) == -1)
{
perror(" pipe");
return 0;
}
if (pipe(pipefd2) == -1)
{
perror(" pipe");
return 0;
}
childA_pid = fork();
if(childA_pid == 0){
//childA
long SIZE = readSize(pipefd);
char input[SIZE];
readFromPipe(pipefd, input, SIZE);
printf("Child A here, input read, size is: %d \n",SIZE);
}
else{
childB_pid = fork();
if(childB_pid == 0){
long SIZE = readSize(pipefd2);
char input[SIZE];
readFromPipe(pipefd2, input, SIZE);
printf("Child B here, input read, size is: %d \n",SIZE);
}else{
//parent
char *buffer;
FILE *fp = fopen("try.txt", "r");
if (fp != NULL)
{
fseek(fp, 0L, SEEK_END);
long stell = ftell(fp);
rewind(fp);
char str[stell];
buffer = (char *)malloc(stell);
if (buffer != NULL)
{
//fread(buffer, stell, 1, fp);
fread(str,stell,1, fp);
// printf("%s", str);
fclose(fp);
fp = NULL;
free(buffer);
}
printf("SIZE: %d", stell);
writeToPipe(pipefd2, str,stell);
writeToPipe(pipefd, str,stell);
}
}
}
return 0;
}
write(fd[WRITE_END], stell, sizeof(stell));
我认为你的代码在这里缺少一个符号。你应该使用它的地址而不是 stell
变量:
write(fd[WRITE_END], &stell, sizeof(stell));
我正在尝试用 C 语言实现一个小型 IPC,但我遇到了无法解决的问题。
我的主进程有两个children,childA和childB; Child 首先创建 A。
我有两个管道将信息传递给这些进程,pipefd
和 pipefd2
.
首先,parent 进程读取文件内容,并将这两个值写入两个管道:
1-length of the string
2-string itself
一旦他们得到字符串的长度,他们就会创建一个具有给定长度的字符数组,然后将字符串读入这个数组。
然后 children 都从他们的管道中读取数据。 Child B 可以从管道中正确获取这些值,但是 child A 获取 0 作为大小。
这是我的代码,感谢您的宝贵时间!
输出:
Child A here, input read, size is: 0
Size is 45169child reads input, size : 45169
Child B here, input read, size is: 45169
//
// Created by Dilara on 16.02.2020.
//
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MESSAGESIZE 10
#define READ_END 0
#define WRITE_END 1
int fsize(FILE *fp){
int prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz=ftell(fp);
fseek(fp,prev,SEEK_SET); //go back to where we were
return sz;
}
void writeToPipe(int fd[2], char string[], long stell){
close(fd[READ_END]);
write(fd[WRITE_END], stell, sizeof(stell));
write(fd[WRITE_END], string, strlen(string)+1);
close(fd[WRITE_END]);
}
static void readFromPipe(int pipefd[2], char input[],long size){
// wait(NULL);
close(pipefd[WRITE_END]);
read(pipefd[READ_END], input, size);
printf("child reads input, size : %d \n",size);
close(pipefd[READ_END]);
}
static long readSize(int pipefd[2]){
long size;
close(pipefd[WRITE_END]);
read(pipefd[READ_END], size, sizeof(size));
printf("Size is %d",size);
close(pipefd[READ_END]);
return size;
}
int main()
{
int pipefd[2];
int pipefd2[2];
int childA_pid;
int childB_pid;
if (pipe(pipefd) == -1)
{
perror(" pipe");
return 0;
}
if (pipe(pipefd2) == -1)
{
perror(" pipe");
return 0;
}
childA_pid = fork();
if(childA_pid == 0){
//childA
long SIZE = readSize(pipefd);
char input[SIZE];
readFromPipe(pipefd, input, SIZE);
printf("Child A here, input read, size is: %d \n",SIZE);
}
else{
childB_pid = fork();
if(childB_pid == 0){
long SIZE = readSize(pipefd2);
char input[SIZE];
readFromPipe(pipefd2, input, SIZE);
printf("Child B here, input read, size is: %d \n",SIZE);
}else{
//parent
char *buffer;
FILE *fp = fopen("try.txt", "r");
if (fp != NULL)
{
fseek(fp, 0L, SEEK_END);
long stell = ftell(fp);
rewind(fp);
char str[stell];
buffer = (char *)malloc(stell);
if (buffer != NULL)
{
//fread(buffer, stell, 1, fp);
fread(str,stell,1, fp);
// printf("%s", str);
fclose(fp);
fp = NULL;
free(buffer);
}
printf("SIZE: %d", stell);
writeToPipe(pipefd2, str,stell);
writeToPipe(pipefd, str,stell);
}
}
}
return 0;
}
write(fd[WRITE_END], stell, sizeof(stell));
我认为你的代码在这里缺少一个符号。你应该使用它的地址而不是 stell
变量:
write(fd[WRITE_END], &stell, sizeof(stell));