操作系统实验一进程与线程_第1页
操作系统实验一进程与线程_第2页
操作系统实验一进程与线程_第3页
操作系统实验一进程与线程_第4页
操作系统实验一进程与线程_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

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

文档简介

1、实验一 进程与线程Linux 进程与线程通讯实验目的实验内容实验准备实验设计参考代码实验结果思考题实验目的深刻理解线程和进程的概念,掌握线程与进程在组成成分上的差别,以及与其相适应的通讯方式和应用目标。实验内容以Linux系统进程和线程机制为背景,掌握fork()和clone()系统调用的形式和功能,以及与其相适应的高级通讯方式。由fork派生的子进程之间通过pipe通讯,由clone创建的线程之间通过共享内存通讯,对于后者需要考虑互斥问题。以生产者/消费者问题为例,通过实验理解fork()和clone()两个系统调用的区别。程序要求能够创建4个进程或线程,其中包括两个生产者和两个消费者,生产

2、者和消费者之间能够传递数据。实验准备fork系统调用clone系统调用pipe系统调用sem_wait(&s)和sem_post(&s) pthread_mutex_lock(&mutex)和pthread_mutex_unlock(&mutex) fork系统调用pid=fork()创建一个子进程,子进程是父进程的完整复制,正常返回值为非负整数,对于父进程来说该数大于0,是子进程的编号(pid);对于子进程来说该数为0。正是利用反回值的差别可以决定二者不同的后继动作。clone系统调用int clone (int ( *fn ) (void *arg) , void *stack , int

3、 flag , void *arg) ;其中,fn是轻进程所执行的函数;stack是轻进程所使用的栈;flag是CLONE_VM, CLONE_FS, CLONE_FILES, CLONE_SIGNAND, CLONE_PID的组合;arg是调用过程的对应参数。Clone()的关键是flag的设定,CLONE_VM表示子进程共享父进程内存,CLONE_FS表示子进程共享父进程的文件系统,CLONE_SIGNAND表示子进程共享父进程的消息处理机制,CLONE_PID是指子进程继承父进程的id号。pipe系统调用ret_val=pipe(fd);参数定义为int fd2。创建一个管道文件,返回两

4、个文件描述符fd0和fd1分别用于管道文件的读和写操作。管道文件创建后,可以被fork创建的子进程共享。sem_wait(&s)和sem_post(&s)sem_wait(&s)和sem_post(&s)分别相当于信号灯的P操作和V操作。其中s是说明为说明为sem_t类型的信号灯。初始化函数sem_init(s,0,8)。pthread_mutex_lock(&mutex)和pthread_mutex_unlock(&mutex)pthread_mutex_lock(&mutex)和pthread_mutex_unlock(&mutex)分别用于加锁和解锁。参数为pthread_mutex_t

5、 mutex定义的互斥锁。初始化tthread_mutex_init(&mutex,NULL)。实验设计 用pipe()创建一个管道文件,然后用fork()创建两个生产进程和两个消费进程,它们之间通过pipe()传递信息。用clone()创建四个轻进程(线程),用参数指明共享内存等资源,通过共享内存模拟生产消费问题,利用pthread_mutex_lock(), pthread_mutex_unlock()等函数实现对共享存储区访问的互斥。参考代码基于fork()系统调用 基于clone()系统调用基于fork()系统调用 #include sys/types.h #include sys/f

6、ile.h #include unistd.h char r_buf4; /读缓冲 char w_buf4; /写缓冲 int pipe_fd2; pid_t pid1, pid2, pid3, pid4; int producer(int id); int consumer(int id);int main(int argc,char *argv) if(pipe(pipe_fd)0) printf(pipe create error n); exit(-1);elseprintf(pipe is created successfully!n);if(pid1=fork()=0) produ

7、cer(1);if(pid2=fork()=0) producer(2);if(pid3=fork()=0) consumer(1);if(pid4=fork()=0) consumer(2); close(pipe_fd0); /需要加上这两句close(pipe_fd1); /否这会有读者或者写者永远等待 int i,pid,status;for(i=0;i4;i+) pid=wait(&status); exit(0);int producer(int id) printf(producer %d is running!n,id); close(pipe_fd0); int i=0; f

