logo资料库

进程和线程的创建 _实验报告.doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
华南理工大学 《操作系统与应用》课程实验报告 实验题目: 进程和线程的创建 姓名: 学号: 班级: 组别: 指导教师: 贺小箭 实验概述 【实验目的及要求】 实验目的: 1.在 linux 下编写一个应用程序,命名为 an_ch2_1b。这个程序不断地输出如下行: Those output come from child,[系统时间] 另外写一个应用程序,命名为 an_ch2_1a。这个程序创建一个子进程,执行 an_ch2_1b。 这个程序不断地输出如下行: Those output come from child,[系统时间] 观察程序运行的结果,并对你看到的现象进行解释。 2.在 linux 环境下编写一个控制台应用程序,程序中有一个共享的整型变量 shared_var, 初始值为 0;创建一个线程并使其立即与主线程并发执行。新创建的线程与主线程均不 断地循环,并输出 shared_var 的值。主线程在循环中不断地对 shared_var 进行加 1 操 作,即每次循环 shared_var 被加 1;而新创建的线程则不断地对 shared_var 进行减 1 操 作,即每次循环 shared_var 被减 1。观察程序运行的结果,并对你看到的现象进行解释。 实验要求: 【实验环境】 操作系统:VMware workstation Pro——Ubuntu 16.04 实验内容 【实验过程】
一、实验步骤: 1. 编写 an_ch2_1a.cpp 编译 an_ch2_1a.cpp 为 aaa 编写 an_ch2_1b.cpp 编译 an_ch2_1b.cpp 为 an_ch2_1b 执行 aaa 执行 an_ch2_1b 2. 编写 shared_var.cpp,编译并执行 二、实验数据: 1. //an_ch2_1a.cpp #include #include #include #include #include using namespace std; int main() { time_t now; tm * timenow; pid_t pid; pid = fork(); if(pid==-1){ } else if(pid==0){ } } //an_ch2.1b.cpp #include #include #include #include #include using namespace std; int main() //创建子进程,返回进程号给 pid cout<<"fail to create"<
{ time_t now; tm * timenow; while(true){ time(&now); timenow = localtime(&now); cout<<"Those output come from child,"< #include #include #include #include #include using namespace std; int share_var=0; void * son(void * arg) { while(1){ } } int main() { shared_var--; cout<<"in the son shared_var:"<
三、实验主要过程: 1. an_ch2_1a.cpp 的编辑: an_ch2_1a 编译 an_ch2_1b 创 建 : an_ch2_1b 编译 执行:
2.创建 shared_var.cpp
编译 小结 通过本次试验,学会了如何在 Ubuntu 中编程、编译、运行,以及了解了进程和线 程创建的相关知识,如创建子进程,等待子进程执行完毕等,程序员需要在编程时就对 应当牢牢把握进程与线程的运行情况,以能够对其进行修改和优化。 指导教师评语及成绩 评语: 成绩: 批阅日期: 指导教师签名:
分享到:
收藏