




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)1.1、1.2 Linux Ubuntu的安裝、創(chuàng)建新的虛擬機(jī)VMWare實(shí)驗(yàn)1.3 Shell編程1.實(shí)驗(yàn)?zāi)康呐c內(nèi)容通過本實(shí)驗(yàn),了解Linux系統(tǒng)的shell機(jī)制,掌握簡(jiǎn)單的shell編程技巧。編制簡(jiǎn)單的Shell程序,該程序在用戶登錄時(shí)自動(dòng)執(zhí)行,顯示某些提示信息,如“Welcome to Linux”, 并在命令提示符中包含當(dāng)前時(shí)間、當(dāng)前目錄和當(dāng)前用戶名等基本信息。2.程序源代碼清單#include<stdio.h>#include<sys/wait.h> int main()printf("Hello Linuxn");int pid;in
2、t state;int pfd2;pipe(pfd);if (fork()=0)printf("In the grep progressn");dup2(pfd0,0);close(pfd0);close(pfd1);execlp("grep","grep","sh",0);perror("exelp grep error");esle if(fork()=0) printf("In the ps progressn");dup2(pfd1,1);close(pfd0);cl
3、ose(pfd1);execlp("ps","ps","-ef",0);perror("execlp ps -ef");close(pfd1);close(pfd0);wait(&state);wait(&state);實(shí)驗(yàn)2.3 內(nèi)核模塊實(shí)驗(yàn)步驟:(1).編寫內(nèi)核模塊 文件中主要包含init_clock(),exit_clock(),read_clock()三個(gè)函數(shù)。其 中init_clock(),exit_clock()負(fù)責(zé)將模塊從系統(tǒng)中加載或卸載,以及增加或刪除模塊在/proc中的入口。rea
4、d_clock()負(fù)責(zé)產(chǎn)生/proc/clock被讀時(shí)的動(dòng)作。(2).編譯內(nèi)核模塊Makefile文件# Make 2.6.25 ifneq ($(KERNELRELEASE),) #kbuild syntax. dependency relationshsip of files and target modules are listed here. obj-m := proc_clock.o else PWD := $(shell pwd) KVER ?= $(shell uname -r) KDIR := /lib/modules/$(KVER)/build all: $(MAKE) -C
5、 $(KDIR) M=$(PWD) modules clean: rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions *.symvers *.order endif 編譯完成之后生成proc_clock.ko模塊文件。(3).內(nèi)核模塊源代碼clock.c#include <linux/kernel.h> #include <linux/module.h> #include <linux/proc_fs.h> #include <linux/string.h> #include <linux/vmallo
6、c.h> #include <asm/uaccess.h> #define MODULE #define MODULE_VERSION "1.0" #define MODULE_NAME "clock" struct proc_dir_entry* my_clock; int read_clock(char* page, char* start, off_t off, int count, int* eof, void* data) int len; struct timeval xtime; do_gettimeofday(&
7、xtime); len = sprintf(page, "%d %dn", xtime.tv_sec, xtime.tv_usec); printk("clock: read_func()n"); return len; struct proc_dir_entry *clock_proc_file; int init_clock(void) clock_proc_("clock",0,NULL,read_clock,NULL); return 0; void exit_clock(void) remove_proc_entry(&qu
8、ot;clock",clock_proc_file); module_init(init_clock) module_exit(exit_clock) MODULE_LICENSE("GPL");(4).編譯內(nèi)核模塊# make (5) .加載內(nèi)核模塊 在系統(tǒng)root用戶下運(yùn)行用戶態(tài)模塊命令裝載內(nèi)核模塊 # insmod proc_clock.ko(6).測(cè)試在終端中輸入以下命令: cat /proc/clock (7).卸載內(nèi)核模塊在系統(tǒng)root用戶下運(yùn)行用戶態(tài)模塊命令卸載內(nèi)核模塊#rmmod proc_clock.ko 實(shí)驗(yàn)2.4 系統(tǒng)調(diào)用實(shí)驗(yàn)步驟:(1).
9、添加新調(diào)用的源代碼 在./linux-2.6.33.7/arch/x86/kernel/sys_i386_32.c中添加相應(yīng)的調(diào)用代碼asmlinkage int sys_xwlcall(struct timeval *tv) struct timeval ktv; do_gettimeofday(&ktv); copy_to_user(tv,&ktv,sizeof(ktv); printk(KERN_ALERT"PID %ld called sys_xwlcall()./n",(long)current->pid); return 0; (2). 連
10、接系統(tǒng)調(diào)用 a、修改./linux-2.6.33.7/arch/x86/include/asm/unistd_32.h,在系統(tǒng)調(diào)用列表后面相應(yīng)位置添加一行,這樣在用戶空間做系統(tǒng)調(diào)用時(shí)就不需要知道系統(tǒng)調(diào)用號(hào)了,如果在用戶空間指明了調(diào)用號(hào),就可以省略這一步,實(shí)際上我就沒寫:#define _NR_xwlcall338 新增加的調(diào)用號(hào)位338 b、修改./linux-2.6.33.7/arch/x86/kernel/syscall_table_32.S 在ENTRY(sys_call_table)清單最后添加一行,這步至關(guān)重要,338就是這里來的: .long sys_xwlcall (3). 重建
11、新的Linux內(nèi)核先安裝好編譯內(nèi)核必要的軟件包:# sudo apt-get install build-essential kernel-package libncurses5-dev 復(fù)制當(dāng)前內(nèi)核的配置文件 # cp /boot/config-uname -r ./.config 保存配置文件# sudo make menuconfig 使用debian的的內(nèi)核編譯方法,要簡(jiǎn)單很多# sudo make-kpkg-initrd-initrd-append-to-version=xwlcallkernel_image kernel-headers 運(yùn)行以下deb包,安裝內(nèi)核鏡像和模塊:lin
12、ux-image-2.6.33.7xwlcall_2.6.33.7xwlcall-10.00.Custom_i386.deb運(yùn)行以下deb包,安裝內(nèi)核頭文件:linux-headers-2.6.33.7xwlcall_2.6.33.7xwlcall-10.00.Custom_i386.deb 運(yùn)行以下命令,使內(nèi)核啟動(dòng)時(shí)能調(diào)用模塊,比如硬件驅(qū)動(dòng):# sudo update-initramfs -c -k 2.6.33.7xwlcall此次編譯的內(nèi)核采用ubuntu默認(rèn)配置文件,通用性非常好,可以拷貝到大部分x86機(jī)器上安裝。安裝后系統(tǒng)自動(dòng)會(huì)修改grub啟動(dòng)選單。4. 重建引導(dǎo)信息 a、安裝deb
13、包就自動(dòng)重建引導(dǎo)信息了,無須另行處理。 b、如果仍然不放心,可以運(yùn)行# update-grub5. 重新引導(dǎo)從新的內(nèi)核進(jìn)入6. 修改系統(tǒng)調(diào)用表 7. 測(cè)試實(shí)驗(yàn)3.3 Shell編程實(shí)驗(yàn)(進(jìn)程管理實(shí)驗(yàn))1、實(shí)驗(yàn)?zāi)康耐ㄟ^編寫shell程序,了解子進(jìn)程的創(chuàng)建和父進(jìn)程與子進(jìn)程間的協(xié)同,獲得多進(jìn)程程序的編程經(jīng)驗(yàn)。2、實(shí)驗(yàn)內(nèi)容1設(shè)計(jì)一個(gè)簡(jiǎn)單的shell解釋程序,能實(shí)現(xiàn)基本的bsh功能。3、實(shí)驗(yàn)原理 將每一條命令分子段壓入argv棧。然后再子進(jìn)程中調(diào)用execvp()來實(shí)現(xiàn)該命令的功能。4、代碼(源代碼清單)#include <stdio.h> #include <string.h>
14、 #include <stdlib.h> #define BUFFERSIZE 256 /最簡(jiǎn)單的shell,只是簡(jiǎn)單的執(zhí)行命令調(diào)用,沒有任何的其他功能 int main() char bufBUFFERSIZE,*cmd,*argv100; char inchar; int n,sv,buflength; int result; buflength = 0; for(;) printf("=> "); /處理過長(zhǎng)的命令; inchar = getchar();/讀取命令 while (inchar != 'n' && bu
15、flength < BUFFERSIZE ) bufbuflength+ = inchar; inchar = getchar(); if (buflength > BUFFERSIZE) printf("Command too long,please enter again!n"); buflength = 0; continue; else bufbuflength = '0' /解析命令行,分成一個(gè)個(gè)的標(biāo)記 /char *strtok(char *s,char *delim) /分解字符串為一組字符串。s為要分解的字符串,delim為分隔符
16、字符串。 cmd=strtok(buf," tn"); if(cmd) if(strcmp(cmd,"exit")=0) exit(0); n=0; argvn+=cmd; while(argvn+=strtok(NULL," tn"); if(fork()=0) execvp(cmd,argv); fprintf(stderr,"sxh:%s:command not found.n",buf);/如果子進(jìn)程順利執(zhí)行,這段話是不會(huì)執(zhí)行的 exit(1); wait(&sv); buflength = 0;
17、實(shí)驗(yàn)內(nèi)容2編寫一個(gè)帶有重定向和管道功能的Shell1.設(shè)計(jì)思路通過fork()創(chuàng)建子進(jìn)程,用execvp()更改子進(jìn)程代碼,用wait()等待子進(jìn)程結(jié)束。這三個(gè)系統(tǒng)調(diào)用可以很好地創(chuàng)建多進(jìn)程。另一方面,編寫的Shell要實(shí)現(xiàn)管道功能,需要用pipe()創(chuàng)建管道使子進(jìn)程進(jìn)行通信。2.源代碼清單#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #define BUFFERSIZE256 /具有輸入輸出重定向的功能 和管道功能 int main()
18、char buf256,*buf2,*cmd,*cmd2,*argv64,*argv264,*infile,*outfile; char inchar; int n,sv,buflength,fd2; for(;) buflength = 0; printf("=> "); inchar = getchar(); while (inchar != 'n' && buflength < BUFFERSIZE ) bufbuflength+ = inchar; inchar = getchar(); if (buflength >
19、; BUFFERSIZE) fprintf(stderr,"Command too long,please enter again!n"); buflength = 0; continue; else bufbuflength = '0' /檢查是否具有管道操作符 /strstr()在字符串中查找指定字符串的第一次出現(xiàn),buf2指向管道符號(hào)前端的命令 buf2=strstr(buf,"|"); if(buf2) *buf2+='0' else /否則查看是否具有重定向的操作符 in(buf,"<"
20、); out(buf,">"); if(infile) *infile='0' in(infile+1," tn"); if(outfile) *outfile='0' out(outfile+1," tn"); /解析命令行,分成一個(gè)個(gè)的標(biāo)記 cmd=strtok(buf," tn"); /執(zhí)行管道命令 if(buf2) if(strcmp(cmd,"exit")=0) exit(0); if(!cmd) fprintf(stderr,"Comm
21、and token error.n"); exit(1); n=0; /管道后端的命令 argvn+=cmd; while(argvn+=strtok(NULL," tn"); /管道前端的命令 cmd2=strtok(buf2," tn"); if(!cmd2) fprintf(stderr,"Command token error.n"); exit(1); n=0; argv2n+=cmd2; while(argv2n+=strtok(NULL," tn"); pipe(fd); if(fork()
22、=0) dup2(fd0,0); /dup2 復(fù)制文件句柄,將fd0復(fù)制到描述符0。close(fd0); close(fd1); execvp(cmd2,argv2); fprintf(stderr,"* bad commandn"); exit(1); else if(fork()=0) dup2(fd1,1); close(fd0);close(fd1); execvp(cmd,argv); fprintf(stderr,"* bad commandn"); exit(1); close(fd0); close(fd1); wait(&sv
23、); wait(&sv); buflength = 0; /如果沒有管道命令,如果有重定向就執(zhí)行重定向操作,如果沒有重定向就當(dāng)作普通shell命令執(zhí)行 else if(cmd) if(strcmp(cmd,"exit")=0) exit(0); n=0; argvn+=cmd; while(argvn+=strtok(NULL," tn"); if(fork()=0) int fd0=-1,fd1=-1; if(infile) fd0=open(in); if(outfile) fd1=open(out); if(fd0!=-1) dup2(fd
24、0,0);/dup2 復(fù)制文件句柄,將fd0復(fù)制到描述符0。 if(fd1!=-1) dup2(fd1,1);/dup2 復(fù)制文件句柄,將fd1復(fù)制到描述符1。 close(fd0); close(fd1); execvp(cmd,argv); fprintf(stderr,"* Bad commandn"); exit(1); wait(&sv); buflength = 0; /for 實(shí)驗(yàn)4.1 觀察實(shí)驗(yàn)(存儲(chǔ)管理實(shí)驗(yàn))1.實(shí)驗(yàn)步驟 (1)、安裝GDB (2)、編寫觀測(cè)程序(3)、按照指令手冊(cè)進(jìn)行觀察操作2.觀測(cè)程序源代碼#include<stdio.h
25、> #include<stdlib.h> char str50 = "Hello Linux." int main() int num = 10; while(num-) printf("%sn",str); /gcc -g -o testing testing.c3.實(shí)驗(yàn)結(jié)果及分析(1).Gdb程序觀察一個(gè)程序文件的內(nèi)容和結(jié)構(gòu)結(jié)果截圖:(2).GDB觀察程序內(nèi)存映象的內(nèi)容和結(jié)構(gòu)(3).在Linux下,用free 和vmstat命令觀察內(nèi)存使用情況(4).在Linux下,查看/proc與內(nèi)存管理相關(guān)的文件,并解釋顯示結(jié)果實(shí)驗(yàn)5.1 觀察
26、實(shí)驗(yàn)(進(jìn)程通信)在Linux下,用ipcs()命令觀察進(jìn)程通信情況,了解Linux基本通信機(jī)制實(shí)驗(yàn)結(jié)果(截圖):實(shí)驗(yàn)6.3 IO系統(tǒng)編程實(shí)驗(yàn)1、實(shí)驗(yàn)?zāi)康木帉懸粋€(gè)daemon進(jìn)程,該進(jìn)程定時(shí)執(zhí)行 ps命令,然后將該命令的輸出寫至文件F1尾部。通過此實(shí)驗(yàn),掌握Linux I/O系統(tǒng)相關(guān)內(nèi)容。2、實(shí)驗(yàn)內(nèi)容編寫一個(gè)daemon進(jìn)程,該進(jìn)程定時(shí)執(zhí)行 ps命令,然后將該命令的輸出寫至文件F1尾部。3、實(shí)驗(yàn)原理在這個(gè)程序中,首先fork一個(gè)子程序,然后,關(guān)閉父進(jìn)程,這樣,新生成的子進(jìn)程被交給init進(jìn)程接管,并在后臺(tái)執(zhí)行。新生成的子進(jìn)程里,使用system系統(tǒng)調(diào)用,將ps的輸出重定向,輸入到f1.txt里面
27、。4、實(shí)驗(yàn)步驟編寫daemon.c代碼如下:#include<stdio.h> #include<stdlib.h> int main(int argc,char* argv) int i,p; p = fork(); if(p > 0) exit(0); else if(p = 0) for(i = 0; i < 100; i+) sleep(100); system("ps > f1.txt"); else perror("Create new process!"); return 1; 編譯程序# gcc
28、-o daemon daemon.c執(zhí)行程序 # ./daemon實(shí)驗(yàn)7.1 代碼分析(文件系統(tǒng)管理實(shí)驗(yàn))1.實(shí)驗(yàn)?zāi)康牧私馀c文件管理有關(guān)的Linux內(nèi)核模塊的代碼結(jié)構(gòu)。2.實(shí)驗(yàn)結(jié)果(源代碼分析)A. 創(chuàng)建文件模塊分析 5780 /*creat system call */ 5781 Creat() 5782 5783 resister *ip; 5784 extern uchar; 5785 5786 ip = namei(&uchar,1); 5787 if(ip = NULL) 5788 if(u.u_error) 5789 return; 5790 ip = maknode(u.
29、u_arg1&07777&(ISVTX); 5791 if (ip = NULL) 5792 return; 5793 open1(ip,FWRITE,2); 5794 else 5795 open1(ip,FWRITE,1); 5796 第 5786:“namei”( 7 5 1 8 )將一路徑名變換成一個(gè)“inode”指針?!皍char”是一個(gè)過程的名字,它從用戶程序數(shù)據(jù)區(qū)一個(gè)字符一個(gè)字符地取得文件路徑名。 5787:一個(gè)空“inode”指針表示出了一個(gè)錯(cuò),或者并沒有具有給定路徑名的文件存在。 5788:對(duì)于出錯(cuò)的各種條件,請(qǐng)見 UMP 的 CREAT(II)。 5790:
30、“maknode”( 7455)調(diào)用“ialloc”創(chuàng)建一內(nèi)存“ inode”,然后對(duì)其賦初值,并使其進(jìn)入適當(dāng)?shù)哪夸?。注意,顯式地清除了“粘住”位( ISVTX)。 B. 刪除文件 rm 模塊分析 3510 unlink() 3511 3512 resister *ip,*pp;3513 extern uchar; 3514 3515 pp = namei(&uchar,2); 3516 if (pp =NULL) 3517 return; 3518 prele(pp); 3519 ip = iset(pp ->dev,u.u_dent.u_ino); 3520 if (ip =
31、 NULL) 3521 panic (*unlink iset *); 3522 if (ip ->i_mode%IFMT) = IFDIR && !suser() 3523 goto out; 3524 u.u_offset1 = - DIRSIZ+2; 3525 u.ubase = &u.u_dent; 3526 u.ucount = DIRSIZE +2; 3527 u.u_dent.u_ino = 0; 3528 writei(pp); 3529 ip ->i_nlink-; 3530 ip->i_flag =! IUPD; 3531 3532
32、 out: 3533 iput(pp); 3534 iput(ip); 3535 新文件作為永久文件自動(dòng)進(jìn)入文件目錄。關(guān)閉文件不會(huì)自動(dòng)地造成文件被刪除。當(dāng)內(nèi)存“ inode”項(xiàng)中的“ i _ nlink”字段值為 0 并且相應(yīng)文件未被打開時(shí),將刪除該文件。在創(chuàng)建文件時(shí),該字段由“ maknode”賦初值為 1。系統(tǒng)調(diào)用“ link”( 5941 )可將其值加1,系統(tǒng)調(diào)用“unlink”( 3529 )則可將其值減 1。創(chuàng)建臨時(shí)“工作文件”的程序應(yīng)當(dāng)在其終止前執(zhí)行“ unlink”系統(tǒng)調(diào)用將這些文件刪除。 注意,“unlink”系統(tǒng)調(diào)用本身并沒有刪除文件。當(dāng)引用計(jì)數(shù)( i _ count )被減
33、為 0 時(shí)(7350、7362),才刪除該文件。 為了減少在程序或系統(tǒng)崩潰時(shí)遺留下來的臨時(shí)文件所帶來的問題,程序員應(yīng)當(dāng)遵守下列約定: (1) 在打開臨時(shí)文件后立即對(duì)其執(zhí)行“ unlink”操作。 (2) 應(yīng)在“tmp”目錄下創(chuàng)建臨時(shí)文件。在文件名中包括進(jìn)程標(biāo)識(shí)數(shù)就可構(gòu)成一惟一文件名 C. 讀寫模塊分析 5711 Read( ) 5712 5713 rdwr(FREAD); 5714 5720 Write( )5721 5722 rdwr(FWRITE); 5723 5731 rdwr(mode) 5732 5733 resister *fp,m; 5734 5735 m = mode; 573
34、6 fp = setf(u.u_argR0); 5737 if (fp =NILL) 5738 return; 5739 if (fp ->f_flag&m =0) 5740 u.u_error = EBADF; 5741 return; 5742 5743 u.u_base = u.u_arg0; 5744 u.u_count = u.u_arg1; 5745 u.u_segflg = 0; 5746 if(fp ->f_flag&FPIPE) 5747 if(m = FREAD) 5748 readp(fp);else 5749 writep(fp); 5750
35、 else 5751 u.u_offset1 = fp ->f_offset1; 5752 u.u_offset0 = fp ->f_offset0; 5753 if (m = FREAD) 5754 readi(fp ->f_inode);else 5755 writei(fp ->f_inode); 5756 dpadd(fp ->f_offset,u.u_arg1 u.u_count; 5757 5758 u.u_ar0R0 = u.u_arg1 u.u_count; 5759 “read”系統(tǒng)調(diào)用的基本工作過程為: read ( f , b , n ) ;*用戶程序* 發(fā)生陷入
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年德標(biāo)碳鋼法蘭項(xiàng)目可行性研究報(bào)告
- 2025年濾嘴棒項(xiàng)目投資可行性研究分析報(bào)告
- 中國(guó)聚氨酯防潮面漆行業(yè)市場(chǎng)發(fā)展現(xiàn)狀及投資方向研究報(bào)告
- 境外工程項(xiàng)目的風(fēng)險(xiǎn)管理
- 5 一個(gè)豆莢里的五粒豆(教學(xué)設(shè)計(jì))2024-2025學(xué)年統(tǒng)編版語文四年級(jí)上冊(cè)
- 2025年度出差安全管理及應(yīng)急預(yù)案合同
- 2025年度單位間房地產(chǎn)項(xiàng)目貸款協(xié)議
- Unit 5 Topic 3 Section C 教學(xué)設(shè)計(jì) 2024-2025學(xué)年仁愛科普版英語八年級(jí)下冊(cè)
- 廣告媒體代理行業(yè)發(fā)展前景預(yù)測(cè)及投資戰(zhàn)略咨詢報(bào)告
- 2025年數(shù)控拋光中心項(xiàng)目投資可行性研究分析報(bào)告
- 護(hù)士長(zhǎng)管理能力提升-課件
- 運(yùn)動(dòng)按摩全套課件
- 家庭急救知識(shí)(異物卡喉的急救)共45張課件
- 機(jī)臺(tái)異常處理規(guī)定
- 2021年蘇州市職業(yè)大學(xué)職業(yè)適應(yīng)性測(cè)試試題及答案解析
- DBJ∕T 13-253-2016 福建省耐腐蝕混凝土應(yīng)用技術(shù)規(guī)程
- 電鍍廢水中各種重金屬?gòu)U水處理反應(yīng)原理及控制條件
- 數(shù)據(jù)結(jié)構(gòu)英文教學(xué)課件:chapter3 Linked Lists
- 《汽車文化》全套教案
- 會(huì)計(jì)英語專業(yè)詞匯全
- 怎樣把握文章線索
評(píng)論
0/150
提交評(píng)論