Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153069
  • 博文数量: 25
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-22 23:16
文章存档

2009年(22)

2008年(3)

我的朋友

分类:

2009-03-05 10:46:23

通过代码或脚本来控制基于FrameBuffer的播放器,方法如下:
1.管道。
#mkfifo inputPipe
#cat ./inputPipe | ./fbPlayer MyMovie.avi >/dev/null 2>&1 &

然后就可以在终端使用写管道来控制PLAYER,如:
#echo "q" > inputPipe //需要该PLAYER支持命令'q'(退出)。

2.程序控制。
通过dup2函数进行标准输入输出重定向。

#include  
#include

#include
#include
#include
#include
#include
#include

#define STDIN 0
#define STDOUT 1
#define TEMP_IN_PIPE "stdin_pipe"

    ...
    pid_t pid;
    pid = fork();
    if (pid < 0)
    {
        perror("fork");
        return -1;
    }
    else if (pid == 0)//sub process
    {

        //delete pipe FIFO firstly
        unlink(
TEMP_IN_PIPE);
 
        //create pipe FIFO
        mode_t mode=0666;
        if ((mkfifo(TEMP_IN_PIPE,mode)) < 0)
        {
              perror("mkfifo error");
               exit(1);
        }

        //redirection stdin to tmp PIPE
        int inputPipe= open(TEMP_IN_PIPE, O_RDWR | O_NONBLOCK);
        if (inputPipe < 0)
        {
            perror("open fifo error");
            exit(1);
        }      
        ::close(STDIN);
        dup2(inputPipe, STDIN);
    }
    else //main process
    {
        printf("main process.\n");
    }


另:
使程序的执行结果同时定向到屏幕和文件




program_name |tee logfile 这样程序执行期间的显示都记录到logfile同时显示到标准输出(屏幕)。



阅读(1002) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~