Você está na página 1de 24

O p e r a t i n gS y s t e m sL a b I n s t r u c t i o n

1C a u t i o n
1. To complete the lab work, you have to be happy with 2 foreign languages, E n g l i s hand C . 2. Time is short. In the lab, you have no time for both understanding the instruction manuals and doing the programing work. So you have to read and understand the lab instructions before you come to the lab. 3. Your work (report + source code) must be handed in within a week after the lab work . 4. Submit your work as a t a r b a l l to wx672ster+os@gmail.com. Besides this tar ball, in your email message you must tell me: 1. Your major work in this report; 2. The major problems you met, and how did you solve (or try solving) them; 3. Reference books and web links; 5. These are Linux projects. Your work must be done in Linux. You are required to finish your report with E m a c so r g m o d e To enforce the above requirement, you have to submit both an o r gf i l e and a generated h t m lf i l eas your lab report. A sample report template written with Emacs org-mode is provided for your reference.

You can get some Chinese help about emacs at Emacs. Org-mode Tutorials David O'Toole Org tutorial Org-mode screencast (local copy) More org-mode tutorials i n f oo r g If you need to draw a picture (e.g. flow chart, etc.), you can use Dia, xfig, or preferably embed an ascii art in your org file. If you need to insert source codes into your org file, you should check: http://orgmode.org/manual/Includefiles.html#Include-files, and you can find some examples in this section. MS-word file will NOT be accepted! 6. Late reports will be penalized 20% per day. 7. Cheating will result in automatic failure of this course.

2A p p r o a c h i n gt ot h eL i n u xk e r n e l
2 . 1p r o cf i l e s y s t e m
Reference Access the filesystem m a np r o c Questions 1. What's the CPU type and model? (/ p r o c / c p u i n f o ) 2. What version of the Linux kernel are you using? (u n a m e ) Linux kernel using the /proc

3. How long has it been since your PC last booted? (u p t i m e ) 4. How much of the total CPU time has been spent executing in user mode? kernel mode? idle? (t o p ) 5. How much memory is configured in your PC? (t o p , f r e e , / p r o c / m e m i n f o ) 6. How much memory is currently (t o p , f r e e , / p r o c / m e m i n f o ) available?

7. How many disk read/write requests have been made? (/ p r o c / d i s k s t a t s ) 8. How many context switches has the kernel performed? (/ p r o c / s t a t ) 9. How many context switches has a process had? (/ p r o c / [ p i d ] / s t a t u s ) 10. How many processes have been created since the system was booted? (/ p r o c / s t a t ) 11. How many processes are there in the ready queue? (/ p r o c / s t a t ) 12. How many processes are blocked waiting for I/O to complete? (/ p r o c / s t a t ) 13. What does the following command do?

c d/ p r o c / ` p s|h e a d2|t a i l1|c u tf 2d '' `& &l sl

Sum up: In your email message, please answer the following questions. 1. How many hours you spent in this section? 2. What's the major difficulties you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

2 . 2P l a yw i t ht h ek e r n e l

Help for beginners Debian Linux Kernel Handbook, Chapter 4 Common kernel-related tasks, 2012 Linux Kernel Newbies, 2013 LinuxChix Kernel Hacking Lessons, 2006, (cs2 mirror) The Linux kernel, Andries Brouwer, Chapter 2 The Kernel Source, 2003, (cs2 mirror) How to get around in the source code tree? The kernel source is in / u s r / s r c / l i n u x / directory. There are several ways to surf this huge repository: 1. RTFS at cs3 2. global (m a ng l o b a l ) 3. cscope (m a nc s c o p e ) 4. ctags (m a nc t a g s ) 5. f i n d+g r e p+l e s s Your Task: (The objective of this task is to get an idea about where and how to view the Linux source code. You are NOT required to understand the code at this moment.) 1. Find the following "famous" code, and tell me why they are "famous". s t a r t _ k e r n e l ( ) ; i n i t ( ) ; s t r u c tt a s k _ s t r u c t ; s t r u c ti n o d e ; s t r u c tf i l e ; f o r k ( ) ; i n c l u d e / a s m g e n e r i c / u n i s t d . h a r c h / x 8 6 _ 6 4 / e n t r y . S k e r n e l / p r i n t k . c

