操作系统第三次实验——线程基础总结
操作系统第三次实验——线程基础总结
文章目录
- 操作系统第三次实验——线程基础总结
- 查看CPU核数
- 创建简单线程
- ps -eLF 查看线程
- 向线程中传递参数
- 传很多个参数
- 创建两个线程实现相关操作
查看CPU核数
在虚拟机中配置好核数后用
lscpu 查看
如图为4个CPU
创建简单线程
重要函数为
pthread_create()
pthread_join()
pthread_exit()
#include <pthread.h>
#include <stdio.h>void *runner(void *param);
int main(int argc, char *argv[])
{pthread_t tid;pthread_attr_t attr;pthread_attr_init(&attr);pthread_create(&tid,&attr,runner,NULL);printf("THis is in parent thread!\n");pthread_join(tid,NULL);
}void *runner(void *param)
{printf("This is in child thread\n");while(1);pthread_exit(NULL);
}
如图,线程调用了 runner函数
ps -eLF 查看线程
如图有两个
向线程中传递参数
#include <pthread.h>
#include <stdio.h>void *runner(void *param);int main(int argc, char *argv[])
{ int argument=12345;pthread_t tid;pthread_attr_t attr;pthread_attr_init(&attr);pthread_create(&tid,&attr,runner,&argument);printf("THis is in parent thread!\n");pthread_join(tid,NULL);
}void *runner(void *param)
{ printf("The passed argument to the created thread:%d\n",*(int*)param);pthread_exit(NULL);
}
argument 为传递的参数(第四个参数)
结果如下:
传很多个参数
在线程中传递两个参数,实现加法操作
#include <pthread.h>
#include <stdio.h>void *runner(void *param);
struct twoint
{int first;int second;
};
int main(int argc, char *argv[])
{ struct twoint argument;argument.first=1;argument.second=2;pthread_t tid;pthread_attr_t attr;pthread_attr_init(&attr);pthread_create(&tid,&attr,runner,&argument);printf("THis is in parent thread!\n");pthread_join(tid,NULL);
}void *runner(void *param)
{ printf("this is in child thread\n");printf("the sum of the passed two param:%d",((struct twoint*)param)->first+((struct twoint*)param)->second);pthread_exit(NULL);
}
如上述代码实现1+2操作
结果如下:
创建两个线程实现相关操作
#include <pthread.h>
#include <stdio.h>
int sum=0;
void *runner(void *param);int main(int argc, char *argv[])
{ pthread_t tid1,tid2;pthread_attr_t attr;pthread_attr_init(&attr);printf("The sum is:%d\n",sum);pthread_create(&tid1,&attr,runner,NULL);pthread_create(&tid2,&attr,runner,NULL);pthread_join(tid1,NULL);pthread_join(tid1,NULL);
}void *runner(void *param)
{ printf("The sum is:%d\n",++sum);pthread_exit(NULL);
}
结果如下:
两个线程,把sum这个全局变量都加上了1
操作系统第三次实验——线程基础总结相关推荐
- Java基础 - 坦克大战(第三章,线程基础与线程使用)
文章目录 本章内容 - 多线程处理 绘制敌方坦克 线程基础 线程相关概念 程序(program) 进程 什么是线程 其他相关概念 单线程 多线程 并发 并行 获取当前电脑处理器(cpu)个数 Java ...
- 操作系统【三】内存管理基础+连续内存分配
内存的基础知识 内存分为按字节编址(8位)和字编制(不同计算机不一样,64位计算机就是64位,即8个字节) 相对地址=逻辑地址 绝对地址=物理地址 从逻辑地址到物理地址的转换由装入解决. 装入的三种方 ...
- java 线程 操作系统线程_线程基础:线程(1)——操作系统和线程原理
1.概述 我在写"系统间通信技术专栏"的时候,收到很多读者的反馈.其中有一部分读者希望我抽空写一写自己关于对Java线程的使用经验和总结.巧的是,这个月我所在的技术团队也有很多同事 ...
- 线程基础知识系列(三)线程的同步
本文是系列的第三篇,前面2篇,主要是针对单个线程如何管理,启动等,没有过多涉及多个线程是如何协同工作的. 线程基础知识系列(二)线程的管理 :线程的状态,控制,休眠,Interrupt,yield等 ...
- Oracle 实验三: SQL语句基础(复习)
实验三: SQL语句基础(复习) 一.实验目的 1.oracle中常用数据字典的使用. 2.复习并掌握表管理(创建.修改.创建约束.删除等)操作 二.实验内容 1.利用数据字典完成以下操作,对运行过程 ...
- 计算机基础课件第三章ppt,计算机应用基础(windows 7+office 2010)课件 第三章 Windows7操作系统.ppt.pdf-汇文网...
计算机应用基础(windows 7+office 2010)课件 第三章 Windows7操作系统.ppt.pdf-汇文网 计算机应用基础 ( windows 7office 2010) Window ...
- [CLR via C#]25. 线程基础
原文:[CLR via C#]25. 线程基础 一.Windows为什么要支持线程 Microsoft设计OS内核时,他们决定在一个进程(process)中运行应用程序的每个实例.进程不过是应用程序的 ...
- Windows核心编程 第六章 线程基础知识 (上)
第6章 线程的基础知识 理解线程是非常关键的,因为每个进程至少需要一个线程.本章将更加详细地介绍线程的知识.尤其是要讲述进程与线程之间存在多大的差别,它们各自具有什么作用.还要介绍系统如何使用线程内核 ...
- 线程基础知识——Windows核心编程学习手札系列之六
线程基础知识 --Windows核心编程学习手札系列之六 线程与进程一样由两部分构成:一是线程的内核对象,操作系统用它来对线程实施管理,也是系统用来存放线程统计信息的地方:二是线程堆栈,用于维护线程在 ...
最新文章
- Xcache安装与使用
- Ubuntu操作系统安装之开发人员必备
- call、bind与apply
- python制作工资计算器-Python计算个人所得税
- 【Accelerated C++】重点回顾
- centos6.4下配置nginx服务器更改根目录
- 饿了么CTO张雪峰:允许90后的技术人员“浮躁“一点
- C++ COM编程之什么是组件
- 单元测试Junit 测试类与测试方法 java
- HTML5定位功能,实现在百度地图上定位
- 阿里巴巴Android开发手册V1.0.0随手笔记
- 垃圾收集器分类与GC性能指标
- java流程图if_if判断流程图怎么画?教你简单制作程序流程图的方法
- 信息学奥赛一本通知识集锦+往年真题
- 给闪讯加装路由器的一个简单方法
- 计算机 在哪看是什么32位,如何查看自己的电脑是32位的还是64位
- 流量控制理论与Sentinel
- 一篇文章了解什么是增长黑客
- minio 上传文件失败报错信息: The difference between the request time and the server‘s time is too large.
- 二维码解码器Zbar+VS2010开发环境配置