The Way of the great learning involves manifesting virtue, renovating the people, and abiding by the highest good.

2010年5月29日星期六

Minix3 Scheduler

Minix3 Scheduler

有关minix3的调度,相关的理论在  Operating Systems - Design and Implementation, 3rd Edition 中的  Scheduling in Interactive Systems   里介绍,
  • Round-Robin Scheduling
  • Priority Scheduling
  • Multiple Queues
  • Shortest Process Next
  • Guaranteed Scheduling
  • Lottery Scheduling
    之类的算法 。这个和批处理系统的job调度不同,所以不可能实现First-Come First-Served,Shortest Job First ,Shortest Reming Time Next之类的调度算法,下面找了一个国外UCSB大学的操作系统课程的相关Minix操作系统的调度算法,有点意思。


Adding a Lottery Scheduler to Minix

This portion of Project 1 requires you to change the process scheduler functionality inside Minix.  Obviously, you will need to modify Minix kernel code to make this happen.  Specifically, you will be adding functionality to support Lottery Scheduling. Lottery scheduling is a flexible way to schedule CPU time by giving each process a number (n) of tickets.  Each time the scheduler is called, it looks at the total number of tickets outstanding (i.e. held by all processes).  It randomly chooses one of the outstanding tickets, and the process holding that ticket is the process that gets to run.  The intuition is that processes can increase or decrease their share of the CPU by getting or giving away tickets.

