Linux编程 多进程,多线程求解PI(圆周率)

题目

链接


多进程

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define n 100000000.0

int main()
{
    int fd[2];
    // 创建二个 fd, fd[0] 管道用于读, fd[1] 管道用于写
    pipe(fd);

    // 创建进程
    pid_t pid;
    for(int i = 0; i < 8; ++i)
    {
        pid = fork();
        if(pid == 0)
        {
            double sum = 0.0;
            for(int j = (int)(i*(n/8)); j < (int)((i+1)*(n/8)); j++)
            {    
                double temp =  (4 / (1 + ((j + 0.5)/n)*((j + 0.5)/n)))*(1.0/n);
                sum += temp;
            }
            write(fd[1], &sum, sizeof(double));
            exit(0);
        }
    }
    //计算求和
    double ans = 0.0;
    for(int i = 0; i < 8; ++i)
    {
        double temp;
        read(fd[0], &temp, sizeof(double));
        ans += temp;
    }
    
    printf("the ans is %.20lf
", ans);
    
    return 0;
}
View Code

多线程

#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 !=0){
        printf("Thread creation failed");
        exit(1);
    }
    
    check_create = pthread_create(&pthread2, NULL, sumpi2, (void *)&n);
    
    if(check_create !=0){
        printf("Thread creation failed");
        exit(1);
    }
    
    //等待第一个线程退出,并接收它的返回值(返回值存储在thread1_return)
    check_end = pthread_join(pthread1, &thread1_return);
    
    if(check_end !=0 ){
        printf("Thread end failed");
        exit(1);
    }
    printf("%.20lf
" ,*(double *)thread1_return);
    //cout << thread1_return << endl;
    ans += (*(double*)thread1_return);
    
    
    
    //等待第二个线程退出,并接收它的返回值(返回值存储在thread2_return)
    check_end = pthread_join(pthread2, &thread2_return);
    
    if(check_end !=0 ){
        printf("Thread end failed");
        exit(1);
    }
    printf("%.20lf
" ,*(double *)thread2_return);
    ans += (*(double*)thread2_return);
    
    printf("the pi is %.20lf
" ,ans);
    
    return 0;
}

void * sumpi1(void * arg)
{
    double n = *(double *)arg;
    ret1 = 0.0;
    for(int i = 0; i < n/2; i++)
    {
        double temp =  (4 / (1 + ((i + 0.5)/n)*((i + 0.5)/n)))*(1.0/n);
        ret1 += temp;
    }
    //printf("%.20lf
", ret1);
    //double * 
    pret1 = &ret1;
    pthread_exit((void*)pret1);
}

void * sumpi2(void * arg)
{
    double n = *(double *)arg;
    ret = 0.0;
    for(int i = n/2; i <= n; i++)
    {
        double temp =  (4 / (1 + ((i + 0.5)/n)*((i + 0.5)/n)))*(1.0/n);
        ret += temp;
    }
    //double * 
    pret = &ret;
    //printf("%.20lf
", *pret);
    pthread_exit((void *)pret);
}
View Code
原文地址:https://www.cnblogs.com/solvit/p/9894746.html