|
查看: 1164|回复: 1
|
[Linux]FIFO程序出错
[复制链接]
|
|
|
本帖最后由 Kidult 于 29-9-2010 03:36 PM 编辑
server.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <string.h>
- #include <signal.h>
- #define MSGSIZE 50
- char *fifo1="fifo1";char *fifo2="fifo2";
- main()
- {
- char msgbuf[MSGSIZE];
- pid_t pid; int fd1,fd2;
-
- system("clear");
- printf("************** SERVER SIDE ***************\n");
- if ((mkfifo(fifo1, 0666) == -1) && (errno != EEXIST))
- { perror("Error creating the named pipe"); exit (1); }
- if ((mkfifo(fifo2, 0666) == -1) && (errno != EEXIST))
- { perror("Error creating the named pipe"); exit (1); }
-
- if((fd2=open(fifo2, O_RDONLY|O_NONBLOCK))<0)
- perror("fifo2 open failed");
- if((fd1=open(fifo1, O_WRONLY|O_NONBLOCK))<0)
- perror("fifo1 open failed");
- pid = fork(); //loop:
-
- if (pid < 0) { //failure in creating a child
- perror ("fork"); exit(2); }
- else if (pid == 0) {
- if(read(fd2, msgbuf, MSGSIZE+1)<0) perror("Message read failed"); printf("Received message: %s", msgbuf); }
- else {
- if(fgets(msgbuf,MSGSIZE, stdin) != NULL) {
- if(write(fd1, msgbuf, MSGSIZE+1)<0)
- perror("Message write failed"); } } // goto loop;}
复制代码
client.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <string.h>
- #include <signal.h>
- #define MSGSIZE 50
- char *fifo1="fifo1";char *fifo2="fifo2";
- main()
- {
- char msgbuf[MSGSIZE];
- int fd1,fd2;
- pid_t pid;
-
- system("clear");
- printf("************** CLIENT SIDE ***************\n");
- if ((mkfifo(fifo1, 0666) == -1) && (errno != EEXIST))
- {
- perror("Error creating the named pipe");
- exit (1);
- }
-
- if ((mkfifo(fifo2, 0666) == -1) && (errno != EEXIST))
- {
- perror("Error creating the named pipe");
- exit (1);
- }
-
- if((fd2=open(fifo1, O_WRONLY|O_NONBLOCK))<0) perror("fifo1 open failed");
- if((fd1=open(fifo2, O_RDONLY|O_NONBLOCK))<0) perror("fifo2 open failed");
- pid = fork(); //
- loop:
- if (pid < 0) { //failure in creating a child perror ("fork"); exit(2); }
- else if (pid == 0)
- {
- if(read(fd1, msgbuf, MSGSIZE+1)<0)
- perror("message read failed");
- printf("Received message: %s", msgbuf);
- }
- else
- { if(fgets(msgbuf,MSGSIZE, stdin) != NULL)
- { if(write(fd2, msgbuf, MSGSIZE+1)<0) perror("Message write failed"); }
- } //
- goto loop;
- }
复制代码
output不对劲啊~
请大家帮忙看看
我这个程序到底哪出问题了
拜托了~ |
|
|
|
|
|
|
|
|
|
|
发表于 27-9-2010 09:31 PM
|
显示全部楼层
我觉得你应该把代码整理一下。。。怎样看 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|