Modifying the scheduler for Minix should mostly involve modifying code inkernel/proc.c , specifically thesched() and pick_proc() functions (and perhaps enqueue() and dequeue() ). You may also need to modifykernel/proc.h to add elements to the proc structure and modify queue information (NR_SCHED_QUEUES, TASK_Q, IDLE_Q, etc.) and may need to modify PRIO_MIN  and PRIO_MAX in/usr/include/sys/resource.h. Process priority is set in do_getsetpriority()  inservers/pm/misc.c(don't worry—the code in here is very simple), which calls do_nice()  inkernel/system.c. You might be better off just using the nice()system call , which calls do_nice()directly. You'll probably want to modify what do_nice() does to assign or take away tickets.

The current MINIX scheduler is relatively simple. It maintains 16 queues of "ready" processes, numbered 0-15. Queue 15 is the lowest priority (least likely to run), and contains only the IDLE task. Queue 0 is the highest priority, and contains several kernel tasks that never get a lower priority. Queues 1–14 contain all of the other processes. Processes have a maximum priority (remember, higher priorities are closer to 0), and should never be given a higher priority than their maximum priority.  System processes (queues 0-14) are run using their original algorithm, and queue 15 still contains the idle process. However, queue 16 contains all of the runnable user processes, each of which has some number of tickets. The default number of tickets for a new process is 5. However, processes can add or subtract tickets by calling setpriority(ntickets), which will increase the number of tickets by ntickets (note that a negative argument will take tickets away). A process cannot accumulate more than 100 tickets.

Each time the scheduler is called, it should randomly select a ticket (by number) and run the process holding that ticket. Clearly, the random number must be between 0 and Tickets-1, where Tickets is the sum of all the tickets belonging to processes in the ready queue (processes that are blocked are ineligible to run). You may use the random() call (you may need to use the random number code in/usr/src/lib/other/random.c) to generate random numbers and thesrandom() call to initialize the random number generator. A good initialization function to use would be the current date and time.

New processes are created and initialized in kernel/system/do_fork.c. This is probably the best place to initialize any data structures. 




实现方案:



reference:


Minix3 Scheduler

Minix3 Scheduler

有关minix3的调度,相关的理论在  Operating Systems - Design and Implementation, 3rd Edition 中的  Scheduling in Interactive Systems   里介绍,
  • Round-Robin Scheduling
  • Priority Scheduling
  • Multiple Queues
  • Shortest Process Next
  • Guaranteed Scheduling
  • Lottery Scheduling
    之类的算法 。这个和批处理系统的job调度不同,所以不可能实现First-Come First-Served,Shortest Job First ,Shortest Reming Time Next之类的调度算法,下面找了一个国外UCSB大学的操作系统课程的相关Minix操作系统的调度算法,有点意思。


Adding a Lottery Scheduler to Minix

This portion of Project 1 requires you to change the process scheduler functionality inside Minix.  Obviously, you will need to modify Minix kernel code to make this happen.  Specifically, you will be adding functionality to support Lottery Scheduling. Lottery scheduling is a flexible way to schedule CPU time by giving each process a number (n) of tickets.  Each time the scheduler is called, it looks at the total number of tickets outstanding (i.e. held by all processes).  It randomly chooses one of the outstanding tickets, and the process holding that ticket is the process that gets to run.  The intuition is that processes can increase or decrease their share of the CPU by getting or giving away tickets.

Modifying the scheduler for Minix should mostly involve modifying code inkernel/proc.c , specifically thesched() and pick_proc() functions (and perhaps enqueue() and dequeue() ). You may also need to modifykernel/proc.h to add elements to the proc structure and modify queue information (NR_SCHED_QUEUES, TASK_Q, IDLE_Q, etc.) and may need to modify PRIO_MIN  and PRIO_MAX in/usr/include/sys/resource.h. Process priority is set in do_getsetpriority()  inservers/pm/misc.c(don't worry—the code in here is very simple), which calls do_nice()  inkernel/system.c. You might be better off just using the nice()system call , which calls do_nice()directly. You'll probably want to modify what do_nice() does to assign or take away tickets.

The current MINIX scheduler is relatively simple. It maintains 16 queues of "ready" processes, numbered 0-15. Queue 15 is the lowest priority (least likely to run), and contains only the IDLE task. Queue 0 is the highest priority, and contains several kernel tasks that never get a lower priority. Queues 1–14 contain all of the other processes. Processes have a maximum priority (remember, higher priorities are closer to 0), and should never be given a higher priority than their maximum priority.  System processes (queues 0-14) are run using their original algorithm, and queue 15 still contains the idle process. However, queue 16 contains all of the runnable user processes, each of which has some number of tickets. The default number of tickets for a new process is 5. However, processes can add or subtract tickets by calling setpriority(ntickets), which will increase the number of tickets by ntickets (note that a negative argument will take tickets away). A process cannot accumulate more than 100 tickets.

Each time the scheduler is called, it should randomly select a ticket (by number) and run the process holding that ticket. Clearly, the random number must be between 0 and Tickets-1, where Tickets is the sum of all the tickets belonging to processes in the ready queue (processes that are blocked are ineligible to run). You may use the random() call (you may need to use the random number code in/usr/src/lib/other/random.c) to generate random numbers and thesrandom() call to initialize the random number generator. A good initialization function to use would be the current date and time.

New processes are created and initialized in kernel/system/do_fork.c. This is probably the best place to initialize any data structures. 




实现方案:



reference:


2010年4月1日星期四

GoodReader User Manual

GoodReader User Manual

Use your local WiFi-network to transfer files from your desktop computer to GoodReader.

Please note that WiFi-networking can sometimes be tricky, there are some hidden options that can prevent normal networking. Should you have any difficulties, please consult our Troubleshooting guide or contact our friendly and experienced customer support service.

Preparation steps
(must be performed the first time only, no need to do it every time)
Step 1. Make sure you have your own local WiFi network ready.
 

Step 2. Connect your computer to your WiFi network.
 

Step 3. Connect your iPhone / iPod touch to your WiFi network.
 

hide details

 Start the Settings app on your iPhone / iPod touch:
     
 
Go to Wi-Fi section. Turn the Wi-Fi switch on, if it's off. Find your network (to which your computer is connected, or which is hosted by your computer) in the list, select it, and enter the password for this network, if needed. It isvery important to ensure that you're entering the password correctly, because sometimes, if the password is incorrect, it may look like you're connected to a network, but no actual file transfer will happen. The checkmark should appear against your network name:
     
 
Wait for the WiFi antenna logo to appear at the top of the screen. It may take some time, during which your iPhone registers within the network, so just wait for antenna to appear.
 
Actual Transfer
(must be performed every time)
Step 4. Start GoodReader app on your iPhone / iPod touch.
 

Step 5. Open GoodReader's WiFi-transfer page, keep it open all the time while transferring, note the IP-address on this page (you gonna need it in Step 6).
 

hide details

 For security reasons and to save your battery power transfer is allowed only when you're on the WiFi-transfer page. You can open this page with this button on the main screen of GoodReader:
     
 
This button brings up the WiFi-transfer page:
     
 
The important things here are:
  • antenna logo at the top, it should indicate a strong WiFi signal (if you don't see this logo, then you're not connected to WiFi network - go back to Steps 1 and 3)
  • WiFi status: ON (if it's OFF, then you're not connected to WiFi network - go back to Steps 1 and 3)
  • IP-address (if you don't see it, then you're not connected to WiFi network - go back to Steps 1 and 3)
Note the IP-address, you will use it to initiate a connection from your computer.
 
IMPORTANT: IP-addresses change from time to time, even if nothing else changes in your setup. So please don't assume that the IP-address is the same as it was during your last transfer session, check this address every time when connecting.

Step 6. Choose the transfer method and do the actual transfer.
 

hide details

 
"Easy" transfer
(just a couple of files to transfer)
"Professional" transfer
(many files and folders to transfer)
Transfer by connecting your iPhone/iPod to your computer as a network folder:
 

Step 7. After all the transfer is completely done, close WiFi-transfer page.

Step 6
"Professional" transfer - connecting your iPhone as a network folder

This technique connects your iPhone to your desktop computer as a network folder. It creates a network folder shortcut, which you can use later to connect.

Connecting your iPhone to Windows Vista or Windows 7:

  • on Windows Vista or Windows 7 computer open the Start menu and select Computer:
     
  • find the toolbar at the top of Computer window and click Map network drive:
     
  • the dialog window will open. Do NOT enter anything in that dialog. Instead, click the Connect to a Web site that you can use to store your documents and pictures link there:
     


    The Add Network Location wizard will start. The whole point of this wizard is to create a network shortcut (network location) that you can use later to connect to GoodReader and to transfer files. Skip the introduction page:
     


    Select the Choose a custom network location item in the list:
     


    Enter your iPhone's IP-address taken from Step 5 of 
    WiFi File Transfer manual. Enter it exactly as you see it on GoodReader's WiFi-transfer page, with http:// and :8080 parts:
     


    Keep the default name for the network location (network shortcut) suggested by the wizard:
     


    Finish the wizard:
     
  • when the wizard will close, the network folder will open:
     


    This is the folder with all files that are stored inside GoodReader on your iPhone/iPod. Use this folder the same way you use a normal Windows folder - drag-and-drop files there, copy, move, rename, delete, create subfolders, etc.
  • only when you're completely done with file transfer, close GoodReader's WiFi-transfer page
  • the Map Network Drive wizard dialog will stay on the screen at that moment - just close it, don't enter anything there:
     
  • the network shortcut (network location) you've created will be located in the Computer window:
     


    Use it every time you need to connect to GoodReader. But if the IP-address of your iPhone changes, you have to create a new shortcut (new network location) for a new IP-address using the same procedure.
  • please do NOT assume that the IP-address is the same as it was when you were connecting the last time - IP-addresses change periodically, check it every time when connecting

GoodReader User Manual

GoodReader User Manual

Use your local WiFi-network to transfer files from your desktop computer to GoodReader.

Please note that WiFi-networking can sometimes be tricky, there are some hidden options that can prevent normal networking. Should you have any difficulties, please consult our Troubleshooting guide or contact our friendly and experienced customer support service.

Preparation steps
(must be performed the first time only, no need to do it every time)
Step 1. Make sure you have your own local WiFi network ready.
 

Step 2. Connect your computer to your WiFi network.
 

Step 3. Connect your iPhone / iPod touch to your WiFi network.
 

hide details

 Start the Settings app on your iPhone / iPod touch:
     
 
Go to Wi-Fi section. Turn the Wi-Fi switch on, if it's off. Find your network (to which your computer is connected, or which is hosted by your computer) in the list, select it, and enter the password for this network, if needed. It isvery important to ensure that you're entering the password correctly, because sometimes, if the password is incorrect, it may look like you're connected to a network, but no actual file transfer will happen. The checkmark should appear against your network name:
     
 
Wait for the WiFi antenna logo to appear at the top of the screen. It may take some time, during which your iPhone registers within the network, so just wait for antenna to appear.
 
Actual Transfer
(must be performed every time)
Step 4. Start GoodReader app on your iPhone / iPod touch.
 

Step 5. Open GoodReader's WiFi-transfer page, keep it open all the time while transferring, note the IP-address on this page (you gonna need it in Step 6).
 

hide details

 For security reasons and to save your battery power transfer is allowed only when you're on the WiFi-transfer page. You can open this page with this button on the main screen of GoodReader:
     
 
This button brings up the WiFi-transfer page:
     
 
The important things here are:
  • antenna logo at the top, it should indicate a strong WiFi signal (if you don't see this logo, then you're not connected to WiFi network - go back to Steps 1 and 3)
  • WiFi status: ON (if it's OFF, then you're not connected to WiFi network - go back to Steps 1 and 3)
  • IP-address (if you don't see it, then you're not connected to WiFi network - go back to Steps 1 and 3)
Note the IP-address, you will use it to initiate a connection from your computer.
 
IMPORTANT: IP-addresses change from time to time, even if nothing else changes in your setup. So please don't assume that the IP-address is the same as it was during your last transfer session, check this address every time when connecting.

Step 6. Choose the transfer method and do the actual transfer.
 

hide details

 
"Easy" transfer
(just a couple of files to transfer)
"Professional" transfer
(many files and folders to transfer)
Transfer by connecting your iPhone/iPod to your computer as a network folder:
 

Step 7. After all the transfer is completely done, close WiFi-transfer page.

Step 6
"Professional" transfer - connecting your iPhone as a network folder

This technique connects your iPhone to your desktop computer as a network folder. It creates a network folder shortcut, which you can use later to connect.

Connecting your iPhone to Windows Vista or Windows 7:

  • on Windows Vista or Windows 7 computer open the Start menu and select Computer:
     
  • find the toolbar at the top of Computer window and click Map network drive:
     
  • the dialog window will open. Do NOT enter anything in that dialog. Instead, click the Connect to a Web site that you can use to store your documents and pictures link there:
     


    The Add Network Location wizard will start. The whole point of this wizard is to create a network shortcut (network location) that you can use later to connect to GoodReader and to transfer files. Skip the introduction page:
     


    Select the Choose a custom network location item in the list:
     


    Enter your iPhone's IP-address taken from Step 5 of 
    WiFi File Transfer manual. Enter it exactly as you see it on GoodReader's WiFi-transfer page, with http:// and :8080 parts:
     


    Keep the default name for the network location (network shortcut) suggested by the wizard:
     


    Finish the wizard:
     
  • when the wizard will close, the network folder will open:
     


    This is the folder with all files that are stored inside GoodReader on your iPhone/iPod. Use this folder the same way you use a normal Windows folder - drag-and-drop files there, copy, move, rename, delete, create subfolders, etc.
  • only when you're completely done with file transfer, close GoodReader's WiFi-transfer page
  • the Map Network Drive wizard dialog will stay on the screen at that moment - just close it, don't enter anything there:
     
  • the network shortcut (network location) you've created will be located in the Computer window:
     


    Use it every time you need to connect to GoodReader. But if the IP-address of your iPhone changes, you have to create a new shortcut (new network location) for a new IP-address using the same procedure.
  • please do NOT assume that the IP-address is the same as it was when you were connecting the last time - IP-addresses change periodically, check it every time when connecting

The iPhone Becomes a Web Server

The iPhone Becomes a Web Server

When those Apple advertisements tout "there's an app for just about anything," they aren't kidding. The latest example? A new iPhone application which just debuted in Japan's App Store transforms the handheld into a full-blown web server. Called "ServersMan@iPhone", the application allows your iPhone to appear just like any other web server on the internet.

The new application was developed by a Japanese operation called FreeBit, a Tokyo-based venture company known for providing its network platform to many VNO/ISPs (virtual network operator/Internet service providers).

Once the app is installed, PCs on the internet can access the iPhone to upload or download files through a browser or they can use the webDAV protocol. If the PC and the iPhone are on the same network, the PC can connect directly. If they are on separate networks, then FreeBit's VPN software will engage the connection.

serversman.png

The name "ServersMan" is said to be inspired by Sony's "WalkMan," and its no coincidence that FreeBit has invited Sony's former CEO Nobuyuki Idei to be a business advisor for the company.

At the moment, the ServersMan@iPhone is only available in the Japanese App Store, but an English version is coming in March. A port for Windows Mobile devices is also under development.


2010年3月28日星期日

upgrkernel 2.6.33 in ubuntu 9.10

 
Upgrade kernel 2.6.33  in ubuntu 9.10

1.Get the kernel source code of 2.6.33 from kernel.org.Patch the kernel if possible .

2.Extra the tar file,
$tar -xzvf linux-2.6.33.tar.gz

3.$cd linux-2.6.33
$sudo apt-get install ncurses     //a text user interface library!
$make menuconfig
{
a good way  for config the kernel !
it can easily get the hardware driver modules .
cp /boot/config-2.6.31-21-generic .config
$make old config
}

4.compile the kernel
$make bzImage 
then
$make modules
then
$sudo make modules_install
it will copy the modules in  /lib/modules/2.6.33 .

5.install the kernel
$sudo make install
it will run install.sh ,just copy three files into /boot directions

config-2.6.33
System.map-2.6.33
vmlinuz-2.6.33

6.create initrd image
$cd /boot
$sudo mkinitramfs  -o initrd.img-2.6.33 2.6.33
or
$sudo update-initramfs -c -k 2.6.33
the 2.6.33 is the kernel release number ,just keep the same as vmlinuz-*,and /lib/modules/*
for it's a temporary file system ,so it contains some device drivers as the loadable modules ,
these modules must be copied by /lib/modules/2.6.33 directional .

7.configure the grub
alex@alex-laptop:/boot$ sudo update-grub2
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.33
Found initrd image: /boot/initrd.img-2.6.33
Found linux image: /boot/vmlinuz-2.6.31-21-generic
Found initrd image: /boot/initrd.img-2.6.31-21-generic
Found linux image: /boot/vmlinuz-2.6.31-20-generic
Found initrd image: /boot/initrd.img-2.6.31-20-generic
Found linux image: /boot/vmlinuz-2.6.31-14-generic
Found initrd image: /boot/initrd.img-2.6.31-14-generic
Found memtest86+ image: /boot/memtest86+.bin
Found Windows 7 (loader) on /dev/sda1
done



notation:
Grub2  
Vmlinuz 
Kernel_Howto
Boot Process
Initrd
System.map
kernel config


2010年1月31日星期日

Google Native Client: web deluxe, or ActiveX redux?

Throughout the brief history of the web, we’ve seen various attempts to make the browsing experience more interactive:  Java applets, JavaScript (and AJAX techniques), ActiveX, Flash, and Silverlight to name a few.  All of the above except for ActiveX achieve a certain level of security from malicious code through the use of runtime-interpreted languages.  The interpreter layer helps to assure security by not providing access to certain types of operations.

An ActiveX control, on the other hand, is native executable code — so it can do anything you can write an executable to do.  That includes access to the local file system and other resources to which the current user has permissions.  Thus, if you install an ActiveX control from an untrusted source, who knows what you’re getting yourself into?  Soon after the release of ActiveX, one developer famously put up a page on the web (I can’t find it now) that would reboot your system without asking — just to demonstrate the security vulnerabilities inherent in the design.  Not long after that, Microsoft added a security feature to Internet Explorer to ask you before loading any ActiveX controls.

The vulnerabilities associated with ActiveX put most web aficionados off the idea of embedding native code in the browser, even though it naturally performs much better than an interpreted language (although recent improvements in JavaScript performance mitigate that somewhat).  But now Google is reopening the possibility of running native code in the browser, in order to provide a richer Internet experience.  Unlike ActiveX, this technology (appropriately named Native Client) is designed to run in many different browsers (Firefox, Safari, Opera, and Chrome — note no IE), and across various client platforms (Linux, OS/X, and Windows are already supported on x86).

Handling the security issues

Chad Perrin of TechRepublic recently posted his concerns about the security of Native Client.  Apparently, when Native Client loads an executable, it decompiles it to insure that the code follows certain “structural criteria” and doesn’t perform any prohibited action, like creating files on the local file system or accessing the network.  Google admits that this security model presents some challenges.  It seems to me it would be next to impossible to prevent all forms of attack — but hey, Google employs some pretty smart people.  I just hope they aren’t misguided on this.

Google’s Native Client team wants your help in testing to see if you can break their security mechanisms.

Taking it for a spin

To try it out, you must first have Python 2.4 or 2.5 installed on your system (it’s not directly used by Native Client, but it is used for the build and test environments).  Download the software, and follow the build instructions.

Just like ActiveX controls, a Native Client executable can be run within a stand-alone application, or within a web page (if you install the Native Client plugin for your browser).  The tests provided in the download offer both options.  Here’s one of my favorite programs (Conway’s Life simulation) running as a stand-alone app on Windows XP:

And here it is inside Firefox:

The same executable (life.nexe on Windows) is used in both cases — it just uses a different loader in each (a stand-alone executable or a browser plugin).  This example runs very quickly.  You can use the mouse to add cells wherever you click.  I could watch this all day.

The API Reference for Native Client can be found here.  I haven’t read through all of it yet.  The API is written in C++.  I like the fact that it’s cross-platform, but I presume that the executables have to be compiled for each operating system.  Judging from the Python code in the stand-alone loader, I’m guessing that the browser plugin’s loader will automatically look for the platform-correct executable on the host system (UPDATE: Sven corrected my assumption – the .nexe’s generated from the compilation are platform-independent).  But I haven’t tried creating any Native Client modules of my own yet.

This technology is designed to handle processing loads that are deemed too onerous for pure JavaScript, or a combination of JavaScript and server-side processing.  One example would be image editing, which would be quite difficult to do in pure JavaScript (even if you can rely on the Canvas object).  But sending commands to the server to modify the image and ship it back to the client gets expensive in a hurry.  The ideal model would be to perform all the image manipulation in the client and then send the final result back to the server (if needed).

What do you think?  Will Native Client finally give us the processing power we’ve always wanted in the web client?  Or will it open too many security vulnerabilities? Will NaCl be worth its salt?


2010年1月14日星期四

using ptrace for system calls filter


using ptrace for system calls filter

Recently i just develop an online compiler system ,taking the server security into account . For example,if the user use the fork() syscall,and it will cause the server unsafe,the ptrace syscall can realize the system calls filter, if  the daemon find the unsafe syscalls kill the process!

ptrace is a system call found in several Unix and Unix-like operating systems. By using ptrace (the name is a abbreviation of "process trace") one process can control another, enabling the controller to inspect and manipulate the internal state of its target. ptrace is used by debuggers and other code-analysis tools, mostly as aids to software development.
ptrace is used by debuggers (such as gdb and dbx), by tracing tools like strace and ltrace, and by code coverage tools. ptrace is also used by specialised programs to patch running programs, to avoid unfixed bugs or to overcome security features.

#include <sys/ptrace.h>
long ptrace(enum __ptrace_request request, pid_t pid,             void
*addr, void *data);

the example code !
  
  1. #include <sys/ptrace.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <unistd.h>
  5. #include <linux/user.h>   /* For constants
  6.                                    ORIG_EAX etc */
  7. int main()
  8. {   pid_t child;
  9.     long orig_eax;
  10.     child = fork();
  11.     if(child == 0) {
  12.         ptrace(PTRACE_TRACEME, 0NULLNULL);
  13.         execl("/bin/ls""ls"NULL);
  14.     }
  15.     else {
  16.         wait(NULL);
  17.         orig_eax = ptrace(PTRACE_PEEKUSER,
  18.                           child, 4 * ORIG_EAX,
  19.                           NULL);
  20.         printf("The child made a "
  21.                "system call %ld\n", orig_eax);
  22.         ptrace(PTRACE_CONT, child, NULLNULL);
  23.     }
  24.     return 0;
  25. }

    When run, this program prints:

    The child made a system call 11
    along with the output of ls. System call number 11 is execve, and it's the first system call executed by the child. For reference, system call numbers can be found in /usr/include/asm/unistd.h.
    it's cool!

reference
http://linux.die.net/man/2/ptrace
http://www.linuxjournal.com/article/6100

different of real user sys time

   different of  real user sys time 

 When we use $time command ,output just like following,
$time ls
real 0m0.007s
user 0m0.004s
sys 0m0.000s
why real!=user+sys.

Real refers to actual elapsed time; User and Sys refer to CPU time used only by the process.

  • Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).

  • User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

  • Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process. See below for a brief description of kernel mode (also known as 'supervisor' mode) and the system call mechanism.

User+Sys will tell you how much actual CPU time your process used.


So $strace time ls 

i get 
.......
gettimeofday({1263467995, 282946}, NULL) = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb774b728) = 5030
rt_sigaction(SIGINT, {0x1, [INT], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {0x1, [QUIT], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, {ru_utime={0, 4000}, ru_stime={0, 0}, ...}) = 5030
--- SIGCHLD (Child exited) @ 0 (0) ---
gettimeofday({1263467995, 292113}, NULL) = 0
.........
real time just use the gettimeofday() function;
and the sys,user use the wait4() syscall ,and get the data from getrusage structure .
 
  1. struct rusage {
  2.     struct timeval ru_utime; /* user time used */
  3.     struct timeval ru_stime; /* system time used */
  4.     long   ru_maxrss;        /* maximum resident set size */
  5.     long   ru_ixrss;         /* integral shared memory size */
  6.     long   ru_idrss;         /* integral unshared data size */
  7.     long   ru_isrss;         /* integral unshared stack size */
  8.     long   ru_minflt;        /* page reclaims */
  9.     long   ru_majflt;        /* page faults */
  10.     long   ru_nswap;         /* swaps */
  11.     long   ru_inblock;       /* block input operations */
  12.     long   ru_oublock;       /* block output operations */
  13.     long   ru_msgsnd;        /* messages sent */
  14.     long   ru_msgrcv;        /* messages received */
  15.     long   ru_nsignals;      /* signals received */
  16.     long   ru_nvcsw;         /* voluntary context switches */
  17.     long   ru_nivcsw;        /* involuntary context switches */
  18. };
So , taking 
resources like memory, I/O , IPC calls and OS's timeslice mechanism  into account.
why real !=sys+user time ,just clear!

Actually ,we discuss which application or program is more fast ,that means the           
sys+user  time.

So ,how measure  the system or user time .
copy the code 

  1. long
  2. get_run_time ()
  3. {
  4. #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
  5.   struct rusage rusage;
  6.   getrusage (0, &rusage);
  7.   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  8.  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  9. #else /* ! HAVE_GETRUSAGE */
  10. #ifdef HAVE_TIMES
  11.   struct tms tms;
  12.   times (&tms);
  13.   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
  14. #else /* ! HAVE_TIMES */
  15.   /* Fall back on clock and hope it's correctly implemented. */
  16.   const long clocks_per_sec = CLOCKS_PER_SEC;
  17.   if (clocks_per_sec <= 1000000)
  18.     return clock () * (1000000 / clocks_per_sec);
  19.   else
  20.     return clock () / clocks_per_sec;
  21.  
  22. #endif  /* HAVE_TIMES */
  23. #endif  /* HAVE_GETRUSAGE */
  24. }