命令行音乐播放器代码
2016-06-12 20:54
471 查看
最近想在linux平台下写一款音乐播放器,找了一下,没有其他的解决办法,于是想调用mmplayer的代码来达到播放音乐的目的,然后开始写了之后,发现需要用的技术还挺多的。包括,多线程编程,多进程编程,进程间通讯,线程间通讯,条件变量,互斥量,线程锁,有名管道以及无名管道,权当复习一下linux系统调用编程,下面我把代码复制到下面,大家参考一下,相关的资料在网上都能找得到。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
int fd_fifo; //有名管道,用于和mplayer通讯
int fd_pipe[2];//无名管道,用于打印mplayer输出的消息
//发送到mplayer的线程
void *get_pthread(void *arg)
{
char buf[100];
while(1)
{
printf("please input you cmd:");
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)]=0;
printf("*%s*\n",buf);
if(write(fd_fifo,buf,strlen(buf)) != strlen(buf))
perror("write");
}
}
//打印消息,在此可以添加消息处理函数
void *print_pthread(void *arg)
{
char buf[100];
close(fd_pipe[1]);
int size = 0;
while(1)
{
size = read(fd_pipe[0],buf,sizeof(buf));
if (size == 0) {
return 1;
}
buf[size] = 0;
printf("th msg read form pipe is %s\n",buf);
}
}
int main(int argc,char *argv[])
{
int fd;
char buf[100];
pid_t pid;
//断开管道
unlink("/tmp/my_fifo");
if( mkfifo("/tmp/my_fifo",O_CREAT|0666) < 0)
{
perror("mkfifo");
}
if(pipe(fd_pipe) < 0)
{
perror("pipe");
exit(-1);
}
pid = fork();
if(pid < 0)
{
perror("fork");
exit(-1);
}
char buf[10];
atoi()
if(pid == 0) //子进程
{
close(fd_pipe[0]);
dup2(fd_pipe[1],1);
if(fd_fifo = open("/tmp/my_fifo",O_RDWR) < 0)
{
perror("son fd_fifo");
}
execlp("mplayer","mplayer","-slave","-quiet","-input",\
"file=/tmp/my_fifo","ge.wav","-loop","0",NULL);
}
else
{
pthread_t tid1;
pthread_t tid2;
fd_fifo=open("/tmp/my_fifo",O_RDWR);
if(fd_fifo<0)
{
perror("open my_fifo");
}
pthread_create(&tid1,NULL,get_pthread,NULL);
pthread_create(&tid2,NULL,print_pthread,NULL);
pthread_join(tid1,NULL); //等待线程结束
pthread_join(tid2,NULL);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
int fd_fifo; //有名管道,用于和mplayer通讯
int fd_pipe[2];//无名管道,用于打印mplayer输出的消息
//发送到mplayer的线程
void *get_pthread(void *arg)
{
char buf[100];
while(1)
{
printf("please input you cmd:");
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)]=0;
printf("*%s*\n",buf);
if(write(fd_fifo,buf,strlen(buf)) != strlen(buf))
perror("write");
}
}
//打印消息,在此可以添加消息处理函数
void *print_pthread(void *arg)
{
char buf[100];
close(fd_pipe[1]);
int size = 0;
while(1)
{
size = read(fd_pipe[0],buf,sizeof(buf));
if (size == 0) {
return 1;
}
buf[size] = 0;
printf("th msg read form pipe is %s\n",buf);
}
}
int main(int argc,char *argv[])
{
int fd;
char buf[100];
pid_t pid;
//断开管道
unlink("/tmp/my_fifo");
if( mkfifo("/tmp/my_fifo",O_CREAT|0666) < 0)
{
perror("mkfifo");
}
if(pipe(fd_pipe) < 0)
{
perror("pipe");
exit(-1);
}
pid = fork();
if(pid < 0)
{
perror("fork");
exit(-1);
}
char buf[10];
atoi()
if(pid == 0) //子进程
{
close(fd_pipe[0]);
dup2(fd_pipe[1],1);
if(fd_fifo = open("/tmp/my_fifo",O_RDWR) < 0)
{
perror("son fd_fifo");
}
execlp("mplayer","mplayer","-slave","-quiet","-input",\
"file=/tmp/my_fifo","ge.wav","-loop","0",NULL);
}
else
{
pthread_t tid1;
pthread_t tid2;
fd_fifo=open("/tmp/my_fifo",O_RDWR);
if(fd_fifo<0)
{
perror("open my_fifo");
}
pthread_create(&tid1,NULL,get_pthread,NULL);
pthread_create(&tid2,NULL,print_pthread,NULL);
pthread_join(tid1,NULL); //等待线程结束
pthread_join(tid2,NULL);
}
return 0;
}
相关文章推荐
- Linux socket 初步
- 使用C++实现JNI接口需要注意的事项
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- 关于指针的一些事情