2. Finish lesson 2-6 of LinuxChix Kernel Hacking Lessons (cs2 mirror). And you are encouraged to go through lesson 7-9, though it's not mandatory. Sum up: In your email message, please answer the following questions. 1. How many hours you spent in this section? 2. What's the major difficulties you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

2 . 3H e l l o ,k e r n e lm o d u l e !
Reference The Linux Kernel Module Programming Guide, 2007 Access the Linux filesystem, 2006 Quick start 1. Find out the right kernel headers package matching your running kernel:
u n a m ea a p t i t u d es e a r c h^ l i n u x h e a d e r s

kernel

using

the

/proc

AM a k e f i l etutorial, 2013

install the right headers-package matching your running kernel:


s u d oa p t i t u d ei n s t a l ll i n u x h e a d e r s ` u n a m er `

2. Make a directory to store your H e l l o , w o r l d ! kernel module

m k d i rh e l l o l k m& &c dh e l l o l k m

3. In h e l l o l k m directory, write a hello-1.c. (See The Linux Kernel Module Programming Guide chapter 2 for details.) 4. Write a Makefile 5. make
m a k e

If everything is ok, there should be a h e l l o 1 . k o file in your h e l l o l k mdirectory. 6. Install the module into the running kernel
s u d oi n s m o dh e l l o 1 . k o

7. See if it's running


l s m o d|g r e ph e l l o

Does it show up? Well, and you can see "Hello, world!" here:
d m e s g|t a i l

8. Now, you can remove this useless module from the kernel by doing:
s u d or m m o dh e l l o _ 1

9. See if it's removed:


l s m o d|g r e ph e l l o

And you should see "Goodbye, world!" here:


d m e s g|t a i l

2.3.1 Questions

1. What's a kernel module? 2. How do modules get into the kernel? 3. How do you know a kernel module is loaded? 4. How do you know a module is working properly or not? 5. How do you unload a module? 6. What's the major problems you met in this section? And how did you solve (or try solving) them? 2.3.2 Sum up In your email questions. message, please answer the following

1. How many hours you spent in this section? 2. What's the major problems you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

2 . 4S y s t e mc a l l s
Reference LINUX KERNEL COMPILATION & SYSTEM CALL IMPLEMENTATION TUTORIAL, 2010. How to compile & recompile linux kernel in Ubuntu , 2011. (cs2 copy) How to add a system call in linux kernel (Ubuntu OS), 2011. (cs2 copy) Implementing a System Call on Linux 2.6 for i386, 2006. (cs2 mirror) LinuxChix Kernel Hacking Lessons, 2006. (cs2 mirror) Kernel command using Linux system calls The Linux kernel, Andries Brouwer, Chapter 4

(cs2 mirror) Question about a s m l i n k a g e Your task: Follow the instructions in Implementing a System Call on Linux 2.6 for i386 (cs2 mirror) to write your own system call. Note: This tutorial is based on 2.6.17. If you want to try it on a newer kernel, you can take a look at: LINUX KERNEL COMPILATION & SYSTEM CALL IMPLEMENTATION TUTORIAL - 2.6.20 How to compile & recompile linux kernel in Ubuntu - 2.6.37 (cs2 copy) How to add a system call in linux kernel (Ubuntu OS) - 2.6.37 (cs2 copy) Sum up: In your email message, please answer the following questions. 1. How many hours you spent in this section? 2. What's the major problems you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

3P r o c e s sM a n a g e m e n t
Reference Fork, Exec and Process control POSIX thread (pthread) libraries Command line commands: 1. p s
p sa f

Get

sense

of

the

following

p sa x p se L f

2. p s t r e e 3. n i c e / r e n i c e 4. t o p
m a nt o p

