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

本文详细介绍了如何在Ubuntu系统中安装gcc,创建进程、子进程与退出状态管理,解析task_struct结构,以及通过内核模块操作进程控制块。涵盖了进程基本操作、进程管理及内核模块开发实践,提供了与ps命令的对比分析。

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

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



前期准备-安装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

然后再跑一遍实验三即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值