操作系统实验报告理解Linux下进程和线程的创建并发执行过程_第1页
操作系统实验报告理解Linux下进程和线程的创建并发执行过程_第2页
操作系统实验报告理解Linux下进程和线程的创建并发执行过程_第3页
操作系统实验报告理解Linux下进程和线程的创建并发执行过程_第4页
操作系统实验报告理解Linux下进程和线程的创建并发执行过程_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.word可编辑.操作系统上机实验报告实验名称:进程和线程实验目的:理解unix/Linux下进程和线程的创建、并发执行过程。实验内容:1 .进程的创建2 .多线程应用实验步骤及分析:一、进程的创建下面这个C程序展示了 UNIX系统中父进程创建子进程及各自分开活动的情 况。fork()创建一个新进程。系统调用格式:pid=fork()参数定义:int fork()fork() 返回值意义如下:0:在子进程中,pid变量保存的fork() 返回值为0,表示当前进程是子>0:在父进程中,pid变量保存的fork()返回值为子进程的id值(进程唯一标识符)。-1 :创建失败。如果fork()调

2、用成功,它向父进程返回子进程的PID,并向子进程返回0,即fork()被调用了一次,但返回了两次。此时 OS在内存中建立一个新进程, 所建的新进程是调用 fork()父进程(parent process )的副本,称为子进程 (child process )。子进程继承了父进程的许多特性,并具有与父进程完全相 同的用户级上下文。父进程与子进程并发执行。2、参考程序代码/*process.c*/#include <stdio.h>#include <sys/types.h>main(int argc,char *argv)int pid;/* fork another p