Process Status: D : uninterruptible sleep R : running S : sleeping T : traced or stopped Z : zombie

3 . 1P r o c e s sc r e a t i o n
Reference m a nf o r k m a ne x e c m a nw a i t m a ne x i t m a n2k i l l Your tasks 1. There is an example c program at the end of the w a i t manual page (m a nw a i t ). Read it, compile it, and run it. Then answer the following questions: 1. Both e x i t ( ) and _ e x i t ( ) are used in the program. What's the difference? 2. Tell me about the following line of code:

w=w a i t p i d ( c p i d ,& s t a t u s ,W U N T R A C E D|W C O N T I N U E D ) ;

2. s y s t e m ( ) ,f o r k ( ) ,e x e c ( ) Compile and run the following 4 programs. Tell me what they do? And their differences?
# i n c l u d e< s t d l i b . h > # i n c l u d e< s t d i o . h > i n tm a i n ( ) { p r i n t f ( " R u n n i n gp sw i t hs y s t e m \ n " ) ; s y s t e m ( " p sa x& " ) ; p r i n t f ( " D o n e . \ n " ) ; e x i t ( 0 ) ; } # i n c l u d e< u n i s t d . h > # i n c l u d e< s t d i o . h > i n tm a i n ( ) { p r i n t f ( " R u n n i n gp sw i t he x e c l p \ n " ) ; e x e c l p ( " p s " ," p s " ," a x " ,0 ) ; p r i n t f ( " D o n e . \ n " ) ; e x i t ( 0 ) ; } # i n c l u d e< s t d i o . h > # i n c l u d e< u n i s t d . h > i n tm a i n ( ) { p i d _ tc h i l d _ p ; p r i n t f ( " R u n n i n gp sw i t hf o r k \ n " ) ; c h i l d _ p=f o r k ( ) ; e x e c l p ( " p s " ," p s " ," a x " ,0 ) ; r e t u r n0 ; } # i n c l u d e< u n i s t d . h >

# i n c l u d e< s t d i o . h > i n tm a i n ( ) { p i d _ tp i d ; p r i n t f ( " R u n n i n gp sa g a i nw i t hf o r k \ n " ) ; p i d=f o r k ( ) ; i f(p i d= =0){/ /i nt h ec h i l d ,d oe x e c e x e c l p ( " p s " ," p s " ," a x " ,0 ) ; } e l s ei f( p i d<0 )/ /f a i l e dt of o r k { p r i n t f ( " f o r kf a i l e d . \ n " ) ; e x i t ( 1 ) ; } e l s e/ /p a r e n t { w a i t ( N U L L ) ; } e x i t ( 0 ) ; }

3. more on f o r k ( )and w a i t ( ) Compile and run this program. Tell me why the output is weird (mixed with the $ prompt)? And fix it with the w a i t ( )system call. 4. zombies and w a i t p i d ( ) Read the NOTES section in the w a i t manual page (m a n2w a i t ) to get a clear idea about zombie processes. And tell me why zombie is not welcomed. At the end of w a i t manual page (m a n 2 w a i t ), there is an example program. Play with it, and tell me about W U N T R A C E D , W C O N T I N U E D , W I F E X I T E D , W E X I T S T A T U S , W I F S I G N A L E D , W T E R M S I G , W I F S T O P P E D , W S T O P S I G ,W I F C O N T I N U E D ,p a u s e ( ) . Compile and run this small program. This program can leave a zombie process in

the system. You can see it with


p su

Your task: Write a similar program that leaves 5 zombies. Tell me what's the difference between a zombie process and a orphan process? Read Beginning Linux Programming, Chapter 11, page 503 to learn how to avoid zombies with w a i t p i d ( ) system call. And correct the above program. Tell me the difference between e x i t ( )and r e t u r n . Sum up: In your email message, please answer the following questions. 1. How many hours you spent in this section? 2. What's the major problems you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

