(Linux)操作系统——线程实验

操作系统一实验3
线程
■目的
深入理解线程及线程在调度执行和内存空间等方面的特点, 并掌握线程与进程的区别。掌握POSIX规范中pthread create( )函数的功能和使用方法。
■实验前准备
阅读参考资料,了解线程的创建等相关系统调用

实验准备:
线程创建函数:pthread_create(&pthread_id[i] , NULL , thread_work , ( void* ) & ( index[ i ] ) );
第一个参数:把创建出来的线程的编号传给pthread_id[ i ];
第二个参数:一般为NULL;
第三个参数:简单理解为该线程执行什么样的工作。
第四个参数:一般来说把第四个参数传到第三个参数中。

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<ctype.h>
#include<pthread.h>

#define MAX_THREAD 3
unsigned long long main_counter,counter[MAX_THREAD];
void* thread_work(void*);
int main(int argc,char*argv[])
{
	int i,rtn,ch;
	int index[MAX_THREAD];
	
	pthread_t pthread_id[MAX_THREAD] = {0};
	for(int i = 0 ; i < MAX_THREAD;i++)
	{
		index[i] = i;
		pthread_create(&pthread_id[i],NULL,thread_work,(void *)&(index[i]));//把整数类型的地址转换成void类型的地址,
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值