操作系统实验三——进程的基本操作与进程管理

操作系统实验三——进程的基本操作与进程管理



前期准备-安装gcc

下载好Unbuntu 之后,它是不自带gcc的,需要自己安装

sudo apt install gcc 报错

在这里插入图片描述

解决方法:

  备份源列表: sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup
  打开:sudo gedit /etc/apt/sources.list
  添加云:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

更新: sudo apt-get update
重新运行命令:sudo apt-get install build-essential
成功:
在这里插入图片描述

1、 创建一个主进程,在主进程中创建一个子进程;在子进程中加载一个新的执行任务;父进程等待子进程退出,并获取子进程的退出状态码;要求父进程获取的退出状态码为 255

代码:mywaitexit.c

#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>

int main(){
	pid_t new_process_id;
	int status;
	new_process_id=fork();
	if(new_process_id==0){
	printf("This is in child process:%d\n",getpid());
	exit(255);
	printf("the rest of child\n");
	}
	else{
	printf("THis is in parent process:%d\n",getpid());
	wait(&status);
	printf("child exit status: %d\n",WEXITSTATUS(status));
	}
	return 0;
	}

运行与结果:
在这里插入图片描述

2 ./a.out arg1 arg2

代码:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	if (argc !=3)
	{
		printf("please input two integers as the arguments!\n");
		return -1;
	}
	printf("The sum is:%d\n",atoi(argv[1])+atoi(argv[2]));
	return 0;
}
		

运行和结果:
在这里插入图片描述

另一种方式:execl:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
	pid_t new_process_id;
	new_process_id=fork();
	if(new_process_id==0){
	printf("This is in child process:%d\n",getpid());
	execl("./mysum"," ","2","3",NULL);
	printf("This is the rest of the child process \n");
	}
	else{
		printf("This is in parent process:%d\n",getpid());
		}
		return 0;
}  

运行过程和结果:
在这里插入图片描述

3. 在系统上找到定义进程控制块结构体 task_struct 的头文件 sched.h,定位到该结构体源代码片段,仔细阅读该段代码,找出其中包含的关于一个进程的核心信息,如进程号、进程状态、进程执行的命令名,等。

coding:

find /usr/src -name "sched.h"

在这里插入图片描述

cat /usr/src/linux-hwe-5.13-headers-5.13.0-37/include/linux/sched.h

在这里插入图片描述

3. 创建一个进程,编写程序找到该进程,做出相应操作

进程: hello.c

#include <stdio.h>
int main(){
	printf("hello");
	while(1){
	}
	}

在这里插入图片描述
另打开一terminal
在这里插入图片描述
在这里插入图片描述
进程号PID 为23659

编写find_hello.c:
改进程号为23659

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched/signal.h>

/* This function is called when the module is loaded. */
int pl_entry(void)
{
	struct task_struct *curr;
	for_each_process(curr){
	if(curr->pid==23659){
		printk(KERN_INFO "I found the pcb of hello world!\n");
		break;
			}
	}
	
	return 0;
}

/* This function is called when the module is removed. */
void pl_exit(void) 
{
	printk(KERN_INFO "Exit hello world!\n");
}

