嵌入式linux多进程编程
嵌入式linux多进程编程
在主程序显示文本菜单。提供例如以下服务。要求每一个服务都通过生成子进程来提供。
服务包含:日历信息显示,日期信息显示,推断闰年服务,文件复制功能,数字排序功能。退出功能。
代码和文档(有流程图的下载地址):http://download.csdn.net/download/jingjingxujiayou/7540893
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h> 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]<b[j])
{
k=j;
}
if(i!=k)
{
t=b[i];
b[i]=b[k];
b[k]=t;
}
}
printf("从大到小的顺序为;\n");
for(i=0;i<10;i++)
printf("%d\t",b[i]);
printf("\n");
}
void menu()
{ int choose ,k=1;
pid_t child;
while(1)
{
choose=Start();
switch (choose) {
case 1:
if ((child=fork())==-1) {
printf("error......\n");
}else if (child==0)
{
DisplayCalen();
}else if( child > 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;
}
嵌入式linux多进程编程的更多相关文章
- 嵌入式Linux串口编程简介
文章目录 简介 用到的API函数 代码 简介 嵌入式Linux下串口编程与Linux系统下的编程没有什么区别,系统API都是一样的.嵌入式设备中串口编程是很常用的,比如会对接一些传感器模块,这些模块大 ...
- Linux多进程编程
进程的状态 Linux进程有7种基础状态(两种running算一种),除了traced都可以用$ps命令查看,$ps可以查看的进程状态如下,更多进程状态信息参见Linux Process VS Thr ...
- Yocto开发笔记之《嵌入式linux libcurl编程》(QQ交流群:519230208)
开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...
- Linux多进程编程实例
前言:编写多进程程序时,我们应该了解一下,创建一个子进程时,操作系统内核是怎样做的.当通过fork函数创建新的子进程时,内核将父进程的用户地址空间的内容复制给子进程,这样父子进程拥有各自独立的用户空间 ...
- 嵌入式Linux 网络编程
涉及到的数据结构: 下面首先介绍两个重要的数据类型:sockaddr和sockaddr_in,这两个结构类型都是用来保存socket地址信息的 定义如下所示: struct sockaddr { un ...
- 嵌入式linux串口编程(二)
/*com_writer.c*/#include "uart_api.h" int main(void){ int fd; char buff[BUFFER_SIZE]; if(( ...
- Linux 多进程编程实例(一)
文章目录 目标: main.c process1.c process2.c 目标: 一个进程,创建两个子进程,利用exec函数族使两个子进程执行不同的程序.子进程1执行ls -l命令后正常返回,子进程 ...
- 【分享】4412开发板-嵌入式Linux开发须要掌握的基础知识和技能
本文转自迅为电子论坛:http://www.topeetboard.com 1.Linux 基础 安装Linux操作系统 Linux文件系统 Linux经常使用命令 Linux启动过程具体解释 熟悉L ...
- Linux下的多进程编程
1.进程 1.1进程的定义 <计算机操作系统>这门课对进程有这样的描述:进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统 ...
随机推荐
- erlang 初体验
近期測试了一下 erlang的坑... 如不出意外.... 大家第一眼看到这语法... 心里第一句一定是"我擦.这TM都是啥!!!!!" 没有变量!!! 没有结构体!!! 没有循环 ...
- LVM的一般操作过程
1. 在磁盘分区上建立物理卷 #fdisk /dev/hdb #pvdisplay /dev/hdb1 //在已经建立好的分区或硬盘上建立物理卷 #pvcreate /dev/hdb1 2 ...
- c++构造析构顺序
class A { public: A(){ cout << "constrcut A" << endl; }; ~A(){ cout << & ...
- HDOJ 2120 并查集
并查集的应用,用来查找被分割的区域个数. 即当两个节点值相同时说明已经为了一个圈,否则不可能,此时区域个数加1. #include<iostream> #include<cstdio ...
- 13-UIKit(tableviewcell贴图、手势GestureRecognizer、transform变形)
目录: 一.tableviewcell贴图 二.手势GestureRecognizer 5.1 Tap(按一下) 5.2 Swipe(轻扫一下) 5.3 pinch(捏/扩) 5.4 longPres ...
- 盘点:#AzureChat - 虚拟机和自动伸缩
感谢大家跟 Corey Sanders 和 Stephen Siciliano 一起参加本次 #AzureChat.我们很高兴能借这次在线讨论的机会,倾听各位社区成员对我们最受欢迎的两个主题的意见 - ...
- CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方
CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方 ...
- linux c setitimer使用方法说明
在linux c编程中.setitimer是一个比較经常使用的函数.可用来实现延时和定时的功能,网上有各种零零散散的使用方法说明,都仅仅提到了个别使用方法,今天抽出时间实践整理了一份比較具体的: 使用 ...
- 第十一章 认识与学习BASH
系统支持的shell在 /etc/shells里面 Bash Shell 的功能: 1.命令修补能力(histroy) 2.命令与档案补全功能 3.命令别名设定功能 4.工作前景背景控制 5.支持 ...
- css图片上下垂直居中
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...