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

本文介绍两个Linux下的实验案例,一个是通过创建子进程来输出指定信息,另一个是通过创建线程并实现变量共享,以此展示进程与线程的基础概念及它们之间的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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);
}



 

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
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值