C 管道上的写入操作总是失败
我对管道和并发性有点陌生,几个小时以来一直对这个问题感到沮丧。我很难理解为什么这个C 管道上的写入操作总是失败,c,linux,process,pipe,C,Linux,Process,Pipe,我对管道和并发性有点陌生,几个小时以来一直对这个问题感到沮丧。我很难理解为什么这个写操作在我的管道上总是失败。我试图让子进程通过父进程接收的管道写入数据。我目前的代码是: #include <unistd.h> #include <stdio.h> #include <stdlib.h> #define MAXSIZE 4096 int main() { pid_t status; int fd[2]; //The array of file
写操作在我的管道上总是失败。我试图让子进程通过父进程接收的管道写入数据。我目前的代码是:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 4096
int main() {
pid_t status;
int fd[2]; //The array of file descriptors
if (pipe(fd) == -1) {
printf("Error piping");
}
status = fork(); //Begin the fork process
switch (status) {
case -1:
perror("Error forking");
break;
case 0:
//Child process
close(fd[0]); //Only send data
char some_string[15] = "hi there";
if (write(fd[1], some_string, MAXSIZE) == -1) {
printf("Error writing to the pipe");
}
close(fd[1]); //Close write end
exit(1);
default:
close(fd[1]); //Only receive data
char readed[500] = "";
while(read(fd[0], readed, MAXSIZE) != 0) {
printf("read this %s\n", readed);
}
printf("Done reading");
close(fd[0]);
break;
}
return 1;
}
#包括
#包括
#包括
#定义MAXSIZE 4096
int main(){
pid_t状态;
int fd[2];//文件描述符的数组
如果(管道(fd)=-1){
printf(“错误管道”);
}
status=fork();//开始fork进程
开关(状态){
案例1:
perror(“错误分叉”);
打破
案例0:
//子进程
关闭(fd[0]);//仅发送数据
char some_string[15]=“你好”;
if(write(fd[1],某些_字符串,MAXSIZE)=-1){
printf(“写入管道时出错”);
}
关闭(fd[1]);//关闭写入端
出口(1);
违约:
关闭(fd[1]);//仅接收数据
读取的字符[500]=“”;
while(read(fd[0],readed,MAXSIZE)!=0){
printf(“读取此%s\n”,已读取);
}
printf(“完成阅读”);
关闭(fd[0]);
打破
}
返回1;
}
但是,我经常收到消息“Error writing to pipe”,这意味着write
操作在子进程中失败。另一件有趣的事情是,如果我将某个字符串改为字符串文字,则此代码工作正常,但它从不终止,相反,父进程中的read
操作从STDIN
读取!我不明白为什么会发生这种情况,有没有可能在父级执行时有一个僵尸子级,所以管道“死了”?或者父进程终止,我们有一个孤儿?我怎样才能避免这种情况,而这又是如何从字符串文字中解释这种奇怪的行为的呢?有什么见解吗?您告诉write()
从数组范围外读取数据,并允许read()
将读取的数据写入数组范围外。那太糟糕了
仅写入有效数据,并将读取长度限制为不导致超出范围的访问
试试这个:
#include <unistd.h>
#include <sys/types.h> /* add this to use pid_t */
#include <sys/wait.h> /* add this to use wait() */
#include <stdio.h>
#include <stdlib.h>
/* remove unused MAXSIZE */
int main() {
pid_t status;
int fd[2]; //The array of file descriptors
int st; /* variable for receiving the status */
if (pipe(fd) == -1) {
printf("Error piping");
return 1; /* return 1 when the execution failed */
}
status = fork(); //Begin the fork process
switch (status) {
case -1:
perror("Error forking");
return 1; /* return 1 when the execution failed */
break;
case 0:
//Child process
close(fd[0]); //Only send data
char some_string[15] = "hi there";
if (write(fd[1], some_string, sizeof(some_string)) == -1) {
printf("Error writing to the pipe");
}
close(fd[1]); //Close write end
exit(0); /* return 0 if the execution finished successfully */
default:
close(fd[1]); //Only receive data
char readed[500] = "";
while(read(fd[0], readed, sizeof(readed) - 1) != 0) { /* -1 for reserving space for terminating null-character */
printf("read this %s\n", readed);
}
printf("Done reading");
close(fd[0]);
wait(&st); /* wait for the child process to exit and release the data of the process */
break;
}
return 0; /* return 0 if the execution finished successfully */
}
#包括
#include/*添加此项以使用pid\t*/
#include/*将此添加到使用wait()中*/
#包括
#包括
/*删除未使用的MAXSIZE*/
int main(){
pid_t状态;
int fd[2];//文件描述符的数组
int st;/*用于接收状态的变量*/
如果(管道(fd)=-1){
printf(“错误管道”);
返回1;/*执行失败时返回1*/
}
status=fork();//开始fork进程
开关(状态){
案例1:
perror(“错误分叉”);
返回1;/*执行失败时返回1*/
打破
案例0:
//子进程
关闭(fd[0]);//仅发送数据
char some_string[15]=“你好”;
if(write(fd[1],some_string,sizeof(some_string))=-1){
printf(“写入管道时出错”);
}
关闭(fd[1]);//关闭写入端
退出(0);/*如果执行成功完成,则返回0*/
违约:
关闭(fd[1]);//仅接收数据
读取的字符[500]=“”;
while(read(fd[0],readed,sizeof(readed)-1)!=0){/*-1用于为终止空字符保留空间*/
printf(“读取此%s\n”,已读取);
}
printf(“完成阅读”);
关闭(fd[0]);
等待(&st);/*等待子进程退出并释放该进程的数据*/
打破
}
返回0;/*如果执行成功完成,则返回0*/
}
您告诉write()
从数组范围外读取数据,并允许read()
将读取的数据写入数组范围外。那太糟糕了
仅写入有效数据,并将读取长度限制为不导致超出范围的访问
试试这个:
#include <unistd.h>
#include <sys/types.h> /* add this to use pid_t */
#include <sys/wait.h> /* add this to use wait() */
#include <stdio.h>
#include <stdlib.h>
/* remove unused MAXSIZE */
int main() {
pid_t status;
int fd[2]; //The array of file descriptors
int st; /* variable for receiving the status */
if (pipe(fd) == -1) {
printf("Error piping");
return 1; /* return 1 when the execution failed */
}
status = fork(); //Begin the fork process
switch (status) {
case -1:
perror("Error forking");
return 1; /* return 1 when the execution failed */
break;
case 0:
//Child process
close(fd[0]); //Only send data
char some_string[15] = "hi there";
if (write(fd[1], some_string, sizeof(some_string)) == -1) {
printf("Error writing to the pipe");
}
close(fd[1]); //Close write end
exit(0); /* return 0 if the execution finished successfully */
default:
close(fd[1]); //Only receive data
char readed[500] = "";
while(read(fd[0], readed, sizeof(readed) - 1) != 0) { /* -1 for reserving space for terminating null-character */
printf("read this %s\n", readed);
}
printf("Done reading");
close(fd[0]);
wait(&st); /* wait for the child process to exit and release the data of the process */
break;
}
return 0; /* return 0 if the execution finished successfully */
}
#包括
#include/*添加此项以使用pid\t*/
#include/*将此添加到使用wait()中*/
#包括
#包括
/*删除未使用的MAXSIZE*/
int main(){
pid_t状态;
int fd[2];//文件描述符的数组
int st;/*用于接收状态的变量*/
如果(管道(fd)=-1){
printf(“错误管道”);
返回1;/*执行失败时返回1*/
}
status=fork();//开始fork进程
开关(状态){
案例1:
perror(“错误分叉”);
返回1;/*执行失败时返回1*/
打破
案例0:
//子进程
关闭(fd[0]);//仅发送数据
char some_string[15]=“你好”;
if(write(fd[1],some_string,sizeof(some_string))=-1){
printf(“写入管道时出错”);
}
关闭(fd[1]);//关闭写入端
退出(0);/*如果执行成功完成,则返回0*/
违约:
关闭(fd[1]);//仅接收数据
读取的字符[500]=“”;
while(read(fd[0],readed,sizeof(readed)-1)!=0){/*-1用于为终止空字符保留空间*/
printf(“读取此%s\n”,已读取);
}
printf(“完成阅读”);
关闭(fd[0]);
等待(&st);/*等待子进程退出并释放该进程的数据*/
打破
}
返回0;/*如果执行成功完成,则返回0*/
}
通过替换printf(“错误写入管道”)使用perror(“错误写入管道”)
,它将打印EFAULT
。如my