【多线程编程】初次尝试 - UnixC 的三线程计算

老师布置了个作业的说……

然后就试试看写咯~




#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
//use "gcc test01.c -o thread -lpthread" to compile
int n=0;
int mode=0;
int arr[1024]={0};

void maxv(void* args)
{
        sleep(1);
        int _max=-1,i=0;
        for(i=0;i<n;i++)_max=(_max<arr[i]?arr[i]:_max);
        if(mode) while(1) {sleep(1);printf("thr_1:the maximal value is: %d\n",_max);}
        else printf("the maximal value is: %d\n",_max);
}

void minv(void* args)
{
        sleep(2);
        int _min=9999,i=0;
        for(i=0;i<n;i++)_min=(_min>arr[i]?arr[i]:_min);
        if(mode) while(1) {sleep(2);printf("thr_2:the minimal value is: %d\n",_min);}
        else printf("the minimal value is: %d\n",_min);
}

void avgv(void* args)
{
        sleep(3);
        int sum=0,i=0;
        for(i=0;i<n;i++)sum+=arr[i];
        if(mode) while(1) {sleep(3);printf("thr_3:the average value is: %d\n",sum/n);}
        else printf("the average value is: %d\n",sum/n);
}

int main()
{
        int tmp=0;
        printf("Please Input numbers split by blanks(zero means end)\n");
        while(scanf("%d",&tmp) && tmp) arr[n++]=tmp;
        printf("If you want to show how thread works input '1',otherwise '0'\n");
        scanf("%d",&mode);
        pthread_t pid1,pid2,pid3;
        printf("Author: Chendian 20125209 Class:IOT01\n");
        printf("=====Multi Pthread Program Test01=====\n");
        if(pthread_create(&pid1,NULL,maxv,NULL)){printf("pid1 is wrong\n");return -1;}
        if(pthread_create(&pid2,NULL,minv,NULL)){printf("pid2 is wrong\n");return -1;}
        if(pthread_create(&pid3,NULL,avgv,NULL)){printf("pid3 is wrong\n");return -1;}
        while(1){sleep(3);}
        printf("=====Multithread Program Test End=====\n");
        return 0;
}



需要注意的其实也不多…… 使用UnixC编的东西在Linux编译的时候需要使用gcc -c xxxxx这样的指令,

然而我们使用的是多线程,所以还少了个库需要添加,所以要多一个参数,

gcc test01.c -o thread -lpthread

这样来编译即可,作用为把 test01.c 的文件使用lpthread编译,输出为thread文件,运行的时候就

./thread
就可以啦~~~


运行结果: (啊对了average老师要求用int的,不是我不愿意用double哦~)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值