写入另一个程序的stdin /从c中的另一个程序的stdout读取

时间:2014-08-15 18:02:12

标签: c stdout stdin io-redirection

这是一个家庭作业问题。我有3个程序A,B和C,它们不是父/子进程,而是单独的程序。 B必须向A的stdin写一条消息(“Hello”)并从C的stdout读取另一条消息(“Hello”)。我应该使用哪个概念来实现它?我环顾了一段时间但我找不到合适的东西。我以为我应该使用FIFO,但我无法重定向管道。我可以将FIFO与FIFO一起使用吗?如果有,怎么样?这不是作业本身,而是它应该的工作方式。然后我会在其上实现其他的东西。 (我可以发布我的fifo基本代码,如果是这样的话,现在就不要这样做,因为我不确定)

2 个答案:

答案 0 :(得分:3)

如果我误解了这个问题,请告诉我。

假设您创建了程序ABC。以编程方式,您可以使用man 1 mkfifoman 3 mkfifo来创建命名管道。

然后,您的每个流程都会open(2)他们并根据他们的需要使用dup2(2)

例如,程序A可以像这样重定向stdout

int fifo = open("fifo_1", O_WRONLY);
dup2(fifo, 1);

或程序B可以像这样重定向stdinstdout

int fifo_in = open("fifo_1", O_RDONLY);
int fifo_out = open("fifo_2", O_WRONLY);

dup2(fifo_in, 0);
dup2(fifo_out, 1);

或其他任何你需要的东西。

答案 1 :(得分:1)

我修改了这段代码,因为@chrk解释道。它是“高级Unix编程”中的一个FIFO示例。一个简单的服务器客户端示例。客户端向服务器发送三个较低的字符串,服务器使它们成为高级通信并通过FIFO发送回客户端(" fifo#clientpid"是客户端的名称fifo)。客户端将它们打印到stdout。所以我修改它的方式是在从客户端收到消息后,服务器将高级字符串写入客户端的STDINT。客户端从STDINT读取它们并打印到它的标准输出。它按我的预期工作。谢谢你的帮助。首先,我以错误的方式实施。在chrk的代码的帮助下,我再次写了它。这是代码:

客户的输出

client 2941 started
client 2941: applesauce --> APPLESAUCE
client 2941: tiger --> TIGER
client 2941: mountain --> MOUNTAIN
Client 2941 done

服务器:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>

#define ec_neg1(s,m) if((s) == -1) {perror(m); exit(errno);}

#define SERVER_FIFO_NAME "fifo_server"
#define PERM_FILE        0664

struct simple_message {
pid_t sm_clientpid;
char sm_data[200];
};


int main()
{
  int fd_server, fd_client, i;
  ssize_t nread;
  struct simple_message msg;
  char fifo_name[100];
  printf("server started\n");

  if (mkfifo(SERVER_FIFO_NAME, PERM_FILE) == -1 && errno != EEXIST)
  {perror("can't make fifo"); exit(errno); }

  ec_neg1( fd_server = open(SERVER_FIFO_NAME, O_RDWR), "cant open fd_server" )

  while (1) 
  {
     ec_neg1( nread = read(fd_server, &msg, sizeof(msg)), "can't read from fd_server")

     if (nread == 0) {
       errno = ENETDOWN;
       perror("nread == 0"); exit(errno);
      }

     for (i = 0; msg.sm_data[i] != '\0'; i++)
       msg.sm_data[i] = toupper(msg.sm_data[i]);

     make_fifo_name(msg.sm_clientpid, fifo_name, sizeof(fifo_name));

     ec_neg1( fd_client = open(fifo_name, O_WRONLY), "can't open fifo_name" )

     ec_neg1( write(fd_client, &msg, sizeof(msg)), "can't write to fd_client" )

     ec_neg1( close(fd_client), "can't close fd_client" )
   }

/* never actually get here */
 ec_neg1( close(fd_server), "can't close fd_server" )
 exit(EXIT_SUCCESS);

 return 0;

 }


int make_fifo_name(pid_t pid, char *name, size_t name_max)
{
 snprintf(name, name_max, "fifo%ld", (long)pid);
 return 0;
}

客户端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#define ec_neg1(s,m) if((s) == -1) {perror(m); exit(errno);}

#define SERVER_FIFO_NAME "fifo_server"
#define PERM_FILE        0664

struct simple_message {
pid_t sm_clientpid;
char sm_data[200];
};


int make_fifo_name(pid_t pid, char *name, size_t name_max)
{
 snprintf(name, name_max, "fifo%ld", (long)pid);
 return 0;
}

int main()
{
 int fd_server, fd_client = -1, i;
 ssize_t nread;
 struct simple_message msg;
 char fifo_name[100];
 char *work[] = {
   "applesauce",
   "tiger",
   "mountain",
   NULL
 };

 printf("client %ld started\n", (long)getpid());

 msg.sm_clientpid = getpid();

 make_fifo_name(msg.sm_clientpid, fifo_name,
       sizeof(fifo_name));

 if (mkfifo(fifo_name, PERM_FILE) == -1 && errno != EEXIST)
   {perror("can't make fifo"); exit(errno); }

 ec_neg1( fd_server = open(SERVER_FIFO_NAME, O_WRONLY), "can't open fd_server" )

 for (i = 0; work[i] != NULL; i++) 
 {
   strcpy(msg.sm_data, work[i]);

   ec_neg1( write(fd_server, &msg, sizeof(msg)),"can't write to fd_server" )

   if (fd_client == -1){
     ec_neg1( fd_client = open(fifo_name, O_RDWR), "can't open fifo_name"  )
     ec_neg1(dup2(fd_client, 0), "can't duplicate stdin")
   }
     ec_neg1( nread = read(0, &msg, sizeof(msg)), "can't read from fd_client" )
   if (nread == 0) {
       errno = ENETDOWN;
       perror("nread == 0"); exit(errno);
   }
   printf("client %ld: %s --> %s\n", (long)getpid(),
             work[i], msg.sm_data);
 }

 ec_neg1( close(fd_server), "can't close fd_server" )
 ec_neg1( close(fd_client), "can't close fd_client" )
 ec_neg1( unlink(fifo_name), "can't unlink fifo_name" )

 printf("Client %ld done\n", (long)getpid());
 exit(EXIT_SUCCESS);

}