关于有名管道的 读写问题 请 大虾们帮我看看 。。。。。
/*
两个进程 : 一个在接到 sigalrm 信号 后向 管道里写入一个随机数和系统时间,另一个读出并写进文件,父进程等待 2分钟后 发出终止信号 结束。*/
并显示
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define filename "myfifo "
void w_fifo();
void r_fifo();
int main(int argc, char *argv[])
{
int pid1,pid2;
int i=0;
int file;
if( (file = mkfifo(filename,O_CREAT|O_EXCL)) < 0)
perror( "mfifo ");
if(( pid1 = fork() ) < 0)
{
perror( "fork ");
exit(1);
}
else if( pid1 == 0)
{
signal(SIGALRM, w_fifo);
alarm(1);
pause();
}
///////////////////////////////////////////////////
if(( pid2 = fork() ) < 0)
{
perror( "fork ");
exit(1);
}
else if( pid2 == 0)
{
r_fifo();
}
///////////////////////////////////////////////////////////////////////////
sleep(120);
kill(pid1,SIGABRT);
kill(pid2,SIGABRT);
unlink(filename);
return 0;
}
////////////////////////////////////////
void w_fifo()
{
int fd;
int * randnum;
time_t *sys_time;
if((fd=open(filename,O_WRONLY|O_NONBLOCK,0)) < 0)
perror( "open ");
*randnum =(int)rand()%101;
*sys_time = time((time_t *) NULL);
write(fd,randnum,sizeof(randnum));
write(fd,sys_time,sizeof(sys_time));
sleep(1);
}
void r_fifo()
{
FILE *fp;
int fd;
char * r_buf;
int readnum;
time_t sys_time;
if((fd=open(filename,O_RDONLY|O_NONBLOCK,0)) < 0)
perror( "open ");
if((readnum = read(fd,r_buf,sizeof(int)+sizeof(time_t))) < 0)
perror( "read ");
printf( "%s ",r_buf);
if((fp=fopen( "out.txt ", "a ")) < 0)
perror( "open ");
fprintf(fp,r_buf,sizeof(r_buf));
fclose(fp);
}
执行结果 :
open: Permission denied
read: Bad file descriptor
open: No such device or address
忽略 (core dumped)
[解决办法]
1. open: Permission denied 是因为 if((fd=open(filename,O_WRONLY|O_NONBLOCK,0)) < 0) 中的O_WRONLY|O_NONBLOCK 不能同时使用
2. 指针的错误使用导致core dump!
试试下面的在 UWIN 4.1 下通过的代码:
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#define filename "myfifo "
void w_fifo(int);
void r_fifo(void);
int main(void)
{
pid_t pid1, pid2;
int i = 0;
int fd;
if ( ( fd = mkfifo(filename, 0666) ) < 0)
{
perror( "mkfifo ");
}
if ( ( pid1 = fork() ) < 0 )
{
perror( "fork ");
exit(1);
}
else if ( 0 == pid1)
{
signal(SIGALRM, w_fifo);
alarm(1);
pause();
exit(0);
}
if ( ( pid2 = fork() ) < 0 )
{
perror( "fork ");
exit(1);
}
else if ( 0 == pid2)
{
r_fifo();
exit(0);
}
sleep(10);
kill(pid1, SIGABRT);
kill(pid2, SIGABRT);
unlink(filename);
return 0;
}
void w_fifo(int sig)
{
int fd;
int randnum;
time_t sys_time;
if ( (fd = open(filename, O_WRONLY)) < 0)
perror( "open ");
randnum = rand() % 101;
sys_time = time(NULL);
write(fd, &randnum, sizeof(randnum));
write(fd, &sys_time, sizeof(sys_time));
sleep(1);
}
void r_fifo(void)
{
int fd;
int randnum;
time_t sys_time;
int n;
if ( ( fd = open(filename, O_RDONLY | O_NONBLOCK) ) < 0)
perror( "open ");
if ( ( n = read(fd, (void *)&randnum, sizeof(randnum)) ) < 0)
perror( "read ");
if ( ( n = read(fd, (void *)&sys_time, sizeof(sys_time)) ) < 0)
perror( "read ");
(void) close(fd);
(void) printf( "%s%d\n ", (void *)ctime(&sys_time), randnum);
}