栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

操作系统原理(哈工大-李治军老师)实验三系统调用

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

操作系统原理(哈工大-李治军老师)实验三系统调用

实验三 系统调用 温馨提示

只会实现具体功能不涉及理论知识

1.添加iam和whoami系统调用编号的宏定义

文件路径:/oslab/linux-0.11/include/unistd.h 在131行代码之后添加一下内容:

#define __NR_whoami     72
#define __NR_iam        73
2.修改系统调用总数,

文件:kernel/system_call.s

nr_system_calls = 72 

改为
nr_system_calls = 74
3.为新增的系统调用添加系统调用名并维护系统调用表

文件:include/linux/sys.h

extern int sys_whoami();
extern int sys_iam();
注!
要在fn_ptr sys_call_table[]添加以上内容

4.为新增的系统调用编写代码实现

在linux-0.11/kernel目录下,创建一个文件 who.c 并添加以下代码

#include 
#include 
#include 

char _myname[24];

int sys_iam(const char *name)
{
    char str[25];
    int i = 0;

    do
    {
        // get char from user input
        str[i] = get_fs_byte(name + i);
    } while (i <= 25 && str[i++] != '');

    if (i > 24)
    {
        errno = EINVAL;
        i = -1;
    }
    else
    {
        // copy from user mode to kernel mode
        strcpy(_myname, str);
    }

    return i;
}

int sys_whoami(char *name, unsigned int size)
{
    int length = strlen(_myname);
    printk("%sn", _myname);

    if (size < length)
    {
        errno = EINVAL;
        length = -1;
    }
    else
    {
        int i = 0;
        for (i = 0; i < length; i++)
        {
            // copy from kernel mode to user mode
            put_fs_byte(_myname[i], name + i);
        }
    }
    return length;
}
4.修改 Makefile

要想让我们添加的 kernel/who.c 可以和其它 Linux 代码编译链接到一起,必须要修改 两处Makefile 文件。路径:kernel/Makefile

第一处
vim +27  Makefile 
OBJS  = sched.o system_call.o traps.o asm.o fork.o 
        panic.o printk.o vsprintf.o sys.o exit.o 
        signal.o mktime.o

修改为

OBJS  = sched.o system_call.o traps.o asm.o fork.o 
        panic.o printk.o vsprintf.o sys.o exit.o 
        signal.o mktime.o who.o

修改结果:
原基础添加了 who.o

第二处
### Dependencies:
exit.s exit.o: exit.c ../include/errno.h ../include/signal.h 
  ../include/sys/types.h ../include/sys/wait.h ../include/linux/sched.h 
  ../include/linux/head.h ../include/linux/fs.h ../include/linux/mm.h 
  ../include/linux/kernel.h ../include/linux/tty.h ../include/termios.h 
  ../include/asm/segment.h

修改为:

### Dependencies:
who.s who.o: who.c ../include/linux/kernel.h ../include/unistd.h
exit.s exit.o: exit.c ../include/errno.h ../include/signal.h 
  ../include/sys/types.h ../include/sys/wait.h ../include/linux/sched.h 
  ../include/linux/head.h ../include/linux/fs.h ../include/linux/mm.h 
  ../include/linux/kernel.h ../include/linux/tty.h ../include/termios.h 
  ../include/asm/segment.h

修改结果。添加…/include/linux/kernel.h …/include/unistd.h
Makefile 修改后和往常一样 make all 就能自动把 who.c 加入到内核中了。

shiyanlou@41a34909ea01:~/oslab/linux-0.11$ make all
到此,内核修改部分完成,接下来是编写测试程序了 5.编写测试程序 iam.c,whoami.c
#define __LIBRARY__
#include  
#include 
#include  
#include 
_syscall1(int, iam, const char*, name);
   
int main(int argc, char *argv[])
{
    
    iam(argv[1]);
    return 0;
}
#define __LIBRARY__
#include  
#include 
#include  
#include 
#include 
   
_syscall2(int, whoami,char *,name,unsigned int,size);
   
int main(int argc, char *argv[])
{
    char username[64] = {0};
    
    whoami(username, 24);
    printf("%sn", username);
    return 0;
}

然后把编写好的测试程序通过挂载到虚拟机操作系统

shiyanlou@41a34909ea01:~/oslab$ sudo ./mount-hdc 
shiyanlou@41a34909ea01:~/oslab$ cp iam.c whoami.c hdc/usr/root

执行脚本./run 进入虚拟机操作系统修改虚拟机系统调用号跟第一步是一样的

#define __NR_whoami	   	72
#define __NR_iam	   	73   

在虚拟机 gcc 编译执行测试程序

[/usr/root]# gcc -o iam iam.c
[/usr/root]# gcc -o whoami whoami.c
[/usr/root]# ./iam wcf
[/usr/root]# ./whoami

实验到此结束
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835808.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号