1.
实验内容
学会使用
pthread_attr_init()
初始化线程参数。学会使用
pthread_create()
函数创建线程,并使用其
参数指定线程执行的函数。学会使用
pthread_join()
函数调整线程的执行次序,测验线程的调度。
了解线程竞争范围的基本知识。学会使用
pthread_attr_getscope
函数获取当前线程的竞争范围;学
会使用
pthread_attr_setscope
函数尝试更改线程的竞争范围。
任务一:
创建两个线程,线程
1
执行
runner01
函数,线程
2
执行
runner02
函数,并且要求运行参数采用命令
行参数的方式从命令中获取。
void *runner01(void *param)
{
int i, upper = atoi(param);
for(i=1; i<=upper; i++) sum += i;
printf("[thread 1] sum = %d\n", sum);
}
void *runner02(void *param)
{
int i, lower = atoi(param);
for(i=1; i<=lower; i++) sum -= i;
printf("[thread 2] sum = %d\n", sum);
}
仿照实验
2
的
test
脚本,编写脚本使程序执行
100
次,观察运行结果。
程序代码:
//需要用到的头文件
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int sum =0;
void* runner01(void *param);
void* runner02(void *param);//声明
//main
int main( int argc, char*argv[])
{
//声明两个线程
pthread_t tid01 , tid02;
pthread_attr_t attr;
//命令行参数的获取
if(argc != 2){//在命令行输入两个参数,即:/.test5 6
fprintf(stderr,"usage:a.out<integer value>\n");
return -1;
}
if(atoi(argv[1]) <= 0){
fprintf(stderr,"%d must be > 0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);// get default setting
pthread_create(&tid01,&attr,runner01,argv[1]);//第一个
pthread_create(&tid02,&attr,runner02,argv[1]);//第二个
pthread_exit(0);
}
//runner01
void *runner01(void *param)
{
int i, upper = atoi(param);
for(i=1; i<=upper; i++) sum += i;
printf("[thread 1] sum = %d\n", sum);
}
//runner02
void *runner02(void *param)
{
int i, lower = atoi(param);
for(i=1; i<=lower; i++) sum -= i;
printf("[thread 2] sum = %d\n", sum);
}
脚本代码:
#!/bin/bash
for((i=1;$i<=100;i++));
do
echo $i
./test5 6
done
~
实验结果:
运行程序
运行脚本
遇到的问题和解决方法
1.显示未引用。解决办法:编译:
gcc -o test5 test5.c -lpthread
2.脚本运行没有权限。解决办法:
sudo chmod 777 test.sh
任务二:
将程序改写为线程
1
先运行,线程
1
运行结束后线程
2
才能运行。编写脚本使程序执行
100
次,观察运 行结果。 针对于本实验中任务一、二出现的结果的区别请在实验报告中予以解释和说明。
程序代码:在任务一创建线程后添加:
pthread_join(tid01,NULL);
pthread_join(tid02,NULL);
实验结果
任务三:
了解线程竞争范围的相关概念。线程一共有两种竞争范围,分别是进程竞争范围和系统竞争范围。
实验探究本机线程之间的竞争范围。
首先使用
pthread_attr_getscope
函数获取当前线程的竞争范围,随后使用
pthread_attr_setscope
函数尝试更改线程的竞争范围,尝试更改后再次查看当前线程的竞争范围。根据实验结果得出当前机器 支持的线程竞争范围是什么。
程序代码:
//头文件
#include <pthread.h>
#include <stdio.h>
//main
int main()
{
int scope;
pthread_attr_t attr;
pthread_attr_init(&attr);
if(pthread_attr_getscope(&attr,&scope)!=0)
fprintf(stderr,"Unable to get scheduling scope\n");
else{
if(scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS\n");
else if(scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM\n");
else
fprintf(stderr,"Illegal scope value.\n");
}
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
if(pthread_attr_getscope(&attr,&scope)!=0)
fprintf(stderr,"Unable to get scheduling scope\n");
else{
if(scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS\n");
else if(scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM\n");
else
fprintf(stderr,"Illegal scope value.\n");
}
pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
if(pthread_attr_getscope(&attr,&scope)!=0)
fprintf(stderr,"Unable to get scheduling scope\n");
else{
if(scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS\n");
else if(scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM\n");
else
fprintf(stderr,"Illegal scope value.\n");
}
}
实验结果: