添加Linux系統調用

來源:互聯網
上載者:User

【實驗環境】
Fedora,核心2.6.21.6

【實驗步驟】
1. cd /usr/src/linux
2. vi kernel/sys.c

在最後添加如下代碼:
asmlinkage long sys_processinfo(void)<br />{<br /> int num=0;<br /> unsigned long state;<br /> struct task_struct *p;<br /> for_each_process(p)<br /> {<br /> printk("pid=%-5d,name=%-20s",p->tgid,p->comm);<br /> printk("prio=%-5d,",p->prio);<br /> state=p->state;<br /> if(state==-1)<br /> printk("state: Z/n");<br /> else if (state==1)<br /> printk("state: R/n");<br /> else printk("state: S/n");<br /> num++;<br /> }<br /> return num;<br />}

上述代碼實現遍曆整個工作清單,並將進程id,name 及state 列印在螢幕上,返回進程總
7
數。

3. vi arch/i386/kernel/syscall_table.S
在最後加入 .long sys_processinfo

4. vi include/asm/unistd.h
在中間添加 #define __NR_process 322 分配系統調用號
將下面改為 #define NR_syscalls 323

5. vi /usr/include/asm/unistd.h
在最後添加 #define __NR_process 322

6. 開始重新編譯核心
cd /usr/src/linux
make mrproper
make menuconfig
make
make modules_install
make install
reboot

7. 進入新核心,編寫測試程式test.c
#include <stdio.h><br />#include <unistd.h><br />#include <errno.h><br />#include <asm/unistd.h><br />#include <syscall.h><br />int main()<br />{<br /> syscall(322);<br /> printf("the total number is: %d",syscall(322));<br /> return 0;<br />}
8. gcc –o test test.c
9. ./test 顯示結果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.