即时尝试使用管道将值从一个程序传递到另一个程序。将相同的管道定义为2个不同的程序
第一个程序创建一个管道,然后用fork创建一个子进程,并在子进程的部分使用execlp执行另一个程序。
我想从第一个程序发送字符到另一个,而他们与管道运行,但我不知道如何做到这一点,因为fd [2]只在第一个程序中定义。
的第一个节目的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <termios.h>
#include <signal.h>
char getch();
int main()
{
bool selected;
int fd[2],pid;
char choice;
pipe(fd);
pid=fork();
if(pid==0)
{
execlp("./draw.out", "draw.out", NULL);
}
else
{
do
{
choice=getch();
close(fd[0]);
write(fd[1],&choice,1);
close(fd[1]);
kill(pid,SIGUSR2);
}while(choice!='q');
}
return 1;
//getchar();
}
第二程序的代码:
#
include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <signal.h>
typedef struct
{
int x;
int y;
}Point;
typedef struct
{
Point dots[3];
}Tool;
void drawBoard(int array[][20]);
void initBoard(int array[][20]);
Tool retrieveTool();
bool changeLocation(int array[][20],Tool* tool);
void my_handler(int signum);
int main()
{
bool nextTool=true;
Tool temp=retrieveTool();
int gameBoard[20][20];
signal(SIGUSR2, my_handler);
initBoard(gameBoard);
changeLocation(gameBoard,&temp);
drawBoard(gameBoard);
while(true)
{
sleep(1);
system("clear");
if(!changeLocation(gameBoard,&temp))
temp=retrieveTool();
drawBoard(gameBoard);
}
return 1;
//getchar();
}
void my_handler(int signum)
{
char geth='a';
if (signum == SIGUSR2)
{
close(fd[1]);
read(fd[0],&geth,1);
close(fd[0]);
printf("Received SIGUSR2!%c\n",geth);
}
}
正如你可以在第一节目中看到我限定FD [2 ]变量,并且我从用户发送一个字符到管道。 我希望其他程序中的信号处理程序“my_handler”将从同一个管道读取,但fd在此处未定义(在第二个程序中)。
我该怎么做?