【linux】——一个小程序
利用工作之余为小伙伴写了份作业,关于进程间通信的。题目如下:
|
父进程从键盘上接受1000个数据,对其求和sum1,子进程对这1000个数平方和sum2,结果传给父进程,父进程将sum1+sum2后,打印结果。 要求:用大小为10的共享区传递1000个数据;子进程用消息机制将sum2传给父进程。 |
主要利用共享内存实现进程间通信,使用管道实现进程间竞争关系,FreeBSD下测试通过。代码如下:时间有限,有可能有些不足,希望高手给予指点。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <signal.h> const int key = 0x12345678;
static int pfd1[], pfd2[]; #define SHM_LEN (10*1024)
#define VAL_NUM 5 int init_shm() {
int shmid = -; shmid = shmget((key_t)key, SHM_LEN, | IPC_CREAT);
if (shmid < ) {
printf("shmget failed!\n");
exit(-);
} return shmid;
} void cancel_shm(int shmid) {
if (shmctl(shmid, IPC_RMID, ) == -) {
printf("shmctl failed!\n");
exit(-);
}
printf("cancel_shm successfully!\n");
} void *shm_get(int shmid) {
void *mem = NULL; mem = shmat(shmid, , );
if (mem == (void *)-) {
printf("shmat failed!\n");
exit(-);
} return mem;
} int get_val(int *val, int num) {
int i;
for (i = ; i < num; i++) {
printf("please input a num:");
scanf("%d", val + i);
}
}
void show_val (int *val, int num) {
int i;
for (i = ; i < num; i++) {
printf("%d\t", *(val + i));
}
printf("\n");
} int add_val (int *val, int num) {
int result = ;
int i; for (i = ; i < num; i++) {
result += *(val + i);
} return result;
} int square_val (int *val, int num) {
int result = ;
int i, tmp; for (i = ; i < num; i++) {
tmp = *(val + i);
result += (tmp * tmp);
} return result;
} void TELL_WAIT (void) {
if (pipe(pfd1) < || pipe(pfd2) < ) {
printf("pipe error!\n");
exit(-);
}
} void TELL_PARENT (pid_t pid) {
if (write(pfd2[], "c", ) != ) {
printf("write error!\n");
exit(-);
}
} void WAIT_PARENT (void) {
char c; if (read(pfd1[], &c, ) != ) {
printf("read error!\n");
exit(-);
}
} void TELL_CHILD (pid_t pid) {
if (write(pfd1[], "p", ) != ) {
printf("write error!\n");
exit(-);
}
} void WAIT_CHILD (void) {
char c; if (read(pfd2[], &c, ) != ) {
printf("read error!\n");
exit(-);
}
} int main(int argc, char *argv[]) {
void *mem = NULL;
int shmid = -;
pid_t pid = -;
int val[VAL_NUM];
int result = ; shmid = init_shm(); TELL_WAIT();
if ((pid = fork()) < ) { //error
printf("fork error!\n");
exit(-);
} else if (pid == ) { //child
int result = ; WAIT_PARENT(); mem = shm_get(shmid); //get share memery memcpy(val, mem, sizeof(int) * VAL_NUM);
result = square_val(val, VAL_NUM); *(int *)((void *)mem + SHM_LEN - ) = result; TELL_PARENT(pid); exit();
} else { //parent
int child_result = ; mem = shm_get(shmid); //get share memery
get_val(val, VAL_NUM); //get user input
memcpy(mem, val, sizeof(int) * VAL_NUM); //copy user input to share memery TELL_CHILD(pid); result = add_val(val, VAL_NUM); WAIT_CHILD();
child_result = *(int *)((void *)mem + SHM_LEN - );
printf("result:%d, child_result:%d, all:%d\n", result, child_result, result + child_result);
} cancel_shm(shmid); return ;
}
【linux】——一个小程序的更多相关文章
- 【Java】一个小程序,计算它包含的代码所需的耗时
写一个小程序,用来计算它包含的代码所需的耗时.虽然简单,测试代码是否耗时还是有点用的,不用重新写嘛~ import java.util.Date; import java.util.concurren ...
- c++学习笔记---04---从另一个小程序接着说
从另一个小程序接着说 文件I/O 前边我们已经给大家简单介绍和演示过C和C++在终端I/O处理上的异同点. 现在我们接着来研究文件I/O. 编程任务:编写一个文件复制程序,功能实现将一个文件复制到另一 ...
- c++学习笔记---03---从一个小程序说起2
从一个小程序说起2 要求:编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行中的任何位置.当用户按下键盘上的"Enter"键时,数据输入 ...
- c++学习笔记---02---从一个小程序说起
从一个小程序说起 这一讲的主要目的是帮助大家在C语言的背景知识上与C++建立联系. 问题探索 问题:对一个整型数组求和. 要求:定义一个存储着 n 个元素的数组,要求用C语言完成这个任务. 赶紧的:大 ...
- Python 练习冊,每天一个小程序
Python 练习冊,每天一个小程序 说明: Github 原文地址: 点击打开链接 Python 练习冊.每天一个小程序.注:将 Python 换成其它语言,大多数题目也试用 不会出现诸如「 ...
- 微信小程序开发——打开另一个小程序
微信小程序打开另一个小程序,有两种方法:1.超链接:2.点击按钮. 全局配置: 跳转到其他小程序,需要在当前小程序全局配置中配置需要跳转的小程序列表,代码如下: App.json { ... &quo ...
- 微信小程序如何跳转到另一个小程序
微信小程序如何跳转到另一个小程序,要注意:在app.json文件里也要配置 navigateToMiniProgramAppIdList,如下图: "navigateToMiniProgra ...
- 【小程序】微信小程序打开其他小程序(打开同一主体公众号下关联的另一个小程序)
微信小程序打开其他小程序(打开同一公众号下关联的另一个小程序) 注:只有同一(主体)公众号下的关联的小程序之间才可相互跳转 wx.navigateToMiniProgram(OBJECT) wx.n ...
- 微信小程序之怎样识别一个小程序用户
本节主要是说下怎样识别一个小程序的用户,需要用什么数据来做标识呢: 我们应该都知道判断是不是一个用户大部分都是通过userid来判断,如果这个用户访问的应用发送了一个请求,把userid之类的数据发给 ...
- 用OllyDbg爆破一个小程序
用OllyDbg爆破一个小程序 一.TraceMe小程序 TraceMe是对用户名.序列号判断是否合法的一个小程序.我们任意输入一组用户名.序列号进行check判断,结果如下: 二.用OllyDbg对 ...
随机推荐
- CSS margin属性与用法教程
margin 属性是css用于在一个声明中设置所有 margin 属性的简写属性,margin是css控制块级元素之间的距离, 它们之间是透明不可见的. margin属性包含了margin left ...
- react-native 入门教程
http://blog.csdn.net/a_zhon/article/category/7170315 几篇文章看下来基本就入门了
- struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)
JSON主要创建如下两种数据对象: 由JSON格式字符串创建,转换成JavaScript的Object对象: 由JSON格式字符串创建,转换成JavaScript的List或数组链表对象. 更多关于J ...
- JS 在 IE9 中出现奇怪的错误(参数是必选项 argument not optional)
最近发现之前运行正常的网站,在 IE9 下会报这个错误.网上查了一下,发现是跟我的方法名字有关... 我起了一个叫做 addFilter 名字的方法,但是很不巧,IE9 里也有一个这个名字的方法,所以 ...
- Netty服务器线程模型概览
一切从ServerBootstrap开始 ServerBootstrap负责初始话netty服务器,并且开始监听端口的socket请求. bootstrap bootstrap =newServerB ...
- Atitti dbutil获取多个返回结果集的解决
Atitti dbutil获取多个返回结果集的解决 1.1. 多个select默认只返回第一个resultset1 1.2. 调用存储过程,也是返回第一个select的1 1.3. 如果insert前 ...
- windows 内存管理的几种方式及其优缺点
windows 内存管理方式主要分为:页式管理,段式管理,段页式管理. 页式管理的基本原理是将各进程的虚拟空间划分为若干个长度相等的页:页式管理把内存空间按照页的大小划分成片或者页面,然后把页式虚拟地 ...
- 我的IT之路2013(二)
严寒即将过去,温暖的春天正在向我们招手,欢呼吧,在迎接新的开始的同时,不要忘了回顾一下过去的这一年,总结一下过去的这一年有什么得失. 英语学习 13年下半年,最大的变化就是有很大一部分时间用来学英语. ...
- [Windows Azure]The Autoscaling Application Block
The Autoscaling Application Block 5 out of 6 rated this helpful - Rate this topic ...
- 每日英语:China's Wistful Wen Gets His Wish
As his term as premier was drawing to a close, Wen Jiabao reflected wistfully on some of the goals h ...