| |
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <unistd.h>
4. #include <fcntl.h>
5. #include <sys/stat.h>
6. #include <sys/types.h>
7. #include <string.h>
8.
9. /**********************全局变量定义区*****************/
10. int fd_fifo; //创建有名管道,用于向mplayer发送命令
11. int fd_pipe[2]; //创建无名管道,用于从mplayer读取命令
12.
13. void *get_pthread(void *arg)
14. {
15. char buf[100];
16. while(1)
17. {
18. printf("please input you cmd:");
19. fflush(stdout);
20. fgets(buf,sizeof(buf),stdin); //从标准输入获取数据
21. buf[strlen(buf)]='\0';
22. printf("*%s*\n",buf);
23. if(write(fd_fifo,buf,strlen(buf))!=strlen(buf))
24. perror("write"); //将命令写入命名管道
25. }
26. }
27.
28. void *print_pthread(void *arg)
29. {
30. char buf[100];
31. close(fd_pipe[1]);
32. int size=0;
33. while(1)
34. {
35. size=read(fd_pipe[0],buf,sizeof(buf)); //从无名管道的写端读取信息打印在屏幕上
36. buf[size]='\0';
37. printf("th msg read form pipe is %s\n",buf);
38. }
39. }
40.
41. int main(int argc, char *argv[])
42. {
43. int fd;
44. char buf[100];
45. pid_t pid;
46.
47. unlink("/tmp/my_fifo"); //如果明明管道存在,则先删除
48. mkfifo("/tmp/my_fifo",O_CREAT|0666);
49. perror(
点赞