- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
os03_2知识点和上机作业
第二三章:进程与线程
掌握要点:
掌握并发执行的特征与条件。
理解进程的概念与状态。
linux的进程(任务)控制块都包含了进程的那些信息?
进程间通信有那几种方式!各自的工作原理?
简述进程的创建过程以及fork函数与vfork函数的区别。
比较线程和进程的优缺点!
用户级线程和内核级线程的概念及各自特点。上机观看pth1.c运行过程中进程(线程)的变化!简述这种变化出现的原因!!
#include pthread.h
#include stdio.h
int sum/* this data is shared by the thread(s) */
void *runner(void *param); /* the thread */
main(int argc,char *argv[])
{
pthread_t tid;/* the thread indentifier */
pthread_attr_t attr;/*set of thread attributes */
if(argc!=2){
fprintf(stderr,usage; %s interger value\n,argv[0]);
exit();
}
if(atoi(argv[1])0){
fprintf(stderr,%d must be = 0\n,atoi(argv[1]));
exit();
}
/* get the default attributes */
pthread_attr_init(attr);
/* creat the thread */
pthread_create(tid,attr,runner,argv[1]);
/* now wait for the thread to exit */
pthread_join(tid,NULL);
printf(sum = %d\n,sum);
}
/* the thread will begin control in this function */
void *runner(void *param)
{
int upper = atoi(param);
int i;
sum = 0;
if(upper 0){
for(i=1; i=upper; i++) sum += i;
}
pthread_exit(0);
}
补充材料:linux多线程编程Linux系统下的多线程遵循POSIX线程接口,称为pthread。编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a。要注意的是:Linux下pthread的实现是通过系统调用clone()来实现的。clone()是Linux所特有的系统调用,它的使用方式类似fork,关于clone()的详细情况,可以去查看有关文档说明或查看linux中的说明。下面我们展示一个最简单的多线程程序example1.c(上机作业)。
/* example.c*/#include stdio.h#include pthread.hvoid thread(void) {int ifor(i=0;i3;i++)printf(This is a pthread.\n); }
int main(void){pthread_t id;int i,ret;ret=pthread_create(id,NULL,(void *) thread,NULL);if(ret!=0){printf (Create pthread error!\n);exit (1); }for(i=0;i3;i++)printf(This is the main process.\n);pthread_join(id,NULL);return (0);}
编译此程序:gcc example1.c -lpthread -o example1运行example1,我们得到如下结果:This is the main process.This is a pthread.This is the main process.This is the main process.This is a pthread.This is a pthread.再次运行,我们可能得到如下结果:This is a pthread.This is the main process.This is a pthread.This is the main process.This is a pthread.This is the m
您可能关注的文档
最近下载
- 《电商生鲜配送发展中的问题及其对策—以盒马鲜生为例》16000字.docx VIP
- 2024高二地理期末复习选必1知识点清单 .pdf VIP
- 生鲜电商的发展问题及对策—以盒马鲜生为例.pdf VIP
- 生鲜电商的发展问题及对策——以盒马鲜生为例.docx VIP
- 田字格word模板(3)最好.doc VIP
- 2024学年九年级上学期第一次月考数学试题及参考答案 .pdf VIP
- 新零售背景下生鲜企业的发展研究——以盒马鲜生为例.docx VIP
- 连锁零售企业物流配送发展现状、问题及对策研究——以盒马鲜生为例.docx VIP
- 桥架多少钱一米?安装大概多少钱?桥架国标厚度是多少?.docx VIP
- 第八章海洋肽类教材.ppt VIP
文档评论(0)