/* Macros for registering module entry and exit points. */
module_init(pl_entry);
module_exit(pl_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("the first kernel module example");
MODULE_AUTHOR("zhaohj");

编写Makefile

# declare the name of the kernel module
# the same name as the *.c source file
# e.g.: compile the kernel module hello.ko from hello.o
# and hello.o will be compiled from hello.c

obj-m += find_hello.o 

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

在这里插入图片描述
make
sudo insmod.ko
dmesg

在这里插入图片描述
在这里插入图片描述

4. 编写、编译、加载一个内核模块,遍历所有进程的进程控制块,向内核缓冲区中打印每一个进程的进程号、进程状态、进程执行的命令名;将结果与 ps -el 命令的执行结果进行对比分析。

编写mymodel.c文件

gedit mymodel.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched/signal.h>
MODULE_LICENSE("GPL");
int pl_entry(void){ 
	int KernelThreadCount = 0; 
	int UserThreadCount = 0; 
	struct task_struct *p; 
	p = &init_task; 
	for_each_process(p) { 
	if( p->mm == NULL ) { 
	printk("\nKernelThread Info!\n"); 
	printk("comm=%s, pid=%d, state=%ld\n", p->comm, p->pid, p->state); 
	++KernelThreadCount; 
	} 
	else { 
	printk("\nUserThread Info!\n"); 
	printk("comm=%s, pid=%d, state=%ld\n", p->comm, p->pid, p->state); 
	++UserThreadCount; } 
	} 
	printk("\nThe number of kernel Thread is:%d.\n",KernelThreadCount); 		printk("\nThe number of User Thread is:%d.\n", UserThreadCount);  		return 0;
	}
void pl_exit(void){ 
	printk(KERN_INFO "Exit hello world!\n");
	}

module_init(pl_entry);
module_exit(pl_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("the first kernel module example");
MODULE_AUTHOR("zhaohj");

编写Makefile文件:
gedit Makefile

# declare the name of the kernel module
# the same name as the *.c source file
# e.g.: compile the kernel module hello.ko from hello.o
# and hello.o will be compiled from hello.c

obj-m += mymodel.o 

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

make
sudo insmod mymodel.ko
在这里插入图片描述
dmesg:
在这里插入图片描述
ps -el 对比:
在这里插入图片描述
宏for_each_process( )宏遍历所有的进程和线程,在接下来的if(p->mm== NULL)找出线程。并count++,进行计数。

p->mm==NULL的解释:
由于操作系统中用户进程与内核线程的区别在于是否分配用户内存空间。内核线程是不分配用户空间的。所以内核线程的mm ==NULL; 以此为依据判断是用户进程还是内核线程。

5 编译、编译、加载内核模块,遍历进程的双向链表,将某个特定进程的进程控制块从双向链表中移除;使用步骤 4 中的内核模块再次对系统中所有的进程进行遍历并打印到内核缓冲区,对比 4 和 5 的结果并分析原因。

删去特定进程块:

find.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched/signal.h>
/* This function is called when the module is loaded. */
int pl_entry(void)
{
	struct task_struct *curr;
	for_each_process(curr)
	{
		if(curr->pid ==4462)
		{	struct list_head * p;
			p=&(curr->tasks);
			list_del(p);
			break;
		}
	}
	return 0;
}


/* This function is called when the module is removed. */
void pl_exit(void) 
{
	printk(KERN_INFO "Exit hello world!\n");
}

/* Macros for registering module entry and exit points. */
module_init(pl_entry);
module_exit(pl_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("the first kernel module example");
MODULE_AUTHOR("zhaohj");


在这里插入图片描述
Makefile:

# declare the name of the kernel module
# the same name as the *.c source file
# e.g.: compile the kernel module hello.ko from hello.o
# and hello.o will be compiled from hello.c

obj-m += find.o 

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

然后再跑一遍实验三即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.基本系统进程   Csrss.exe:这是子系统服务器进程,负责控制Windows创建或删除线程以及16位的虚拟DOS环境。   System Idle Process:这个进程是作为单线程运行在每个处理器上,并在系统不处理其它线程的时候分派处理器的时间。   Smss.exe:这是一个会话管理子系统,负责启动用户会话。   Services.exe:系统服务的管理工具。   Lsass.exe:本地的安全授权服务。   Explorer.exe:资源管理器。   Spoolsv.exe:管理缓冲区中的打印和传真作业。   Svchost.exe:这个进程要着重说明一下,有不少朋友都有这种错觉:若是在“任务管理器”中看到多个Svchost.exe在运行,就觉得是有病毒了。其实并不一定,系统启动的时候,Svchost.exe将检查注册表中的位置来创建需要加载的服务列表,如果多个Svchost.exe同时运行,则表明当前有多组服务处于活动状态;多个DLL文件正在调用它。   至于其它一些附加进程,大多为系统服务,是可以酌情结束运行的。由于其数量众多,我们在此也不便于一一列举。   在系统资源紧张的情况下,我们可以选择结束一些附加进程,以增加资源,起到优化系统的作用。在排除基本系统及附加进程后,新增的陌生进程就值得被大家怀疑了。 更多内容请看Windows操作系统安装、系统优化大全、系统安全设置专题,或进入讨论组讨论。
实验进程管理   Windows所创建的每个进程都从调用CreateProcess() API函数开始,该函数的任务是在对象管理器子系统内初始化进程对象。每一进程都以调用ExitProcess() 或TerminateProcess() API函数终止。通常应用程序的框架负责调用 ExitProcess() 函数。对于C++ 运行库来说,这一调用发生在应用程序的main() 函数返回之后。 1. 创建进程 CreateProcess() 调用的核心参数是可执行文件运行时的文件名及其命令行。表 2-1详细地列出了每个参数的类型和名称。   表2-1 CreateProcess() 函数的参数 参数名称 使用目的 LPCTSTR lpApplivationName 全部或部分地指明包括可执行代码的EXE文件的文件名 LPCTSTR lpCommandLine 向可执行文件发送的参数 LPSECURIITY_ATTRIBUTES lpProcessAttributes 返回进程句柄的安全属性。主要指明这一句柄是否应该由其他子进程所继承 LPSECURIITY_ATTRIBUTES lpThreadAttributes 返回进程的主线程的句柄的安全属性 BOOL bInheritHandle 一种标志,告诉系统允许新进程继承创建者进程的句柄 DWORD dwCreationFlage 特殊的创建标志 (如CREATE_SUSPENDED) 的位标记 LPVOID lpEnvironment 向新进程发送的一套环境变量;如为null值则发送调用者环境 LPCTSTR lpCurrentDirectory 新进程的启动目录 STARTUPINFO lpStartupInfo STARTUPINFO结构,包括新进程的输入和输出配置的详情 LPPROCESS_INFORMATION lpProcessInformation 调用的结果块;发送新应用程序的进程和主线程的句柄和ID   可以指定第一个参数,即应用程序的名称,其中包括相对于当前进程的当前目录的全路径或者利用搜索方法找到的路径;lpCommandLine参数允许调用者向新应用程序发送数据;接下来的个参数与进程和它的主线程以及返回的指向该对象的句柄的安全性有关。 然后是标志参数,用以在dwCreationFlags参数中指明系统应该给予新进程什么行为。经常使用的标志是CREATE_SUSPNDED,告诉主线程立刻暂停。当准备好时,应该使用ResumeThread() API来启动进程。另一个常用的标志是CREATE_NEW_CONSOLE,告诉新进程启动自己的控制台窗口,而不是利用父窗口。这一参数还允许设置进程的优先级,用以向系统指明,相对于系统中所有其他的活动进程来说,给此进程多少CPU时间。 接着是CreateProcess() 函数调用所需要的个通常使用缺省值的参数。第一个参数是lpEnvironment参数,指明为新进程提供的环境;第二个参数是lpCurrentDirectory,可用于向主创进程发送与缺省目录不同的新进程使用的特殊的当前目录;第个参数是STARTUPINFO数据结构所必需的,用于在必要时指明新应用程序的主窗口的外观。 CreateProcess() 的最后一个参数是用于新进程对象及其主线程的句柄和ID的返回值缓冲区。以PROCESS_INFORMATION结构中返回的句柄调用CloseHandle() API函数是重要的,因为如果不将这些句柄关闭的话,有可能危及主创进程终止之前的任何未释放的资源。 2. 正在运行的进程 如果一个进程拥有至少一个执行线程,则为正在系统中运行的进程。通常,这种进程使用主线程来指示它的存在。当主线程结束时,调用ExitProcess() API函数,通知系统终止它所拥有的所有正在运行、准备运行或正在挂起的其他线程。当进程正在运行时,可以查看它的许多特性,其中少数特性也允许加以修改。 首先可查看的进程特性是系统进程标识符 (PID) ,可利用GetCurrentProcessId() API函数来查看,与GetCurrentProcess() 相似,对该函数的调用不能失败,但返回的PID在整个系统中都可使用。其他的可显示当前进程信息的API函数还有GetStartupInfo()和GetProcessShutdownParameters() ,可给出进程存活期内的配置详情。 通常,一个进程需要它的运行期环境的信息。例如API函数GetModuleFileName() 和GetCommandLine() ,可以给出用在CreateProcess() 中的参数以启动应用程序。在创建应用程序时可使用的另一个
(1)进程的软中断通信 #include #include #include #include int wait_flag; void stop(); main( ) { int pid1, pid2; // 定义两个进程号变量 signal(2,stop); // 或者 signal (14,stop); while((pid1 = fork( )) == -1); // 若创建子进程1不成功,则空循环 if(pid1 > 0) { // 子进程创建成功,pid1为进程号 while((pid2 = fork( )) == -1); // 创建子进程2 if(pid2 > 0) { wait_flag = 1; //sleep(1); // 父进程等待5秒 kill(pid1,SIGUSR1); // 杀死进程1 kill(pid2,SIGUSR2); // 杀死进程2 wait(0); wait(0); printf("\n Parent process is killed !!\n"); exit(0); // 父进程结束 } else { wait_flag = 1; signal(SIGUSR2,stop); // 等待进程2被杀死的中断号17 printf("\n Child process 2 is killed by parent !!\n"); exit(0); } } else { wait_flag = 1; signal(SIGUSR1,stop); // 等待进程1被杀死的中断号16 printf("\n Child process 1 is killed by parent !!\n"); exit(0); } } void stop() { wait_flag = 0; } (2)进程的管道通信 #include #include #include int pid1,pid2; // 定义两个进程变量 main( ) { int fd[2]; char OutPipe[100],InPipe[100]; // 定义两个字符数组 pipe(fd); // 创建管道 while((pid1 = fork( )) == -1); // 如果进程1创建不成功,则空循环 if(pid1 == 0) { lockf(fd[1],1,0); // 锁定管道 sprintf(OutPipe,"\n Child process 1 is sending message!\n"); write(fd[1],OutPipe,50); // 向管道写入数据 sleep(5); // 等待读进程读出数据 lockf(fd[1],0,0); // 解除管道的锁定 exit(0); // 结束进程1 } else { while((pid2 = fork()) == -1); // 若进程2创建不成功,则空循环 if(pid2 == 0) { lockf(fd[1],1,0); sprintf(OutPipe,"\n Child process 2 is sending message!\n"); write(fd[1],OutPipe,50); sleep(5); lockf(fd[1],0,0); exit(0); } else { wait(0); // 等待子进程1 结束 read(fd[0],InPipe,50); // 从管道中读出数据 printf("%s\n",InPipe); // 显示读出的数据 wait(0); // 等待子进程2 结束 read(fd[0],InPipe,50); printf("%s\n",InPipe); exit(0); // 父进程结束 } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值