Linux内核编译

Linux内核编译

2023年8月2日发(作者:)

操作系统课程设计

实验报告册

班级: 131112

学号: 13111xxx

姓名: xxxxxx

教师:

第 1 页 共 18 页 目 录

实验说明

重要提示

实验2

makefile的编写及Linux内核编译 (8学时)

要求:

掌握Linux中makefile文件的编写

理解Linux内核的Makefile

掌握至少一个版本Linux内核的编译步骤

了解Linux内核的配置过程

实验编号

实验

目的

2 题目

makefile的编写及Linux内核编译

掌握Linux中makefile文件的编写

理解Linux内核的Makefile

掌握至少一个版本Linux内核的编译步骤

了解Linux内核的配置过程

 准备工作

– 相关软件的获取与安装(源代码,软件包)

 编译工作

– 配置,编译

 修改启动配置文件

– 修改grub2启动项

实验内容

 能够正确的编译源代码

 能够用编译出的内核启动系统

报告内容要求

(1) 实现方法和思路

(2) 测试及结果

报 告 正 文

第 2 页 共 18 页 内核编译过程

内核原版本号:

2.6.32-28-generic

拷贝源代码到/usr/src目录下

在usr/src目录下解压源码:

第 3 页 共 18 页 拷贝packages到“~”目录下:

安装软件包:

dpkg -i gcc-4.4_4.4.3-4ubuntu5.1_;

dpkg -i libgomp1_4.4.3-4ubuntu5.1_;

dpkg -i cpp-4.4_4.4.3-4ubuntu5.1_;

dpkg -i xz-utils_4.999.9beta+20091116-1_;

dpkg -i patch_2.6-2ubuntu1_;

dpkg -i dpkg-dev_1.15.5.6ubuntu4.6_;

dpkg -i fakeroot_1.14.4-1ubuntu1_;

dpkg -i gcc-4.4-base_4.4.3-4ubuntu5.1_;

dpkg -i libstdc++6_4.4.3-4ubuntu5.1_;

dpkg -i libgcc1_4.4.3-4ubuntu5.1_;

dpkg -i --force- g++-4.4_4.4.3-4ubuntu5.1_ ;

dpkg -i libstdc++6-4.4-dev_4.4.3-4ubuntu5.1_;

dpkg -i g++_4.4.3-1ubuntu1_;

dpkg -i build-essential_11.4build1_;

dpkg -i cvs_1.12.13-12ubuntu1.10.04.1_;

dpkg -i gettext_0.17-8ubuntu3_;

dpkg -i intltool-debian_0.35.0+20060710.1_;

dpkg -i po-debconf_1.0.16_;

dpkg -i kernel-package_12.032_;

dpkg -i libsys-hostname-long-perl_1.4-2_;

dpkg -i libmail-sendmail-perl_0.79.16-1_;

dpkg -i libncurses5-dev_5.7+20090803-2ubuntu3_

第 4 页 共 18 页 转到内核源代码所在的目录 “/usr/src/linux-2.6.32.60”

输入Make menuconfig,进入general setup选项,进入local version菜单,添加版本标示:rain13111153,保存并退出。

输入make语句,等待2小时„

make modules_install

make install

update-initramfs –c –k 2.6.32.60rain13111153

第 5 页 共 18 页 修改grub启动项:

cd/boot/grub

Gedit

重新启动ubuntu

查看内核版本号:

2.6.32.60rain13111153

第 6 页 共 18 页 实验编号 3 题目

Linux的进程和线程

实验掌握创建和终止进程/线程的方法

目的

理解进程/线程的概念

掌握与进程/线程控制相关的系统函数

实验创建和终止进程/线程

内容

使用进程/线程控制相关的系统函数

报告(1) 实现方法和思路

内容(2) 测试及结果

要求

报 告 正 文

getpid():获得当前进程ID

getppid():获得当前进程的父进程的ID

getuid():获得用户ID

getgid():获得组ID

源代码:

#include

#include

#include

int main(){

pid_t myPid;

pid_t myParentPid;

gid_t myGid;

uid_t myUid;

myPid = getpid();

myParentPid = getppid();

myGid = getgid();

myUid = getuid();

printf("my process id is %dn", myPid);

printf("my parent is process id is %dn", myParentPid);

printf("my group id is %dn", myGid);

printf("my user id is %dn", myUid);

return 0;

}

第 7 页 共 18 页 运行结果;

API函数

fork

wait

signal

pause

kill

exit

Wait函数

pid = wait( &status );

If( WIFEXITED(status) ){

printf(“Child exited normally with status %dn”,

WEXITSTATUS(status));

}else if( WIFSIGNALED(status) ){

printf(“Child

}

第 8 页 共 18 页

用途

创建一个新的子进程

将进程挂起直到子进程退出

注册一个新的信号句柄

将进程挂起直到捕获到信号

向某个指定的进程发出信号

正常中止当前进程

exited by signal with

status %dn”,WTERMSIG(status)); 源代码:

#include

#include

#include

int main(){

}

pid_t ret;

int status , i;

int role = -1;

ret = fork();

if(ret > 0){

printf("Parent: This the parent process (pid %d)n", getpid());

for(i=0;i<6;i++){

printf("Parent: At count %dn", i);

sleep(3); }

ret = wait(&status);//防止僵尸进程的产生

role=0; }

if(ret ==0){

}

else{

}

printf("%s: ", ((role ==0)?"Parent":"Child"));

return 0;

printf("Parent: Error trying to fork() (%d)n", errno);

printf("Child: This the child process (pid %d)n",

for(i=0;i<6;i++){

}

role = 1;

printf("Chile: At count %dn",i);

sleep(1);

else

getpid());