3、rocess */pid = fork();if (pid < 0) /* error occurred */ fprintf(stderr, "Fork Failed");exit(-1);else if (pid = 0) /* child process */execlp( "/bin/ls", "ls",NULL);else /* parent process */* parent will wait for the child to complete */ wait(NULL);printf( "Child

4、Complete");exit(0);3、编译和运行$gcc process.c - o processs4、运行$./process编辑如图所小:文件®编辑®终撇D转到(£)帮助®fl inc lude<stdio,h>#include<sys/types *h>tih in( i n t a rgc , ;'lj j r *a rgy)1in i pid;p id = fork();i '( p id<0) fpr intf( &加r r ., Fork Fa i ledT ):ex i

5、Ielse i f ( p id=OexecIpC/bin/ls' /IsMXL);e sepr ini f( "Oii IdGonple e ');exi t(O)Q运行如图所示:vi progress gcc progress.c -o processs/process./prore s s ssub ing sheng$IxubIhos t xub ingsheng$xub ing5hcng'loca Ihos l xubingshengl$ xub ingsheng'ioca Ihos l xub ingsheng$ bash: 4/proce

6、ss:没有那个文6成日一 Ixub ingsheng$1o<i Ihos l xubingsheng$ proce sss progresstcCh i IdGoiTp k te(xub ingshengloca Ihos I思考:(1)系统是怎样创建进程的?1,申请空白PCB(进程控制块);2,为新进程分派资源;3,初始化PCB 4,将新进程插入就绪队列;(2)扩展程序,在父进程中输出1到5,在子进程中输出6-10,要求父子进程并发输出;记录实验结果,并给出简单分析实验结果如图:Edn View Terminal Go Helpxubingshnglocalhost xubingshe

7、ngvi process.cxubingshengClocaihost xubingsheng$ gcc process*c -o process xubingshenglocalhost xubingshengX t/process2 6 7 8 9 1012313 exatnplel exainple. c process proceSs2 process. c process. c 1 2 3 4 5xubingshenglocalhost xubingsheng $ |L.二、多线程应用编写unix/Linux 下的多线程程序,需要使用头文件pthread.h ,连接时需要使用库lib

8、pthread.a 。下面是一个最简单的多线程程序 examplel.c。下面的示例中,要使用到两个函数, pthread_create 和pthread_join ,并 声明了一个pthread_t型的变量。函数pthread_create用来创建一个线程,它的原型为:extern int pthread_create_P (pthread_t * thread,_constpthread_attr_t*_attr,void*(*_start_routine)(void *), void *_arg);专业.专注.word可编辑.第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,

9、第三 个参数是线程运行函数的起始地址,最后一个参数是运行函数的参数。这里,我 们的函数thread不需要参数,所以最后一个参数设为空指针。第二个参数我们 也设为空指针,这样将生成默认属性的线程。当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAINT口 EINVAL前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的 线程属性值非法。创建线程成功后,新创建的线程则运行参数三和参数四确定的 函数,原来的线程则继续运行下一行代码。函数pthread_join 用来等待一个线程的结束。函数原型为:extern int pthread_join

10、 _P (pthread_t_th,void* thread return);第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。 这个函数是一个线程阻塞的函数,调用它的 函数将一直等待到被等待的线程结束为止, 当函数返回时,被等待线程的资源被 收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了, 调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:extern void pthread_exit_P (void*_retval)_attribute_(noreturn_);唯一的参数是函

11、数的返回代码,只要pthread_join 中的第二个参数thread_return 不是 NUL这个值将被传递给 thread_return 。.专业.专注.word可编辑.2、参考程序代码/* thread.c*/#include <stdio.h>#include <pthread.h>void thread(void)int i;for(i=0;i<3;i+)printf("This is a pthread.n");int main(int argc,char *argv口)pthread_t id;int i,ret;ret=pth

12、read_create(&id,NULL,(void *) thread,NULL);if(ret!=0)printf ("Create pthread error!n");exit (1);for(i=0;i<3;i+)printf("This is the main process.n");pthread_join(id,NULL);return (0);3、编译和运行编译此程序:gcc example1.c -lpthread -o example1-lpthread:使用线程库运行example1 ,得到如下结果:This is t

13、he 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 main process.编辑过程如图所示:专业.专注执行如图所示:thread .c: 13: &

14、#39; id ' undf c la red (first use in ih" fund ion) sub ingsheng>'loca Ihos t xub ingshcngS v i thread .cxubingshenglocaIhoti xuhingsheng$ gee thread.c -Ipthr?ad -q thread thread ,c± In fund ion irain'thread . c 11L : p thread L id ' undec la red (first use in this func

15、t ion?thread,c;11: (Each undeclared idenii f ier i* repur led only once thread .c± 1L: for each fund ion i I appears in t) ihrcad.c:IS: id undec La red (first use in this func t ion)xubing!(heitgl0ca Iho*; 1 xub*ug*:heng$ vi thi ead"icub ingsheiigLoca Ihos I Kiib ingshf ng$ gee thread . c

16、- Ipt hread -o thread xub ingshengMoca Ihos i subingshcngS ./ threadThThThThThThis is 舟 pihread.i s i s a p thread L i s i s a p thread F is is I he im in process. i s i 与 the its in proce s s . is i s the ns in process.实验总结:在实验中很多粗心造成的问题,比如指令输错字母,代码写错字 母,没有注意是否需要空格等。通过课堂的理论知识学习和实验课的上机实验, 让我更能理解操作系统

17、的知识。4、思考(1)程序运行后,进程thread中有几个线程存在?3个(2)为什么前后两次运行结果不一样?单核的cpu在处理多线程时每次只能执行一跳指令,也就是说无论你的程序有多少个线程,每一时刻执行的也只是一个线程里的代码, cpu会轮流给每个线程 分配时间片,时间片分配到哪个线程头上,哪个线程里的代码就执行。但是多核 cpu就不一样了,他可以同时执行多个线程里的代码, 这才是真正的“多线程”。 所以你那段程序,在单核的电脑上跑应该是没有问题的, 但是在多核cpu的电脑 上出现的结果就会有很大的随机性。5、程序的扩展试在本程序中再添加一个或多个其他线程,观测运行结果,充分理解多线程的含义。多添加一个线程,将会多一行This is a pthread. 和This is the mainprocess.结果如图所示:xubingsheng'locu Llws t xubingsheng$ v i tliread .cxubinghengloca Ihos t xubi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论