8、or(i=1;i10;i+) sleep(3); if(id=1) /生产者1 strcpy(w_buf,aaa0); else /生产者2 strcpy(w_buf,bbb0); if(write(pipe_fd1,w_buf,4)=-1) printf(write to pipe errorn); close(pipe_fd1); printf(producer %d is over!n,id); exit(id);int consumer(int id) close(pipe_fd1); printf(producer %d is running!n,id); if (id=1) /消费

9、者1strcpy(w_buf,ccc0); else /消费者2strcpy(w_buf,ddd0); while(1) sleep(1); strcpy(r_buf,eee0); if(read(pipe_fd0,r_buf,4)=0) break; printf(consumer %d get %s, while the w_buf is %sn,id,r_buf,w_buf); close(pipe_fd0); printf(consumer %d is over!n, id); exit(id);#include sched.h#include pthread.h#include st

10、dio.h#include stdlib.h#include semaphore.hint producer(void * args);int consumer(void *args);pthread_mutex_t mutex;sem_t product;sem_t warehouse;char buffer84;int bp=0;基于clone()系统调用main(int argc,char* argv) pthread_mutex_init(&mutex,NULL); sem_init(&product,0,0); sem_init(&warehouse,0,8); int clone_

11、flag,arg,retval; char *stack; clone_flag=CLONE_VM|CLONE_SIGNAND|CLONE_FS| CLONE_FILES; int i; for(i=0;i2;i+) /创建四个线程 arg = i; stack =(char*)malloc(4096); retval=clone(void*)producer,&(stack4095),clone_flag, (void*)&arg); stack =(char*)malloc(4096); retval=clone(void*)consumer,&(stack4095),clone_flag

12、, (void*)&arg); exit(1);int producer(void* args) int id = *(int*)args); int i; for(i=0;i10;i+) sleep(i+1); /表现线程速度差别 sem_wait(&warehouse); pthread_mutex_lock(&mutex); if(id=0) strcpy(bufferbp,aaa0); else strcpy(bufferbp,bbb0); bp+; printf(producer%d produce %s in %dn,id,bufferbp,bp-1); pthread_mutex

13、_unlock(&mutex); sem_post(&product); printf(producer%d is over!n,id);int consumer(void *args) int id = *(int*)args); int i; for(i=0;i10;i+) sleep(10-i); /表现线程速度差别 sem_wait(&product); pthread_mutex_lock(&mutex); bp-; printf(consumer%d get %s in%dn,id,bufferbp,bp+1); strcpy(bufferbp,zzz0); pthread_mut

14、ex_unlock(&mutex); sem_post(&warehouse); printf(consumer%d is over!n,id);实验结果 程序(1)的输出 程序(2)的输出 结果分析程序(1)的输出 pipe is created successfully!producer 1 is running!producer 2 is running!producer 1 is running!producer 2 is running!consumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_

15、buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddconsumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddconsumer 2 get aaa, while the w_buf is

16、 dddconsumer 1 get bbb, while the w_buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddconsumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_buf is cccconsumer 1 get aaa, while the w_buf is cccconsumer 2 get bbb, while the w_buf is dddpr

17、oducer 1 is over!producer 2 is over!consumer 2 get aaa, while the w_buf is dddconsumer 1 get bbb, while the w_buf is cccconsumer 2 is over!consumer 1 is over!程序(2)的输出 producer0 produce aaa in 0producer1 produce bbb in 1producer0 produce aaa in 2producer1 produce bbb in 3producer0 produce aaa in 4pro

18、ducer1 produce bbb in 5consumer0 get bbb in 5consumer1 get aaa in 4producer0 produce aaa in 4producer1 produce bbb in 5producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 get aaa in 6producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 g

19、et aaa in 6producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 get aaa in 6producer0 produce aaa in 6producer1 produce bbb in 7consumer0 get bbb in 7consumer1 get aaa in 6consumer0 get bbb in 5consumer1 get aaa in 4producer0 produce aaa in 4producer1 produce bbb in 5c

20、onsumer0 get bbb in 5consumer1 get aaa in 4consumer0 get bbb in 3consumer1 get aaa in 2consumer0 get bbb in 1consumer1 get aaa in 0producer0 produce aaa in 0producer0 is over!producer1 produce bbb in 1producer1 is over!consumer0 get bbb in 1consumer0 is over!consumer1 get aaa in 0consumer1 is over!结果分析程序1结果分析程序2结果分析程序(1)由程序(1)结果

温馨提示

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

评论

0/150

提交评论