3 . 2T h r e a d
Reference m a n3p t h r e a d _ c r e a t e m a n3p t h r e a d _ a t t r _ i n i t Tasks 1. At the end of p t h r e a d _ c r e a t emanual page (m a n 3 p t h r e a d _ c r e a t e ), there is an example

program. Play with it, and then tell me: 1. What's the t i n f o [ ] ? 2. What's the r e s ? 2. At the end of p t h r e a d _ a t t r _ i n i t manual page (m a n3p t h r e a d _ a t t r _ i n i t ), there is an example program. Compile and run it. 3. Compile and run this program. Now, remove the p t h r e a d _ j o i ncall, i.e. comment out line 29-32. Compile and run it again for multiple times. Tell me the difference, and why? Sum up: In your email message, please answer the following questions. 1. How many hours you spent in this section? 2. What's the major difficulties you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

3 . 3I P C
Reference: Beej's Guide to Unix IPC (cs2 mirror) 3.3.1 Signals Reference Beej's Guide to Unix IPC, section 3 (cs2 mirror) signal.h How to understand function prototype signal() The Function Pointer Tutorials typedef m a n2s i g n a l ,m a n7s i g n a l m a ns i g a c t i o n

k i l l ,k i l l a l l Tasks 1. understanding s i g n a l ( ) If you can understand


t y p e d e fi n tM Y I N T ; M Y I N Tm y f u n c t i o n ( M Y I N T ) ;

Then, you should be able to understand


t y p e d e fv o i d( * s i g h a n d l e r _ t ) ( i n t ) ; s i g h a n d l e r _ ts i g n a l ( i n ts i g n u m ,s i g h a n d l e r _ th a n d l e r ) ;

Now, tell me your understanding about the following function prototype

v o i d( * s i g n a l ( i n ts i g ,v o i d( * f u n c ) ( i n t ) ) ) ( i n t ) ; ` v ' ` >* s i g n a l ( )i saf u ` p o i n t i n ` >* s i g n a l ( )t a k e s2 . s i g-a ni n t .f u n c-a' f u n

v o i d( * s i g n a l ( i n ts i g ,v o i d( * f u n c ) ( i n t ) ) ) ( i n t ) ; v o i d( * ) ( i n t ) ; ` v ' ` >v o i d ( * ) ( i n t )i saf u n c t i o np o i w h i c hi sr e t u r n e db yf u n c t i o n i t ' sp o i n t i n gt oaf u n c t i o nt a

m a n2s i g n a lcan help 2. s i g a c t i o n ( ) Following Beej's Guide to Unix IPC, section 3 to play with signals. And then tell me details about the following code

i n ts i g a c t i o n ( i n ts i g n u m ,c o n s ts t r u c ts i g a c t i o n* a c t ,s t r

m a n2s i g a c t i o ncan help 3.3.2 Pipe Reference Beej's Guide to Unix IPC, section 4 (cs2 mirror) m a np i p e m a n2w r i t e m a n2r e a d Tasks 1. Follow Beej's Guide to Unix IPC, section 4 step by step to learn how to use p i p e . 2. Modify pipe3.c in Beej's Guide to Unix IPC, section 4 to make the child does the w cl , and the parent does the l s 3. At the end of p i p e manual page (m a n2p i p e ), there is an example program. Compile it, run it, understand it, and then, modify the program, let parent do read, and child do write. 3.3.3 FIFO Reference Beej's Guide to Unix IPC, section 5 (cs2 mirror) m a nf i f o m a nm k f i f o m a n3m k f i f o m a nm k n o d m a n2m k n o d Tasks 1. Follow Beej's Guide to Unix IPC, section 5 step by step to learn how to use FIFOs. 2. When you run the example programs (s p e a kand

t i c k ), there should be a new file named a m e r i c a n _ m a i dappear in your working directory ($ P W D ). What will happen if you delete this FIFO file while the two programs running? Why? 3. Modify the example programs to use m k f i f o instead of m k n o d . 4. Extend the example programs, and make it have 3 writers. 3.3.4 File Locking Reference Beej's Guide to Unix IPC, section 6 (cs2 mirror) m a nf l o c k m a n2f l o c k m a nf c n t l i n c l u d e / a s m g e n e r i c / f c n t l . h Tasks 1. Follow Beej's Guide to Unix IPC, section 6 step by step to learn how to use File locks. 2. Try the example l o c k d e m o . c with both F _ R D L C K and F _ W R L C K . 3. Tell me whether the locked file, e.g. l o c k d e m o . c can be delete while the programs are running? And why? 3.3.5 Message Queues Reference Beej's Guide to Unix IPC, section 7 (cs2 mirror) m a ni p c s m a ni p c r m m a ni p c m k

