|
这是我做的一个作业,用命名管道(fifo)实现进程间通信
用gcc client.c进行编译时,老是提示errno:in door_func.c:two or more types of declaration of Creat_Door.
这是怎么回事
//door_func.h
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<errno.h>
#define B_SIZ (PIPE_BUF/2)
#define PUBLIC "/tmp/PUBLIC"
struct message{
char fifo_name[B_SIZ];
char string[B_SIZ];
int type;
}
//door_func.c
#include "door_func.h"
/**********************************/
/* These door function stuff */
/* uses FIFO to do communication */
/* between processes */
/* */
/**********************************/
/***************************************/
/* Create_Door() */
/* create a door */
/***************************************/
void Create_Door(char *fifo_name)
{
mknod(fifo_name,S_IFIFO|6060,0);
}
/*******************************************/
void door_delete(char *str)
{
unlink(str);
}
/*********************************************/
int door_open(char *fifoname,const int type)
{
int fd;
if((fd=open(fifoname,type))==-1)
{
perror(fifoname);
exit(2);
}
return fd;
}
/*******************************************/
void door_close(int fd)
{
close(fd);
}
/********************************************/
int door_read(int fd,char *buffer,int sizeofbuffer)
{
read(fd,buffer,sizeofbuffer);
return 1;
}
/**************************************************/
void door_write(int fd,char *buffer,int sizeofbuffer)
{
write(fd,buffer,sizeofbuffer);
}
/**************************************************/
void door_call( )
{
int publicfifo,privatefifo;
struct message msg;
publicfifo=door_open(PUBLIC,O_WRONLY|O_NDELAY);
//send a request to create a private door
sprintf(msg.fifo_name,"/tmp/fifo%d",getpid());
memset(msg.string,0x0,B_SIZ);
msg.type=0;
door_write(publicfifo,(char *)&msg,sizeof msg);
door_wait(5);
privatefifo=door_open(msg.fifo_name,O_RDONLY);
door_read(privatefifo,(char *)&msg,sizeof msg);
door_write(filono(stdout),msg.string,sizeof msg.string);
//send a request to test the private door
strcpy(msg.string,"a test");
msg.type=1;
door_write(publicfifo,(char *)&msg,sizeof msg);
door_wait(5);
door_read(privatefifo,(char *)&msg,sizeof msg);
door_write(filono(stdout),msg.string,sizeof msg.string);
//send a request to kill this private door
memset(msg.string,0x0,B_SIZ);
msg.type=2;
door_write(publicfifo,(char *)&msg,sizeof msg);
}
/************************************************/
void server_door(void)
{
int publicfifo,privatefifo;
struct message msg;
publicfifo=door_open(PUBLIC,O_RDONLY);
while((door_read(publicfifo,(char *)&msg,PIPE_BUF))>0)
{
switch (msg.type)
{
case 0:
//create a private door for a client
Create_Door(msg.fifo_name);
case 1:
//deal with the string from the client
privatefifo=door_open(msg.fifo_name,O_WRONLY|O_NDELAY);
door_write(privatefifo,"congratulations",sizeof("congratulations"));
door_close(privatefifo);
case 2:
//delete a private door
door_delete(msg.fifo_name);
}//switch
}
door_close(publicfifo);
}
//client.c
#include "door_func.h"
main(void)
{
door_call();
} |
|