#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int res = 0;
int read_nm = 0;
int write_nm = 0;
int fp = 0;
char buff[1024];
memset(buff, 0x00, sizeof(buff));
res = mkfifo("myfifo", 0777);
if (res != 0)
{
if (errno==EEXIST)
{
printf("The fifo is exist.\n");
}
else
{
printf("creat myfifo failed!\n");
exit(-1);
}
}
fp = open("myfifo", O_WRONLY);
if (fp < 0)
{
printf("open exec failed, errno=%d, errmsg=%s\n", errno, strerror(errno));
return -1;
}
while (1)
{
memset(buff, 0x00, sizeof(buff));
printf("please input msg:");
read_nm = read(0, buff, sizeof(buff)-1);
if (read_nm <= 0)
{
printf("read exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), read_nm);
close(fp);
return -1;
}
if (memcmp(buff, "exit", 4) == 0)
{
printf("process exit\n");
close(fp);
return 0;
}
write_nm = write(fp, buff, read_nm);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int res = 0;
int read_nm = 0;
int write_nm = 0;
int fp = 0;
char buff[1024];
memset(buff, 0x00, sizeof(buff));
if ((mkfifo("myfifo",0777))<0)
{
if (errno==EEXIST)
{
printf("The fifo is exist.\n");
}
else
{
printf("creat myfifo failed!\n");
exit(-1);
}
}
fp = open("myfifo", O_RDONLY);
if (fp < 0)
{
printf("open exec failed, errno=%d, errmsg=%s, fp=%d\n", errno, strerror(errno), fp);
return -1;
}
while (1)
{
memset(buff, 0x00, sizeof(buff));
read_nm = read(fp, buff, sizeof(buff)-1);
if (read_nm <= 0)
{
printf("read exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), read_nm);
close(fp);
return -1;
}
if (memcmp(buff, "exit", 4) == 0)
{
printf("process exit\n");
close(fp);
return 0;
}
printf("read buff:%s\n", buff);
}
return 0;
}