m a nm s g g e t m a nf t o k m a nm s g s n d ,m a nm s g r c v m a nm s g c t l m a no f f s e t o f i n c l u d e / l i n u x / m s g . h Tasks 1. Follow Beej's Guide to Unix IPC, section 7 step by step to learn how to use message queues. 2. What happens when you're running both in separate windows and you kill one or the other? 3. Also try running two copies of k i r k or two copies of s p o c kto get an idea of what happens when you have two readers or two writers. 4. Another interesting demonstration is to run kirk, enter a bunch of messages, then run spock and see it retrieve all the messages in one swoop. Just messing around with these toy programs will help you gain an understanding of what is really going on. 5. What happens if you i p c r mthe queue while it's in use? Why? 6. Create a message queue with i p c m k , and use it in your programs. 3.3.6 Semaphores Reference Beej's Guide to Unix IPC, section 8 (cs2 mirror) i n c l u d e / l i n u x / s e m . h m a ns e m g e t m a ns e m o p m a ns e m c t l

Tasks 1. Follow Beej's Guide to Unix IPC, section 8 step by step to learn how to use semaphores. 2. Semaphores are used to lock some shared resources to enforce mutual-exclusion. In the demo program s e m d e m o . c , what's locked? 3. Draw a flow chart to show how the demo program works. 3.3.7 Sum up In your email questions. message, please answer the following

1. How many hours you spent in this section? 2. What's the major difficulties you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

4M e m o r ym a n a g e m e n t
Reference User-Level Memory Management in Linux Programming, 2004.

4 . 1B a s i cc o m m a n d s
1. top
t o p

With the help of m a nt o p , get a clear idea about:

V I R T
Virtual Image (kb) The total amount of virtual memory used by the

task. It includes all code, data and shared libraries plus pages that have been swapped out. VIRT = SWAP + RES

S W A P
Swapped size (kb) The swapped out portion of a task's total virtual memory image.

R E S
Resident size (kb). (p sa l y ). The non-swapped physical memory a task has used. RES = CODE + DATA

C O D E
Code size (kb) The amount of physical memory devoted to executable code, also known as the 'text resident set' size or TRS.
Table of Contents

D A T A
Data+Stack size (kb) The amount of physical memory devoted to other than executable code, also known as the 'data resident set' size or DRS.

S H R
Shared Mem size (kb) The amount of shared memory used by a task. It simply reflects memory that could be potentially shared with other processes.

n F L T
Page Fault count The number of major page faults that have occurred for a task. A page fault occurs when a process attempts to read from or write to a virtual

page that is not currently present in its address space. A major page fault is when backing storage access (such as a disk) is involved in making that page available.

n D R T
Dirty Pages count The number of pages that have been modified since they were last written to disk. Dirty pages must be written to disk before the corresponding physical memory location can be used for some other virtual page. 2. s i z e ,o b j d u m p ,n m Compile this c program with
g c cW a l ls i z e . c

Then, use s i z eto see the size of its text, data, and bss segments.
s i z ea . o u t

The output should be something like


t e x t 1 2 0 0 d a t a 5 2 0 b s s 1 0 2 4 0 3 2 d e c 1 0 2 5 7 5 2 h e x f a 6 d 8 f i l e n a m e a . o u t

