华南理工大学2013级计科一班操作系统实验

实验一:进程和线程的创建

1. 在linux下编写一个应用程序,命名为an_ch2_1b。这个程序不断地输出如下行:

Those output come from child,[系统时间]

另外写一个应用程序,命名为an_ch2_1a。这个程序创建一个子进程,执行an_ch2_1b。这个程序不断地输出如下行:

Those output come from child,[系统时间]

观察程序运行的结果,并对你看到的现象进行解释。

这个是an_ch2_1a.cpp

#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
#include <stdlib.h>
using namespace std;
int main()
{
	time_t now;
	tm * timenow;
	pid_t pid;
	pid = fork();
	if(pid==-1){
		cout<<"fail to create"<<endl;
	}
	else if(pid==0){
		system("./an_ch2_1b");
	}
}

写完之后保存,命令行输入:g++ -o aaa an_ch2_1a.cpp
进行编译。生成可执行文件aaa。
这个是an_ch2_1b.cpp
#include <iostream>
#include <cstdio>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
using namespace std;
int main()
{   
	time_t now;
	tm * timenow;
	while(true){
		time(&now);
		timenow = localtime(&now);
		cout<<"Those output come from child,"<<asctime(timenow)<<endl;
	}
} 
写完之后保存,命令行输入:g++ -o an_ch2_1b an_ch2_1b.cpp
进行编译。生成可执行文件an_ch2_1b

<pre name="code" class="cpp"><pre name="code" class="cpp">在命令行输入:./an_ch2_1b
执行an_ch2_1b

 在命令行输入:./aaa 执行aaa 
 

kill掉当前进程是ctrl+c

 

2.

 

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
int share_var=0;
void * son(void * arg)
{
	while(1){
	   share_var--;
	   printf("in the son share_var:%d\n",share_var);	
	   usleep(1000);
	}
}
int main()
{
    pthread_t pt;
	int ret;
	ret=pthread_create(&pt,NULL,(void*)son,NULL);
	if(ret!=0){
		printf("create fail\n");
	}
    while(1){
		share_var++;
		printf("in the main shaer-var:%d\n",share_var);
        usleep(1000);
	}
	pthread_join(pt,NULL);
}



 

  • 16
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
session1 【Objective and Requirement】 Objective: Be familiar with the creation of process and thread on Windows. Requirement: Sub Task 1: Create a console application, "child", which keeps printing out "The child is talking at [system time]" (in a loop, one per 1s). Sub Task 2: Create another console application, "parent". It create a child process to execute “child”. The "parent" process keeps printing out "The parent is talking at [system time]". (one per 1s) Execute "parent" and explain the output you see. Sub Task 3: Create a "winPS" program which can output information about all the running processes. Try to output details about each process, such as PID, executable file name and path, etc. Sub Task 4: In the "mainThread" program, use "CreateThread" to create a child thread. Both the main thread and the child thread keep printing out "[ThreadID] + [System time]". session2 Objective: Create "ps" and "kill" commands on Windows. Requirement: Sub Task 1: On Linux/Unix there are a "ps" command and a "kill" command. The "ps" command can list information about all the running processes. The "kill" command can terminate processes. Based on the tasks finished in Session 1, create "ps" and "kill" commands on Windows. Tips: using "TerminateProcess" to "kill" a process. session3 Objective: Learn how to use semaphore to solve IPC problems. Requirement: Task 3.1. Sleeping barber Use semaphores to solve the problem of sleeping barber. Task 3.2. Reader & Writer Use semaphores to solve the reader and writer problem, with the readers (and writers) have higher priority. session4 Title: Upgrade Linux/Unix commands Problem: Write a program "supershell" that takes another command as an argument and executes that command. For instance, executing: “./supershell cat /usr/greg/readme" would invoke the cat command on the file /usr/greg/readme. After execution of the specified command has completed, "supershell" should display statistics that show some of the system resources the co
### 回答1: 华南理工大学操作系统》去年试卷整合版.pdf 是华南理工大学的一份操作系统课程的试卷整合版。这份试卷包括了去年上学期每个章节的考试题目,涵盖了操作系统的基本概念、原理和应用等方面的知识。 通过研究这份试卷,可以对当年的操作系统课程的内容和考察重点有一个清晰的了解。对于正在学习这门课程的学生来说,这个文件可以作为复习的参考资料,帮助他们更好地掌握课程所涉及的知识点,为考试做好准备。 对于老师来说,这份试卷可以作为一份很宝贵的教学资源。通过分析试卷中的题目类型和难度分布,老师可以对自己的教学方法和知识重点进调整,以提高教学质量。 此外,这份试卷整合版还可以作为操作系统知识的参考资料。无论是在学术研究方向还是在际应用领域,操作系统都是一个重要的基础知识。通过阅读并领会试卷中的问题和解答,读者可以加深对操作系统的理解,了解际应用中的问题和解决方法。 总的来说,华南理工大学操作系统》去年试卷整合版.pdf 是一份非常有价值的学习资料,对于学生和教师来说都具有很重要的意义。 ### 回答2: 华南理工大学操作系统》去年试卷整合版.pdf是华南理工大学某年操作系统课程的试卷集合。这份试卷可能由教师们根据该年的教学内容整理而成,目的是为了帮助学生们复习和备考。 试卷整合版.pdf中,可能包含了一整年或一个学期的考试题目,包括选择题、填空题、简答题和计算题等。这些题目可能覆盖了操作系统的各个重要内容,如进程管理、内存管理、文件系统、输入输出等。整合版试卷的出现是为了方便学生们统一下载、打印和使用,便于学习和备考。 对于学生们来说,整合版试卷是一份宝贵的学习资源。通过复习这些试题,学生们可以了解老师对该课程的考查重点和考察方式,进一步巩固对操作系统知识的掌握。同时,试卷中的题目也可以作为学习的参考,帮助学生们提高解题能力和应试技巧。 总之,华南理工大学操作系统》去年试卷整合版.pdf是华南理工大学某年操作系统课程的试卷集合。通过复习这份试卷,学生们可以加深对操作系统知识的理解和掌握,提高学习效果和考试成绩。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值