使用管道在C中的子进程和父进程之间的双向通信 [英] two way communication between child and parent processes in C using pipes
本文介绍了使用管道在C中的子进程和父进程之间的双向通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望父进程和子进程使用管道在C linux中进行通信。我创建了两个文件描述符。一个用于父母对孩子,即readpipe和其他writepipe for viceversa。
但是我在下面的代码中获取ch和ch1字符串的输出为null。
请帮助我这个。
I want parent and child processes to communicate in C linux using pipes. I have created two file descriptors. one for parent to child i.e. readpipe and other writepipe for viceversa.
But I am getting null as output for ch and ch1 strings in my code below.
Please help me in this.
#include <stdio.h>
#include<stdlib.h>
#include
#include<unistd.h>
int main()
{
pid_t pid;
int r;
char *ch=NULL;
char *ch1=NULL;
int readpipe[2];
int writepipe[2];
int a;
int b;
a=pipe(readpipe);
b=pipe(writepipe);
// check a and b
pid=fork();
// check pid
if(pid==0)
{ //CHILD PROCESS
close(readpipe[1]);
close(writepipe[1]);
read(readpipe[0],ch,sizeof(ch));
printf("\nREAD = %s",ch);
close(readpipe[0]);
ch1="YES";
write(writepipe[1],ch1,sizeof(ch1));
close(writepipe[1]);
}
else
{ //PARENT PROCESS
close(writepipe[0]);
close(writepipe[1]);
ch="HI!! YOU THERE";
write(readpipe[1],ch,sizeof(ch));
close(readpipe[1]);
read(writepipe[1],ch1,sizeof(ch1));
printf("\nACK RECEIVED %s",ch1);
close(writepipe[1]);
}
return 0;
}
谢谢......:)
Thanks...:)
推荐答案
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include<wait.h>
int main(void)
{
pid_t pid;
int r;
char buf[1024];
char cp[50];
char ans;
int readpipe[2];
int writepipe[2];
long int a;
int b;
a=pipe(readpipe);
b=pipe(writepipe);
if (a == -1) { perror("pipe"); exit(EXIT_FAILURE); }
if (b == -1) { perror("pipe"); exit(EXIT_FAILURE); }
printf("\nSIZE OF CP IS %d",sizeof(cp));
printf("\nSEND SOMETHING TO CHILD PROCESS\t");
scanf("%[^\n]%*c",&ans);
fflush(stdin);
pid=fork();
if(pid==-1)
{
printf("pid:main");
exit(1);
}
while(ans=='y' || ans=='Y')
{
if(pid==0)
{
//CHILD PROCESS
if(ans=='n')
{
//a=getpid();
//printf("\nPARENT PROCESS a=%ld",a);
kill(pid,SIGKILL);
}
close(readpipe[1]);
close(writepipe[0]);
if(read(readpipe[0],buf,sizeof(buf)) < 0)
{
break;
}
printf("\nChild Process Read: %s\n",buf);
printf("\n(child)Enter data:\t");
fflush(stdin);
fgets(cp, 50, stdin);
printf("\nData Written to Parent%s",cp);
if(/*!strncmp("Q",cp,1) || */write(writepipe[1],cp,strlen(cp)+1) < 0)
{
break;
}
//printf("\nSEND SOMETHING TO PARENT PROCESS\t");
//fflush(stdin);
//scanf(" %[^\n]%*c",&ans);
}
else
{
close(readpipe[0]);
close(writepipe[1]);
printf("\n(Parent)Enter data\t");
fgets(cp, 50, stdin);
printf("\nData Writtent to Child: %s",cp);
if(/*!strncmp("Q",cp,1) ||*/ write(readpipe[1],cp,strlen(cp)+1) < 0)
{
break;
}
if(read(writepipe[0],buf,sizeof(buf)) < 0)
{
break;
}
printf("\nParent Process Read: %s\n",buf);
printf("\nSEND SOMETHING TO CHILD PROCESS\t");
fflush(stdin);
scanf(" %[^\n]%*c",&ans);
if(ans=='n')
{
kill(pid,SIGKILL);
}
}
// printf("\nSEND SOMETHING TO CHILD PROCESS\t");
// scanf("%[^\n]%*c",&ans);
}
close(readpipe[1]);
close(writepipe[0]);
close(readpipe[0]);
close(writepipe[1]);
return 0;
}
我看到两个问题(可能还有更多)......
1.在CHILD PROCESS的一半代码中,你关闭writepipe [1]然后写入它。
2在PARENT PROCESS的一半代码中,你关闭了两个写管脚,但后来又关闭了writepipe [1]。
I see two problems (there may be more) ...
1. In the CHILD PROCESS half of the code, you close writepipe[1] and then write to it.
2. In the PARENT PROCESS half of the code, you close BOTH writepipe handles but later close writepipe[1] again.
#include< stdio.h>
#include< unistd.h>
#include< sys types.h =>
#include< stdlib.h>
#include< memory.h>
int main(){
//父母对孩子>
int parentToChild [2];
//儿童为父母
int childToParent [2];
//字符串数据
char message1 [] =从孩子到父母的按摩;
char message2 [] =从父母到孩子的消息\ n;
char readBuffer [120];
pipe(parentToChi ld);
pipe(childToParent);
int processId,bytes;
if((processId = fork())== -1){
printf(创建子进程时出错);
}
if(processId == 0){
//这里的子进程
//从孩子发送消息给父母
close(childToParent [0]);
写(childToParent [1],message1,strlen(message1)+1);
/来自父母的/ recive消息
close(parentToChild [1]);
bytes = read(parentToChild [0],readBuffer,sizeof(readBuffer));
printf(%s,readBuffer);
//退出(1);
}
if(processId!= 0){
//这里的父流程
//从父母发送消息
close(parentToChild [0]);
write(parentToChild [ 1],message2,strlen(message2)+1);
//收到孩子的留言
close(childToParent [1]) ;
bytes = read(childToParent [0],readBuffer,sizeof(readBuffer));
printf(%s,readBuffer);
//退出(1);
}
返回0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys types.h="">
#include <stdlib.h>
#include <memory.h>
int main() {
//for parent to child
int parentToChild[2];
//for child to parent
int childToParent[2];
//string data
char message1[] = "massage from child to parent\n";
char message2[] = "message from parent to child\n";
char readBuffer[120];
pipe(parentToChild);
pipe(childToParent);
int processId , bytes;
if((processId = fork())== -1){
printf("error while creating a child process");
}
if(processId == 0){
//child process here
//send message from child to parent
close(childToParent[0]);
write(childToParent[1] , message1 , strlen(message1)+1);
//recive message from parent
close(parentToChild[1]);
bytes = read(parentToChild[0] , readBuffer , sizeof(readBuffer));
printf("%s",readBuffer);
// exit(1);
}
if(processId !=0 ){
//parent process here
//send message from parent to child
close(parentToChild[0]);
write(parentToChild[1] , message2 , strlen(message2)+1);
//recieve message from child
close(childToParent[1]);
bytes = read(childToParent[0] , readBuffer , sizeof(readBuffer));
printf("%s", readBuffer);
// exit(1);
}
return 0;
}
这篇关于使用管道在C中的子进程和父进程之间的双向通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文