华科操作系统实验报告.docxVIP

  • 7
  • 0
  • 约6.82千字
  • 约 16页
  • 2019-09-27 发布于广东
  • 举报
课程实验报吿 课程名称: 操作系统原理 专业班级: 学 号: 姓 名: 指导教师: 报告日期: 计算机科学与技术学院 目录 TOC \o 1-5 \h \z \o Current Document 目录 2 \o Current Document 实验一:线程的同步 3 \o Current Document 一、 实验目的 3 \o Current Document 二、 实验内容 3 \o Current Document 三、 实验心得 6 \o Current Document 实验二:誉抄实验的进程实现 7 \o Current Document 一、 实验目的 7 \o Current Document 二、 实验内容 7 \o Current Document 三、 实验心得 10 \o Current Document 实验三:Linux文件目录操作 12 \o Current Document 一、 实验目的 12 \o Current Document 二、 实验内容 12 \o Current Document 三、 实验心得 14 实验一:线程的同步 一、实验目的 1、 掌握Linux系统用户界面中键盘命令的使用。 2、 学会一种Linux下的编程环境。 3、 掌握Linux下进(线)程的概念。 4、 了解Linux进程同步与通信的主要机制,并通过信号灯操作实现进程间的同 步与互斥。 二、实验内容 1、 程序要求 两个线程,共享公共变量a 线程1负责计算(+1) 线程2负责打印 2、 运行环境 软件配置(含操作系统版本人ubuntu - 14.10 硬件:PC 3、 源程序 源程序: #include stdio.h #include stdlib.h #include pthread.h #include sys/types.h #include sys/sem.h #include sys/ipc.h int semid; int a=0; typedef union senum { int val; struct semid_ds *buf; unsigned short * array; struct seminfo *_buf; }SEM_CTL_UN; void P(int semidjnt index) stmct sembuf sem; sem.sem_num = index; sem.sem_op = -1 ;//P sem.sem_flg = 0;//biaoji semop(semid,sem, 1); } void V(int semidjnt index) { struct sembuf sem; sem.sem_num = index; sem.sem_op = 1; sem.sem_flg = 0; semop(semid,sem, 1); void* thread 1 (void *arg) { int i=0; for(i;ii++) { P(semid,0); printf(add:\nn); a=a+1; printf(a=%d\n,a); V(semid,l); void* thread2(void *arg) { int i=0; for(i;iv8;i++) { P(semid,l); printf(print:\nn); printf(a=%d\n,a); V(semid,0); } } int main() pthread_t idl,id2; int retl,ret2; key_t key; SEM_CTL_UN semctlargl; SEM_CTL_UN senictlarg2; key= 1; semid=semget(key,2,IPC_CREATI0666);// 创建 semctlargl. val=l; semctlarg2.val=0; semctl(semid,O,SETVAL,semctlarg 1);//初始化 semctl(semid,l ,SETVAL,semctlarg2); ret l=pthread_create(idl,NULL,threadl,NULL); ret2=pthread_create(id2,NULL,thread2,NULL); pthread _join(id 1 ,NULL); pthreadJoin(id2,NULL); } 4、实验结果 实验结果截图如口 isac@lsac-X450VC:~$ gcc test,c -o test ?lpthread isac@isac-X450VC:~$ ?/test add: a=l prin

文档评论(0)

1亿VIP精品文档

相关文档