3 POSIX 多任务及同步机制-实验2: POSIX线程机制

3 POSIX 多任务及同步机制-实验2: POSIX线程机制

一.实验目的

·通过实验深入理解操作系统的进程概念、线程概念,Linux的进程概念和线程概念
·理解POSIX线程机制。
·通过实验深入理解操作系统中的进程和线程的并发问题和同步问题

二.实验背景

·什么是POSIX进程概念
·ANSI C
·POSIX
·POSIX 标准中的 pthread 库提供了线程库的相关函数,包括线程的创建、撤销和等待等操作,头文件是<pthread.h>
在这里插入图片描述

三.关键代码及分析

void thread_create()
{
	int temp;
	memset(&thread, 0, sizeof(thread)); 
	/*create the new thread*/
	if((temp = pthread_create(&thread, NULL, my_thread, NULL)) != 0) //创建线程
		printf("Creating thread 1 has failed!\n");
	else
		printf("Thread 1 has been created! \n");	
	
}

Pthread_create() 创建线程,确定调用该线程函数的入口点。在线程创建以后,就开始运行相关的线程函数。
·函数原形int pthread_create (pthread_t *__restrict __newthread,
const pthread_attr_t *__restrict __attr,
void (__start_routine) (void *),
void *__restrict __arg) __THROWNL __nonnull ((1, 3)
);
参数说明:
·第一个参数 __restrict __newthread 表示线程的标识符
·第二个参数 __restrict __attr 表示线程属性设置
·第三个参数 __start_routine 表示线程函数的入口地址
·第四个参数 __restrict __arg 表示传递给 __start_routine 的参数
·函数返回值:成功返回0;出错时返回-1

·第二个参数pthread_attr_t 类型的变量,当为NULL时表示使用默认参数设置
在这里插入图片描述

·线程退出函数 pthread_exit

函数原型:
void pthread_exit (void *__retval) attribute ((noreturn));
在这里插入图片描述

函数功能:使用函数pthread_exit 退出线程,这是线程的主动行为。由于一个进程中的多个线程是共享数据段的,因此通常在一个线程退出之后 ,该线程所占用的资源并不会随着其终止而得到释放,但是主线程可以通过调用pthread_join()函数来同步并释放资源。

参数说明: __retval表示调用pthread_exit()线程的回值,可由其他函数,如pthread_join对其进行检索.

·等待线程结束函数pthread_join

函数原型:
int pthread_join (pthread_t __th, void **__thread_return);
功能:以阻塞的方式等待 __th 指定的线程结束。当函数返回时,被等待线程的资源被收回,如果指定的线程已经结束,该函数会立即返回
参数说明:__th 表示线程的表示符,即线程ID,表示唯一的线程;__thread_return 是用户定义的指针,用来存储被等待线程的返回值;函数返回值为0代表成功,失败返回错误号。
在这里插入图片描述

·线程函数实例:

void *my_thread()
{
	int	i, temp;
	printf ("I'm thread 1\n");
	for (i = 0; i < upper; i++) 
	{
		temp = counter;
		temp += 1;
		counter = temp;
	}
	printf("thread1 :Is Main function waiting for me ?\n");
	pthread_exit(NULL);

}

函数实现了一个对全局变量 counter 的累加计数工作。也就是说,子线程每完成一次工作,counter 就会加1。工作完成后调用 pthread_exit() 函数退出。

·等待线程实例:

void thread_wait(void)
{/*waiting for the thread finished*/	
	if(thread !=0) 
	{ 
		pthread_join(thread,NULL);//等待线程退出
		printf("Theread 1 has exited! \n");
	}	
}

四.实验结果与分析

·编译
使用POSIX 库函数的程序在编译、连接时需要显式声明使用 lpthread 库,否则会报错。
· gcc -o mythread_posix1 mythread_posix1.c //错误编译方式
在这里插入图片描述

·gcc -o mythread_posix1 mythread_posix1.c -lpthread
在这里插入图片描述

·运行程序
·需要增加一个上线值

./mythread_posix1 10

在这里插入图片描述

程序先检查参数是否合理,如果参数为2并且是正整数,则执行upper = atoi(argv[1]);printf(“I am main function, I am creating the threads! \n”);,当执行到 thread_create();创建了线程打印printf(“Thread 1 has been created! \n”),由于执行速度较快线程来不及执行,创建完成后主函数继续执行printf(“I am main function , I am waiting the threads finished! \n”),当执行thread_wait()时候,等待线程执行打印了I’m thread 1 h和thread1 :Is Main function waiting for me ? ,然后执行pthread_exit(NULL),打印Theread 1 has exited! ;线程成功退出后回到主函数执行printf(“counter = %d\n”,counter)。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Hello Spring

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

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

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

打赏作者

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

抵扣说明:

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

余额充值