首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用两个单向管道的双向通信

问使用两个单向管道的双向通信
EN

Stack Overflow用户
提问于 2017-12-13 00:27:51
回答 2查看 487关注 0票数 0

我写了一个代码,让父母通过管道发送消息,孩子接收消息并将其打印到屏幕上……然后,子进程应回复一个消息,而父进程将消息打印到管道内。代码在父级到子级的通信中工作得很好。但是当涉及到相反的close()返回错误时,消息不会被传递...请注意,我还使用共享内存来进行一些同步和控制。我知道在孩子和父母之间有一种特殊类型的管道..but,我想使用一般情况下的管道,如果它可以用于该目的,请告诉我这是不是真的。以下是代码

代码语言:javascript
复制
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <errno.h>
#include <sched.h>
#include <string.h>
int main()
{
////creating shared memory with check///
int  shmid ;
int err;
char *shm;
shmid = shmget(IPC_PRIVATE,3 ,IPC_CREAT | IPC_EXCL | 0666 );
shm=shmat(shmid,0, IPC_CREAT | IPC_EXCL | 0666  );
if (shmid > -1)
{
printf("shared memory created succefully\n");
shm[0]='C';
shm[1]='C';
shm[2]='C';
}
else
{
return 0;
}


/////////////////////////////////////////

//////////making pipe with check/////////
int  testpipe[2];
int  size_ptr ;
char buff1[51];
char buff2[51];
char buff3[51];
char buff4[51];
int check;
check = pipe(testpipe);
if (check > -1)
{
printf("pipe created succefully\n");
}
else
{
return 0 ;
}
////////////////////////////////////////


///////////making new process///////////
pid_t pid ;
pid = fork();
////////////////////////////////////////


///////////Executing process ///////////
    if (pid > 0) //am in parent
    {


//////////partA////////////////////
     strcpy(buff1,"Hi am tweeting now ..\n");
    // err = close(testpipe[0]);
     printf("... Close state %d \n",err);
     write(testpipe[1],buff1,25);
     close(testpipe[1]);
     printf("partA Ended ... Close state %d \n");

/////////////////////////////////////
shm[0] = 'O'; // lock 2 is opened here

shm[1] = 'C'; // lock 1 is closed
while(shm[1] != 'O');
/////////////////////////////////////

//////////partD///////////////////
    //close(testpipe[1]);
    err = read(testpipe[0],buff4,25);
    //close(testpipe[1]);
    printf(" error state%i \n",err);
    printf("the output is : %s\n",buff4);
    printf("part D Ended ...\n");
    printf("Close state %d ... %i \n",err,EACCES);
    shmdt(shm);
    return 0;

    }
    else if (pid ==0) // am in child now
    {
    while(shm[0]!= 'O');  //from child read'

    ///////////////////////////////////

    //////////partb////////////////////
    read(testpipe[0],buff2,25);
    //err = close(testpipe[0]);
    printf("Close state %d \n",err);
    printf("the output is : %s\n",buff2);
    printf("partB Ended ... Close state %d\n",err);
    //close(testpipe[0]);
    //////////////////////////////////

    ///////////////    /part c////////////
     close(testpipe[0]);
     err = write(testpipe[1],buff3,25);
     printf("III... Close state %d \n",err);
     err = close(testpipe[1]);
     printf("Close state %d \n",err);
     printf("partC Ended ..\n");
     shm[1] ='O';
/////////////////////////////////////////////////
     //shm[1] = 'O';
     shm[0] = 'C';
     while(shm[0]!= 'O');
     printf("process child closed\n");

    }
    else{
     printf("Error Ocuured");
    }
//////////////////////////////////////////



}

编辑:我更改了标题a screen shot of the terminal output here :

EN

回答 2

Stack Overflow用户

发布于 2017-12-13 01:29:58

我写了一段代码,父母通过管道发送消息,孩子接收消息并将其打印到屏幕上……

您的代码打印有关close()的消息,但是根据您当前的代码,这些消息在很大程度上不报告close()调用的实际返回值。

但是你怎么知道消息没有被传递呢?您从buff3写入管道,该管道从未被初始化或写入。那么,您如何识别消息是否已传递?如果您幸运的话,那么您可以传递一条第一个字节为'\0'的消息,这样当您打印时,它将作为一个空字符串。

注意,我还使用了共享内存来进行一些同步和控制。

不,你不需要。人们可以使用共享内存来托管同步对象,例如进程共享信号量和互斥锁,但是共享内存本身并不提供任何同步语义。因此,您的程序包含数据竞争,因此其行为是未定义的。

我建议使用信号量来执行您似乎正在执行的那种锁定,除非它看起来从一开始就不是必需的。适当的管道使用不需要额外的同步。

虽然可以在两个方向上只使用一个,但这样做是混乱和不必要的。

父进程和子进程之间没有特殊类型的管道。

进程通常通过调用pipe()本身或从其父进程继承一个或多个开放管道端文件描述符,来获得对通过pipe()函数创建的管道的访问。当以这种方式获得访问权限时,只有具有共同祖先的进程才能通过管道相互通信。尽管父进程和子进程之间的通信是一个常见的示例,但这样的管道绝不限于特定的使用模式。

总体而言:

  • 我的主要建议是抛弃共享内存和不工作的“锁”,而是使用两个管道,每个direction.
  • If一个管道您必须只使用一个管道,然后执行适当的同步。出于显而易见的目的,使用一个或两个信号量--共享的进程和驻留在共享内存中的信号量--来控制允许哪个进程在任何给定时间从管道中读取数据就足够了。也有其他选择,但仅共享内存并不能提供您所需的内容。
票数 2
EN

Stack Overflow用户

发布于 2017-12-13 03:45:39

并使用共享存储器同步处理操作(r/w)。它成功了..。

代码语言:javascript
复制
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <errno.h>
#include <sched.h>
#include <string.h>

#define parent_read parent_child[0]
#define parent_write parent_child[1]
#define child_read child_parent[0]
#define child_write child_parent[1]

int main()
{
////creating shared memory with check///
int  shmid ;
int err;
char *shm;
shmid = shmget(IPC_PRIVATE,4 ,IPC_CREAT | IPC_EXCL | 0666 );
shm=shmat(shmid,0, IPC_CREAT | IPC_EXCL | 0666  );
if (shmid > -1)
{
printf("shared memory created succefully\n");
shm[0]='I';
shm[1]='I';

}
else
{
return 0;
}


/////////////////////////////////////////

//////////making pipe with check/////////
int  parent_child[2];
int  child_parent[2];
int  size_ptr ;
char buff1[51];
char buff2[51];
char buff3[51];
char buff4[51];
int check1;
int check2;
check1 = pipe(parent_child);
check2 = pipe(child_parent);
if (check1 > -1 && check2 > -1)
{
printf("pipe 1 created succefully\n");
}
else
{
return 0 ;
}
////////////////////////////////////////


///////////making new process///////////
pid_t pid ;
pid = fork();
////////////////////////////////////////


///////////Executing process ///////////






    if (pid > 0) //am in parent
    {


//////////partA////////////////////
     strcpy(buff1,"Hi am tweeting now ..\n");
     close(parent_read);
     printf("... Close state %d \n",err);
     write(parent_write,buff1,25);
     close(parent_write);
     printf("partA Ended ... Close state %d \n");


/////////////////////////////////////
    shm[1] = 'B';
    shm[0]='A';
    while(shm[0] == 'A');
//////////partD///////////////////



    close(child_write);
    printf(" error state%i \n",err);
    read(child_read,buff4,25);
    close(child_read);
    printf("from child  : %s\n",buff4);
    printf("part D Ended ...\n");
    shmdt(shm);
    return 0;

    }








    else if (pid ==0) // am in child now
    {

    while(shm[1] == 'I');
    //////////partb////////////////////
    close(parent_write);
    read(parent_read,buff2,25);
    close(parent_read);
    printf("Close state %d \n",err);
    printf("from parent : %s\n",buff2);
    printf("partB Ended ... Close state %d\n",err);
    //close(testpipe[0]);
    //////////////////////////////////

    ///////////////    /part c////////////
     close(child_read);
     strcpy(buff3,"Hello from the other side...");
     err = write(child_write,buff3,25);
     printf("III... Close state %d \n",err);
     err = close(child_write);
     printf("Close state %d \n",err);
     printf("partC Ended ..\n");
     printf("process child closed\n");
     shm[0]='D';
    }
    else{
     printf("Error Ocuured");
    }
//////////////////////////////////////////



}

父母对孩子:推特评论孩子对父母:来自另一边的hello

屏幕截图:the output on terminal

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47777271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档