题目:

链接


多进程:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> #define n 100000000.0 int main()
{
int fd[];
// 创建二个 fd, fd[0] 管道用于读, fd[1] 管道用于写
pipe(fd); // 创建进程
pid_t pid;
for(int i = ; i < ; ++i)
{
pid = fork();
if(pid == )
{
double sum = 0.0;
for(int j = (int)(i*(n/)); j < (int)((i+)*(n/)); j++)
{
double temp = ( / ( + ((j + 0.5)/n)*((j + 0.5)/n)))*(1.0/n);
sum += temp;
}
write(fd[], &sum, sizeof(double));
exit();
}
}
//计算求和
double ans = 0.0;
for(int i = ; i < ; ++i)
{
double temp;
read(fd[], &temp, sizeof(double));
ans += temp;
} printf("the ans is %.20lf\n", ans); return ;
}

多线程:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h> void * sumpi1(void * arg);
void * sumpi2(void * arg); double * pret;
double * pret1;
double ret,ret1; int main()
{
//定义两个线程标识符
pthread_t pthread1, pthread2;
//两个线程退出后的返回值
void * thread1_return;
void * thread2_return;
//判断线程创建成功否
int check_create;
//判断线程退出成功否
int check_end; double ans = 0.0;
double n = 10000000.0;
// 创建两个线程
check_create = pthread_create(&pthread1, NULL, sumpi1, (void *)&n); if(check_create !=){
printf("Thread creation failed");
exit();
} check_create = pthread_create(&pthread2, NULL, sumpi2, (void *)&n); if(check_create !=){
printf("Thread creation failed");
exit();
} //等待第一个线程退出,并接收它的返回值(返回值存储在thread1_return)
check_end = pthread_join(pthread1, &thread1_return); if(check_end != ){
printf("Thread end failed");
exit();
}
printf("%.20lf\n" ,*(double *)thread1_return);
//cout << thread1_return << endl;
ans += (*(double*)thread1_return); //等待第二个线程退出,并接收它的返回值(返回值存储在thread2_return)
check_end = pthread_join(pthread2, &thread2_return); if(check_end != ){
printf("Thread end failed");
exit();
}
printf("%.20lf\n" ,*(double *)thread2_return);
ans += (*(double*)thread2_return); printf("the pi is %.20lf\n" ,ans); return ;
} void * sumpi1(void * arg)
{
double n = *(double *)arg;
ret1 = 0.0;
for(int i = ; i < n/; i++)
{
double temp = ( / ( + ((i + 0.5)/n)*((i + 0.5)/n)))*(1.0/n);
ret1 += temp;
}
//printf("%.20lf\n", ret1);
//double *
pret1 = &ret1;
pthread_exit((void*)pret1);
} void * sumpi2(void * arg)
{
double n = *(double *)arg;
ret = 0.0;
for(int i = n/; i <= n; i++)
{
double temp = ( / ( + ((i + 0.5)/n)*((i + 0.5)/n)))*(1.0/n);
ret += temp;
}
//double *
pret = &ret;
//printf("%.20lf\n", *pret);
pthread_exit((void *)pret);
}

Linux编程 多进程,多线程求解PI(圆周率)的更多相关文章

  1. [转帖]Windows和Linux对决(多进程多线程)

    Windows和Linux对决(多进程多线程) https://blog.csdn.net/world_2015/article/details/44920467 太长了 还没看完.. 还是没太理解好 ...

  2. linux编程之多线程编程

    我们知道,进程在各自独立的地址空间中运行,进程之间共享数据需要用mmap或者进程间通信机制,有些情况需要在一个进程中同时执行多个控制流程,这时候线程就派上了用场,比如实现一个图形界面的下载软件,一方面 ...

  3. 【转】 Linux下的多线程编程

    作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/07/280 ...

  4. Linux下的多线程编程

    1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...

  5. 【转】Linux下的多线程编程

    1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...

  6. 《转》Linux下的多线程编程

    原地址:http://linux.chinaunix.net/doc/program/2001-08-11/642.shtml 1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程 ...

  7. Linux 多进程多线程相关概念

    进程:可执行程序是存储在磁盘设备上的由代码和数据按某种格式组织的静态实体,而进程是可被调度的代码的动态运行.在Linux系统中,每个进程都有各自的生命周期.在一个进程的生命周期中,都有各自的运行环境以 ...

  8. Cpython解释器下实现并发编程——多进程、多线程、协程、IO模型

    一.背景知识 进程即正在执行的一个过程.进程是对正在运行的程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有内容都 ...

  9. linux 下 多进程与多线程

    [Linux]多进程与多线程之间的区别 http://blog.csdn.net/byrsongqq/article/details/6339240 网络编程中设计并发服务器,使用多进程与多线程 ,请 ...

随机推荐

  1. Attribute 'num_units' in Tensorflow BasicLSTMCell blocks

    在之前使用Tensorflow来做音乐识别时,LSTM给出了非常让人惊喜的学习能力.当时在进行Tuning的时候,有一个参数叫做num_units,字面看来是LTSM单元的个数,但最近当我试图阅读Te ...

  2. C#静态变量总结

    1.初始化 全局static变量的初始化在编译的时候进行,并且只初始化一次 . 函数static变量在函数中有效,第一次进入函数初始化.以后进入函数将沿用上一次的值.  2.生命期 全局static变 ...

  3. 15.队列Queue的特点以及使用,优先级等

    #生产者与消费者模式,模式解释:比如MVC设计模式 ''' 1.队列 (1)特点:先进先出 (2)python2 VS python3 python2:from Queue import queue ...

  4. Tensorflow--Keras官方原文

    Keras 是一个用于构建和训练深度学习模型的高阶 API(应用程序接口).它可用于快速设计原型.高级研究和生产,具有以下三个主要优势: 方便用户使用 Keras 具有针对常见用例做出优化的简单而一致 ...

  5. System的两常用个静态方法

    package cn.learn; /* System类在java.lang.System,和操作系统有关 1.currentTimeMillis直接调用,是一个返回为long型的静态方法 常用来计算 ...

  6. 最小公倍数(lcm与gcd)

    J - Worker Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard p ...

  7. [暑假集训Day4T2]卡拉赞之夜

    抹茶学长给的标程可以被卡到O(N2M2)??? 考虑二分答案+暴力check+离散化+卡常数 首先进行离散化,其实判重的话会更快,但是由于矩阵元素大小太大了,hash判重MLE,所以我就直接记录了NM ...

  8. CodeChef Gcd Queries

    Gcd Queries   Problem code: GCDQ   Submit All Submissions   All submissions for this problem are ava ...

  9. CodeChef Little Elephant and Balance

    Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that L ...

  10. python学习第十七天字符串的创建和操作方法

    字符串也是任何编程语言最常见的编程语言,字符串是有序的,可以通过下标来访问,可以切片,可以查找,可以替换,字符串可以和列表之间互相转换 join() split() 等函数 1,字符串的创建 单引号 ...