第 9 页 共 18 页 运行结果:

signal函数

信号

SIGHUP

SIGINT

SIGKILL

SIGUSR1

SIGUSR2

SIGPIPE

SIGTERM

源代码:

#include

#include

#include

#include

void catch_ctlc( int sig_num){

printf("Caught Control-Cn");

第 10 页 共 18 页

说明

挂起

键盘中断

Kill信号

用户自定义信号

用户自定义信号

终止管道

终止信号 }

fflush(stdout);//清除标准输出的缓存区

int main(){

}

运行结果:

signal( SIGINT, catch_ctlc);

printf("Go ahead, make my day.n");

pause();

return 0;

• pause函数

– pause函数会把进程挂起,直到接收到信号。在接收到以后,调用进程从pause中返回,继续进行。如果进程捕获的信号已经注册了信号句柄,那么pause函数会在信号句柄被调用并返回之后才返回。

– pause原型:

• 头文件

• int pause( void );

pid

>0

0

-1

<0

第 11 页 共 18 页

说明

信号发送到由pid指定的进程

信号发送到与调用进程同组的所有进程

信号发送到所有进程(init进程除外)

信号发送到由pid的绝对值指定的进程组中的所有进程 源代码:

#include

#include

#include

#include

#include

#include

void usr1_handler( int sig_num){

}

int main(){

else{

if(ret == 0){

printf("Child: This is the child process (pid %d)n",

role = 1;

sleep(1);

printf("Child: Sending SIGUSR1 to pid %dn", getppid());

kill(getppid(), SIGUSR1);

sleep(2);

pid_t ret;

int status;

int role = -1;

ret = fork();

if( ret > 0){

printf("Parent: This is the parent process

printf("Parent (%d) got the SIGUSR1n", getpid() );

(pid %d)n",getpid() );

signal( SIGUSR1, usr1_handler);

role = 0;

pause();

printf("Parent: Awaiting child exitn");

ret = wait( &status);

}

getpid());

第 12 页 共 18 页

}

}

}else{

}

printf("%s: Exiting„n", ( ( role == 0) ? "Parent" :

printf("Parent: Error trying to fork() (%d)n", errno);

"Child"));

return 0;

运行结果:

• exit函数

– 终止调用进程。传入exit的参数会返回给父进程,为wait或waitpid调用提供所需要的状态信息。

– exit原型:

• void exit( int status);

– 进程调用exit时还会向父进程发出SIGCHLD信号,释放当前进程占用的资源。

– 这个函数调用十分重要,因为他会向Shell坏境表明状态时成功还是失败。

第 13 页 共 18 页 • 线程函数

– 头文件

– 创建线程原型

• int pthread_create(pthread_t

*attr,

*thread,

void pthread_attr_t

*(*start_routine)(void*), void *arg);

• 第一个参数为指向线程标示符的指针。

• 第二个参数用来设置线程属性。

• 第三个参数是线程运行函数的起始地址。

• 最后一个参数是运行函数的参数。

• 若成功则返回0,若失败则返回出错编号。

• 线程函数

– 终止线程原型

– int pthread_exit(void *retval);

源代码:

#include

#include

#include

#include

#include

void *myThread(void *arg){

}

int main()

{

int ret;

pthread_t mythread;

ret = pthread_create(&mythread,NULL,myThread,NULL);

if(ret!=0){

printf("Can not create pthread (%s)n",strerror(errno));

exit(-1);

printf("Thread rann");

pthread_exit(arg);

第 14 页 共 18 页

}

}

return 0;

程序无输出内容

修改代码:

#include

#include

#include

#include

#include

void *myThread(void *arg){

}

int main()

{

}

第 15 页 共 18 页

printf("Thread rann");

pthread_exit(arg);

int ret;

pthread_t mythread;

ret = pthread_create(&mythread,NULL,myThread,NULL);

if(ret!=0){

}

if(ret!=1){

}

return 0;

printf("11111111n");

exit(-1);

printf("Can not create pthread (%s)n",strerror(errno));

exit(-1); 运行结果:

• 线程管理

– pthread_t pthread_self(void);//获得自己的线程描述符

void *myThread(void *arg)

{

• int pthread_join(pthread_t th, void **thread_return );

• 参数th是想要加入的线程

• 参数thread_return存储线程完成后的返回值,可以为NULL,表明不捕获线程的返回状态。

• 返回值,0代表成功,非0代表失败的编号。

源代码;

#include

#include

void *myThread(void *arg){

}

#define MAX_THREADS 5

int main(){

int ret, i, status;

pthread_t threadIds[MAX_THREADS];

for(i = 0; i

第 16 页 共 18 页

pthread_t pt;

pt = pthread_self();

printf("Thread %x ran!n", (int)pt);

pthread_exit(NULL);

}

printf("Thread %d startedn", (int)arg);

pthread_exit(arg);

}

}

ret = pthread_create(&threadIds[i], NULL, myThread, (void*)i);

if(ret != 0){

}

printf("Error creating thread %dn", (void*)i);

for(i = 0; i

}

return 0;

ret = pthread_join(threadIds[i], (void **)&status);

if(ret != 0){

}

else{

}

printf("Status = %dn", status);

printf("Error joining thread %dn", (void *)i);

运行结果:

第 17 页 共 18 页 实验编号

实验目的

实验

内容

4 题目

xxxxxxxxxxxxxxxxx

报告(1) 实现方法和思路

内容(2) 测试及结果

要求

报 告 正 文

第 18 页 共 18 页

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690957134a472697.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信