当前位置: 首页 > ds >正文

利用多进程定时播放,关闭音乐播放器

主进程监控时间,一个子进程负责播放,一个进程负责关闭, 如要更多时间控制可以增加子进程。

当然,通过if判断,一个子进程也可完成多种操作。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>int closejc(){char *file=tmpnam(NULL);       //生成临时文件freopen(file,"w",stdout);       system("ps -a");                //列出进程到临时文件freopen("/dev/tty","w",stdout);       //取消重定向FILE *f=fopen(file,"r");char o[100];while(fgets(o,100,f)!=NULL){char *pd=strstr(o,"deepin-music");     //杀死运行的deepin-music 程序进程if(pd!=NULL){char *bz=strtok(o," ");         //提取进程号//		int ps=atoi(bz);//		printf("%d\n",ps);char ps[20];snprintf(ps,20,"kill -9 %s",bz);   //拼接kill -9  进程号  system(ps);                         //kill -9  杀死bz号进程}}return 0;
}int main(void){int pi[2];pipe(pi);int cs[2];pipe(cs);pid_t pd=fork();if(pd<0){perror("no york");exit(-1);}if(pd==0){                       //接收open  开始播放音乐close(pi[1]);char o[30];read(pi[0],o,30);if(memcmp(o,"ok",2)==0){system("deepin-music");}_exit(0);}
//-----------------------------------------------------------pd=fork();if(pd<0){perror("no fork");exit(-1);}if(pd==0){close(cs[1]);char o[30];read(cs[0],o,30);if(memcmp(o,"no",2)==0){closejc();}_exit(0);}//---------------------------------------------------close(pi[0]);close(cs[0]);char o[10];struct tm* tp;time_t tim;char bz[][7]={"18611","18711"};         //启动,关闭时间  格式:时分秒while(1){time(&tim);tp=localtime(&tim);snprintf(o,10,"%d%d%d%c",tp->tm_hour,tp->tm_min,tp->tm_sec,'\0');if(memcmp(bz[0],o,7)==0){memcpy(o,"ok",3);write(pi[1],o,3);}if(memcmp(bz[1],o,7)==0){memcpy(o,"no",3);write(cs[1],o,3);}sleep(1);}	return 0;
}

http://www.xdnf.cn/news/10635.html

相关文章:

  • go环境配置
  • 深入剖析C#构造函数执行:基类调用、初始化顺序与访问控制
  • UNION 与 UNION ALL 的区别
  • DAY 36 超大力王爱学Python
  • 设计模式——外观设计模式(结构型)
  • 力扣上C语言编程题
  • LangGraph(八)——LangGraph运行时
  • K3s简介、实战、问题记录
  • STM32F407寄存器操作(ADC非连续扫描模式)
  • 操作系统学习(九)——存储系统
  • AI 代理框架:使用正确的工具构建更智能的系统
  • 2025.6.1总结
  • 仓颉鸿蒙开发:制作底部标签栏
  • python训练营打卡第41天
  • 启动你的RocketMQ之旅(七)-Store存储原理
  • MySQL优化全链路实践:从慢查询治理到架构升级
  • 邮件验证码存储推荐方式
  • 前端基础学习html+css+js
  • 计算机网络第1章(上):网络组成与三种交换方式全解析
  • 【IC】多角多模式信号完整性优化
  • VBA数据库解决方案二十:Select表达式From区域Where条件Order by
  • 基于React + TypeScript构建高度可定制的QR码生成器
  • 鸿蒙OSUniApp结合机器学习打造智能图像分类应用:HarmonyOS实践指南#三方框架 #Uniapp
  • MCU SoC
  • Shape and boundary-aware
  • Ubuntu配置中文语言
  • GoldenEye
  • 机器学习-ROC曲线​​ 和 ​​AUC指标
  • 内存管理 : 06 内存换出
  • 不使用绑定的方法