佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1165|回复: 1

[Linux]FIFO程序出错

[复制链接]
发表于 26-9-2010 11:09 PM | 显示全部楼层 |阅读模式
本帖最后由 Kidult 于 29-9-2010 03:36 PM 编辑

server.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #define MSGSIZE 50

  11. char *fifo1="fifo1";char *fifo2="fifo2";
  12. main()
  13. {           
  14.      char msgbuf[MSGSIZE];           
  15.      pid_t pid;        int fd1,fd2;
  16.        
  17.      system("clear");       
  18.      printf("************** SERVER SIDE ***************\n");
  19.      if ((mkfifo(fifo1, 0666) == -1) && (errno != EEXIST))       
  20.      {                    perror("Error creating the named pipe");                    exit (1);        }               
  21.      if ((mkfifo(fifo2, 0666) == -1) && (errno != EEXIST))        
  22.      {                    perror("Error creating the named pipe");                    exit (1);        }
  23.      
  24.      if((fd2=open(fifo2, O_RDONLY|O_NONBLOCK))<0)               
  25.              perror("fifo2 open failed");
  26.      if((fd1=open(fifo1, O_WRONLY|O_NONBLOCK))<0)      
  27.              perror("fifo1 open failed");

  28.      pid = fork();        //loop:   
  29.        
  30.      if (pid < 0)         {                //failure in creating a child               
  31.               perror ("fork");                      exit(2);            }
  32.      else if (pid == 0)         {      
  33.               if(read(fd2, msgbuf, MSGSIZE+1)<0)         perror("Message read failed");        printf("Received message: %s", msgbuf);   }
  34.      else        {       
  35.               if(fgets(msgbuf,MSGSIZE, stdin) != NULL)         {                       
  36.                          if(write(fd1, msgbuf, MSGSIZE+1)<0)                               
  37.                                    perror("Message write failed");                }        }            //        goto loop;}
复制代码


client.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #define MSGSIZE 50

  11. char *fifo1="fifo1";char *fifo2="fifo2";

  12. main()
  13. {              
  14.           char msgbuf[MSGSIZE];       
  15.           int fd1,fd2;           
  16.           pid_t pid;
  17.        
  18.           system("clear");       
  19.           printf("************** CLIENT SIDE ***************\n");            
  20.           if ((mkfifo(fifo1, 0666) == -1) && (errno != EEXIST))        
  21.           {                   
  22.                   perror("Error creating the named pipe");                   
  23.                   exit (1);        
  24.            }
  25.         
  26.            if ((mkfifo(fifo2, 0666) == -1) && (errno != EEXIST))        
  27.           {            
  28.                     perror("Error creating the named pipe");            
  29.                     exit (1);        
  30.            }
  31.           
  32.           if((fd2=open(fifo1, O_WRONLY|O_NONBLOCK))<0)                perror("fifo1 open failed");
  33.           if((fd1=open(fifo2, O_RDONLY|O_NONBLOCK))<0)                perror("fifo2 open failed");
  34.           pid = fork(); //
  35. loop:           
  36.            if (pid < 0)         {                //failure in creating a child                perror ("fork");                exit(2);            }
  37.                else if (pid == 0)        
  38.           {                
  39.                    if(read(fd1, msgbuf, MSGSIZE+1)<0)                       
  40.                             perror("message read failed");               
  41.                             printf("Received message: %s", msgbuf);           
  42.           }   
  43.           else       
  44.           {                if(fgets(msgbuf,MSGSIZE, stdin) != NULL)                
  45.                         {         if(write(fd2, msgbuf, MSGSIZE+1)<0)                                perror("Message write failed");                }                           
  46.            } //   

  47. goto loop;
  48. }
复制代码


output不对劲啊~
请大家帮忙看看
我这个程序到底哪出问题了
拜托了~
回复

使用道具 举报


ADVERTISEMENT

发表于 27-9-2010 09:31 PM | 显示全部楼层
我觉得你应该把代码整理一下。。。怎样看
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 18-11-2025 08:12 PM , Processed in 0.129110 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表