博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
嵌入式linux多进程编程
阅读量:6688 次
发布时间:2019-06-25

本文共 2541 字,大约阅读时间需要 8 分钟。

嵌入式linux多进程编程

在主程序显示文本菜单。提供例如以下服务。要求每一个服务都通过生成子进程来提供。

服务包含:日历信息显示,日期信息显示,推断闰年服务,文件复制功能,数字排序功能。退出功能。

代码和文档(有流程图的下载地址):

#include 
#include
#include
#include
#include
#include
void DisplayCalen();// 1 显示日历void DisplayDate(); // 2 显示日期void LeapYear();// 3 推断闰年void CopyFile();// 4 文件赋值功能void SortNum(); // 5 对数字排序int Start();// 開始界面void menu(); // 菜单函数int Start(){ int n; printf("-----linux多进程编程-----\n"); printf("1 日历信息的显示\n"); printf("2 日期信息的显示\n"); printf("3 推断闰年信息服务\n"); printf("4 文件的赋值功能\n"); printf("5 数字排序\n"); printf("6 退出程序\n"); printf("please input your choose(1-6) "); scanf("%d",&n); return n;}void DisplayCalen(){ execlp( "cal", "cal", "-sy", (char *)0 );}// 2 显示当前的系统日期void DisplayDate(){ struct tm *ptr; time_t it; it=time(NULL); ptr=localtime(&it); printf("%4d年%02d月%02d日 %d:%d:%d\n",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);}// 3 推断闰年void LeapYear(){ int m; printf("please input your years "); scanf("%d",&m); if (((0==m%4)&&(0!=m%100))||(0==m%400)) { printf("%d是闰年\n",m); }else{ printf("%d不是闰年\n",m); } }// 4 拷贝文件void CopyFile(){ const char* pathfile = "file1.c"; int in,out,flag; char buffer[1024]; in = open("file2.c",O_RDONLY, S_IRUSR); if(-1 == in) { printf("open file file2.c error!\n"); return; } out = creat(pathfile,S_IWUSR); if (-1 == out) { printf("create file %s error!\n",pathfile); return; } while((flag = read(in,buffer,1024))>0) { write(out,buffer,flag); } close(in); close(out); printf("copy file file2.c to %s\n",pathfile);}// 5 对数字排序void SortNum(){ int b[10]={29,59,8,9,16,7,2,98,29,10}; int i,j,t,k; printf("数组中的10个数字为:\n"); for (i = 0; i < 10; i ++) { printf("%d\t",b[i]); } printf("\n"); for(i=0;i<10-1;i++) for(k=i,j=i+1;j<10;j++) { if(b[k]
0 ) { waitpid( child, NULL, 0) ; } break; case 2: if ((child=fork())==-1) { printf("error......\n"); }else if (child==0) { DisplayDate(); }else if( child > 0 ) { waitpid( child, NULL, 0) ; } break; case 3: if ((child=fork())==-1) { printf("error......\n"); }else if (child==0) { LeapYear(); }else if( child > 0 ) { waitpid( child, NULL, 0) ; } break; case 4: if ((child=fork())==-1) { printf("error......\n"); }else if (child>0) { CopyFile(); }else if( child == 0 ) { waitpid( child, NULL, 0) ; } break; case 5: if ((child=fork())==-1) { printf("error......\n"); }else if (child==0) { SortNum(); }else if( child > 0 ) { waitpid( child, NULL, 0) ; } break; case 6: system("exit"); break; default: break; } }}int main(){ menu(); return 0;}

转载地址:http://abhao.baihongyu.com/

你可能感兴趣的文章
CCF201412试题
查看>>
HDU1164 Eddy's research I【素因子分解】
查看>>
学习前端工程师手册--学习记录
查看>>
9段高效率开发PHP程序的代码
查看>>
纯干货分享: 如何在 React 框架中使用SpreadJS
查看>>
机器学习实战之logistic回归
查看>>
SQL优化三板斧:精简之道、驱动为王、集合为本
查看>>
MVC中实现部分内容异步加载
查看>>
PTA编程总结2:
查看>>
《C++常见问题及解答》
查看>>
剑指OFFER——顺时针打印矩阵
查看>>
Live Archive 3490 - Generator 概率
查看>>
洛谷 1417 烹调方案
查看>>
Oracle SQL Developer
查看>>
基于DDD的.NET开发框架 - ABP初探
查看>>
Redis持久化机制
查看>>
dede的使用-2
查看>>
C++银行储蓄程序代码
查看>>
Java 线程池框架核心代码分析
查看>>
第六次作业:素数判断及求和
查看>>