实验一 Linux多线程编程I
一、 实验目的
熟悉GNU GCC编译器,能够用Pthreads线程库熟练编写多线程程序。
二、实验内容
1、设一个double型的一维数组,数组长度是1,000,000,计算全数组部元素的和。
要求:
1) 编制一个串行程序,仅有一个主线程完成上述计算。
2)编制一个多线程程序,除主线程外,派生四个子线程完成上述计算。
分别获得以上两个程序的计算时间,对比分析。
2 定义以下三个数组:
    #define LEN 100000;
    Double A[LEN], B[LEN], C[LEN];
    for (i=0; i++;i<LEN)
{
      C[i]=sin(i);
          B[i]=C[i]*2;
      A[i]=B[i]+C[i]
}
分别编制一段串行程序和一段多线程程序(包括四个线程),执行上述循环体,比较其执行所花费的时间。
提示:clock()C/C++中的计时函数,与其相关的数据类型是clock_t,通过clock()可以获得当前的系统时间。
四、实验步骤
1、打开Linux终端
2、输入命令$ emacs hello.c & (文件名可以自定义)
3、编制源程序,注意加入必要的头文件  #include <pthread.h>,对于信号量,还需要加头文件 <semaphore.h>
4、编译:$gcc -o hello hello.c –lpthread 对于是信号量,编译时还需要加-lrt 参数。
5、执行: ./hello
五、实验要求
写出完整的源程序,给出程序执行的结果。
串行实验源代码如下:
#include <pthread.h>
#include <semaphore.h>
#include<stdio.h>
#include<time.h>
#define LEN 1000000
void main()
{
time_t t1,t2;
double D[LEN];
int i;
double sum=0.0;
t1=clock();
for(i=0;i<LEN;i++)
{
sum+=D[i];
}
t2=clock();
printf("sum=%d\n",t2-t1);
}
执行结果:
多线程实验源代码如下:
#include <pthread.h>
#include <stdio.h>
#include <time.h>
c编程步骤#include <string.h>
#define LEN 1000000
pthread_t thread[4];
pthread_mutex_t mut;
int number=0, i;
double D[LEN];
double sum=0.0;
void *thread1()
{
        for (i = 0; i < LEN; i++)
        {
                pthread_mutex_lock(&mut);
                        sum+=D[i];
                pthread_mutex_unlock(&mut);
             
        }
        pthread_exit(NULL);
}
void *thread2()
{
 
        for (i = 0; i < LEN; i++)
        {
                pthread_mutex_lock(&mut);
                        sum+=D[i];
                pthread_mutex_unlock(&mut);
             
        } 
        pthread_exit(NULL);
}
void *thread3()
{
        for (i = 0; i < LEN; i++)
        {
                pthread_mutex_lock(&mut);
                        sum+=D[i];
                pthread_mutex_unlock(&mut);
               
        }
        pthread_exit(NULL);
}
void *thread4()
{
        for (i = 0; i < LEN; i++)
        {
                pthread_mutex_lock(&mut);
                        sum+=D[i];
                pthread_mutex_unlock(&mut);
             
        }
        pthread_exit(NULL);
}
void thread_create(void)
{
        int temp;
        memset(&thread, 0, sizeof(thread));       
        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)                      printf("线程1创建失败!\n");
        else
                printf("线程1被创建\n");
        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) 
                printf("线程2创建失败");
        else
                printf("线程2被创建\n");
        if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0) 
                printf("线程3创建失败");
        else
                printf("线程3被创建\n");
      if((temp = pthread_create(&thread[3], NULL, thread4, NULL)) != 0) 
                printf("线程4创建失败");
        else
                printf("线程4被创建\n");
}
void thread_wait(void)
{
                if(thread[0] !=0) {                 
                pthread_join(thread[0],NULL);
        }
                if(thread[1] !=0) {             
                pthread_join(thread[1],NULL);
        }
                if(thread[2] !=0) {             
                pthread_join(thread[2],NULL);
        }