If you exam its size with l s l , you should get something similar to the following line
r w x r x r x 1w x 6 7 2 w x 6 7 2

6 6 2 7O c t1 81 8 : 0 5a .

Tell me: 1. What does the 6 6 2 7mean? 2. What do the 1 2 0 0 , 5 2 0 , 1 0 2 4 0 3 2 , and 1 0 2 5 7 5 2 mean?

4 . 2S h a r e dM e m o r yS e g m e n t s
Reference Beej's Guide to Unix IPC, section 9 (cs2 mirror) m a ns h m g e t m a ns h m a t m a ns h m d t m a ns h m c t l m a ni p c r m m a ni p c s Your tasks 1. Follow Beej's Guide to Unix IPC, section 9 step by step to learn how to use shared memory segments. 2. Use i p c r m to remove the segment you just created while running the example code. 3. Add semaphore mechanism into the sample program (s h m d e m o . c ) to enforce mutualexclusive access to the shared data area.

4 . 3M e m o r yM a p p e dF i l e s
Reference Beej's Guide to Unix IPC, section 10 (cs2 mirror) m a nf s e e k m a nm m a p ,m a nm u n m a p m a ng e t p a g e s i z e m a ns t a t Your tasks 1. Follow Beej's Guide to Unix IPC, section 10 step by step to learn how to use memory mapped files.

2. Write a small program to find out the page size of your Linux PC. 3. Add semaphore mechanism into the sample program (m m a p d e m o . c ) to enforce mutualexclusive access to the shared data area.

4 . 4S u mu p
In your email questions. message, please answer the following

1. How many hours you spent in this section? 2. What's the major difficulties you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

5F i l eS y s t e m
Reference Ext2 file system analysis Ext2 documentation Anatomy of the Linux file system The VFS (Virtual File System) in Linux Kernel V2.4 A Play In 5 Acts

5 . 1F i l es y s t e mc r e a t i o n
1. Create a initialized file
d di f = / d e v / z e r oo f = f s . i m gb s = 1 kc o u n t = 1 0 0 0 0

You now have a file called f s . i m gthat's 10MB. 2. Use the l o s e t u p command to associate a loop device with the file (making it look like a block device instead

of just a regular file within the file system):


s u d ol o s e t u p/ d e v / l o o p 0f s . i m g

3. Creating an ext2 file system within the loop device


s u d om k e 2 f s/ d e v / l o o p 0

4. Mount the newly directory

created file

system onto

/ m n t

s u d om o u n tte x t 2/ d e v / l o o p 0/ m n t

or
s u d om o u n tol o o pf s . i m g/ m n t

5. Get information about your new file system


d u m p e 2 f s/ d e v / l o o p 0

5.1.1 Your tasks 1. By following the above 5 steps, you've got a new file system mounted on / m n t directory. Now you can get into / m n t , and create a new file in it. And then associate it with a loop device (/ d e v / l o o p 1 ), and create another file system in it. 2. Plug your USB disk into your PC's USB port, and check 1. what file system it is? 2. which directory it's mounted on? 3. Use f s c k to get some details about your USB disk.

5 . 2F i n d i n gaf i l ew i t hh e x d u m p
Follow the instructions in http://cs2.swfu.edu.cn/~wx672/lecture_notes/os/ext2analysis.html#sec-3 to learn how to find a file with h e x d u m p

in your newly created file system. And then answer this question: After you do 'c a th e l l o ' at the command line, you will see on the screen the content of file h e l l o , in our case it is 'h e l l o w o r l d '. Now give me a detailed picture about what is happened in the OS from 'c a th e l l o ' to 'h e l l o w o r l d ' is shown on the screen.

5 . 3S u mu p
In your email questions. message, please answer the following

1. How many hours you spent in this section? 2. What's the major difficulties you met in this section? And, 3. How did you solve (or try solving) them? 4. Have you learnt anything? Or your time was just wasted?

Date: 2013-11-17T21:44+0800

Author: WANG Xiaolin (wx672ster+os@gmail.com

Você também pode gostar