Linux Process Management

类别:编程语言 点击:0 评论:0 推荐:
>沒有中文,希望大家能接受!
>
>--
>Love need to learn.
>Love need to pay.
>wade, coventive, 886-2-8226-3600 ext 229
>

>
>
> ------------------------------------------------------------------------
>
>    1. P.15 Chapter 2 - Process Management
>          1. Process include :
>                1. text section: executing program code
>                2. data section: global variables, a set of resources
>                   such as open files, pending signals, an address space
>                3. one or more threads of execution
>          2. Each thread includes a unique program counter, process
>             stack, and set of processor registers, but linux does not
>             differentiate between threads and processes.
>          3. Processes provide two vitualizations:
>                1. virtualized proessor : sharing the processor among
>                   dozens of other processes
>                2. virtual memory : it lets the process allocate and
>                   manage memory as if it alone owned all the memory in
>                   the system.
>          4. When a process exits, it is placed into a special zombie
>             state that is used to represent terminated processes until
>             the parent calls wait() or waitpid()
>          5. The kernel representation of a running program as a task,
>             and the user-space representation as a process.
>    2. P.16 The Process Descriptor and Task Structure
>          1. The kernel stores the processes in a circular doubly
>             linked list called the task list.
>          2. The process descriptor contains the data that describe the
>             executing program: running state, open files, the
>             process's address space, pending signals, permission,
>             semaphore, executing time...
>          3. The task_struct is allocated via the slab allocator to
>             provide object reuse and cache coloring.
>          4. Prior to the 2.6 kernel series, the task_struct was stored
>             at the end of the kernel stack of each process. This
>             allowed architectures with few registers, such as x86, to
>             calculate the location of the process descriptor via the
>             stack pointer without using an extra register to store the
>             location.
>    3. P.18 Storing the Process Descriptor
>          1. The system identifies processes by a unique PID. The
>             default maximum value is 32767(up to 4294967296).
>          2. It is very useful to be able to quickly lookup the process
>             descriptor of the currently executing task, which is done
>             via the current macro. This macro must be separately
>             implemented by each supported architecture.
>    4. P.19 Process states:
>          1. The state field of the process descriptor describes the
>             current condition of the process. The value is represented
>             by one of five flags:
>                1. TASK_RUNNING: it is either currently running or on a
>                   runqueue waiting to run (runqueue are discussed in
>                   Chap. 3).
>                2. TASK_INTERRUPTIBLE: it is sleeping, blocked, waiting
>                   for some condition to occur. When this condition
>                   occurs, the kernel sets the process's state to
>                   TASK_RUNNING. The processes also awakes prematurely
>                   and becomes runnable if it receives a signal.
>                3. TASK_UNINTERRUPTIBLE: it will not wake up and become
>                   runnable if it receives a signal. (see fork())
>                4. TASK_ZOMBIE: The task has terminated, but its parent
>                   has not yet issued a wait4() system call. The task's
>                   process descriptor must remain in case the parent
>                   wants to access it.
>                5. TASK_STOPPED: This occurs if the task receives the
>                   SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU signal or if
>                   it receives any signal while it is being debugged.
>
>             --- refer ---
>
>                1. running: on user space.
>                2. interrupt routine: a exception handler such as KBD,
>                   timer.
>                3. system call: such as read, write.
>                4. waiting: wait for an event.
>                5. return: kernel will check scheduler, signals.
>                6. ready: ready to run.
>
>          interrupt       (kernel)         scheduler
>    ----------.--------------------------------.--------------
>              .                                .
>              .                                .
>     running ===+==> system call <==================> waiting
>        ^     . |     |                        .       ^
>        |     . |     |                        .       |
>        |     . |=====|=> interrupt routing <==========+
>        |     .       |   |                    .
>        |     .       |   |                    .
>        |     .       v   v                    .
>        +-----.----- return  <=======================> ready
>              .                                .
>              .                                .
>     
>
>    5. P.20 Manipulating the Current Process State
>          1. Using the set_task_state(task, state) function to set the
>             given task to the given state.
>    6. P.20 Process Context
>          1. One of the most important parts of a process is the
>             executing program code.
>          2. When a program executes a system call or triggers an
>             exception, it enters kernel-space. At this point, the
>             kernel is said to "be executing on behalf of the process"
>             and is in process context.
>          3. When in process context, the current macro is valid.
>          4. All processes are descendents of the init process whose
>             PID is 1.
>          5. Every process on the system has exactly one parent.
>             Likewise, every process can have one or more children. The
>             relationship between processes is stored in the process
>             descriptor.
>
># pstree
>  init-+-apache--5*[apache]
>       |-apmd
>       |-atd
>       |-bash-+-bash--Xprt
>       |      |-logger
>       |      `-tee
>       |-bdflush
>       |-brltty
>       |-cron
>       |-5*[getty]
>       |-inetd
>       |-kapmd
>       |-keventd
>       |-khubd
>       |-kjournald
>       |-klogd
>       |-kreiserfsd
>       |-ksoftirqd_CPU0
>       |-kswapd
>       |-kupdated
>       `-syslogd
>     
>
>    7. P.22 Process Creation
>          1. fork() : creates a child process that is a copy of the
>             current task. It differs from the parent only in its PID,
>             its PPID, and certain resources and statistics, such as
>             pending signals, which are not inherited.
>          2. exec() : loads a new executable into the address space and
>             begins executing it.
>          3. With Linux, fork() is implemented using copy-on-write
>             pages. Instead of duplicating the process address space,
>             the parent and the child can share a single copy. The data
>             is marked in such a way that if it is written to, a
>             duplicate is made and each process receives a unique copy.
>          4. If exec() is called immediately after fork() - they never
>             need to be copied. The only overhead incurred by fork() is
>             the duplication of the parent's page tables and the
>             creation of a unique process descriptor for the child.
>    8. P.23 fork()
>          1. fork() <-- clone() <-- do_fork() <-- copy_process()
>                1. Calls dup_task_struct() which creates a new kernel
>                   stack, thread_info structure, and task_struct for
>                   the new process whose values are identical to those
>                   of the current task.
>                2. Check that the new child will not exceed the
>                   resource limits on the number of processes for the
>                   current user.
>                3. Now the child needs to differentiate itself from its
>                   parent. Various members of the process descriptor
>                   are cleared or set to initail values.
>                4. Next, the child's state is set to
>                   TASK_UNINTERRUPTIBLE, to insure it does not yet run.
>                5. Calls copy_flags() to update the flags member of the
>                   task_struct. The PF_SUPERPRIV flag, which denotes
>                   whether a task used super-user priv, is cleared. The
>                   PF_FORKNOEXEC flag, which denotes a process that has
>                   not called exec(), is set.
>                6. Calls get_pid() to assign an available PID to the
>                   new task.
>                7. Depending on the flags passed to clone(), either
>                   copy or share open files, filesystem information,
>                   signal handlers, process address space, and namespace.
>                8. Share the remaining timeslice between the parent and
>                   its child.
>                9. Cleanup and return a pointer to the new child.
>          2. ==> The kernel runs the child process first. (avoid
>             copy-on-write overhead)
>    9. P.24 vfork()
>          1. The vfork() has the same effect as fork(), except that the
>             page table entries of the parent process are not copied.
>             Instead, the child executes as the sole thread in the
>             parent's address space, and the parent is blocked until
>             the child either calls exec() or exits.
>          2. The child is not allowed to write to the address space.
>          3. ==> use fork() is ok in kernel > 2.2.
>   10. P.25 The Linux Implementation of Threads
>          1. The Linux kernel done not provide any special scheduling
>             semantics or data structures to represent threads.
>             Instead, a thread is merely a process which share certain
>             resources. Each thread has a unique task_struct and
>             appears to the kernel as a normal process which shares the
>             address space, filesystem resources, file descriptors, and
>             installed signal handlers.
>          2. Threads are created like normal task with the exception
>             that the clone() system call is passed flags corresponding
>             to specific resources to be shared:
>             clone (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
>             fork() can be implemented by clone() as:
>             clone (SIGCHLD, 0);
>             vfork() as:
>             clone (CLONE_VFORK | CLONE_VM | SIGCHLD, 0);
>
>- clone() Flags:
>
>  Flag     Meaning
>  ==============   ======================================================
>  CLONE_CLEARTID   clear the TID
>  CLONE_DETACHED   the parent does not want a SIGCHLD signal send on exit
>  CLONE_FILES    parent and child share open files
>  CLONE_FS    parent and child share filesystem information
>  CLONE_IDLETASK   set PID to zero (only used by the idle tasks)
>  CLONE_NEWNS    create a new namespace for the child
>  CLONE_PARENT    child is to have same parent as its parent
>  CLONE_PTRACE    continue tracing child
>  CLONE_SETTID    write the PID back to user-space
>  CLONE_SETTLS    create a new TLS for the child
>  CLONE_SIGHAND    parent and child share signal handlers
>  CLONE_SYSVSEM    parent and child share System V SEM_UNDO semantics
>  CLONE_THREAD    parent and child aew in the same thread group
>  CLONE_VFORK    vfork() was used and the parent will sleep until the
>     child wakes it
>  CLONE_VM    parent and child share address space
>  ==============   ======================================================
>     
>
>   11. P.26 Kernel Threads
>          1. Kernel Threads is often useful for the kernel to perform
>             some operations in the background -- standard processes
>             that exist solely in kernel space.
>          2. The significant difference between kernel threads and
>             normal processes is that kernel threads do not have an
>             address space and do not context switch into user-space.
>             Kernel threads are schedulable and preemptable as normal
>             processes.
>         3.
>
> # ps axw
>  PID TTY      STAT   TIME COMMAND
>    1 ?        S      0:05 init [5]
>    2 ?        SW     0:00 [keventd]
>    0 ?        SWN    0:00 [ksoftirqd_CPU0]
>    0 ?        SW     0:11 [kswapd]
>    0 ?        SW     0:00 [bdflush]
>    0 ?        SW     0:00 [kupdated]
>    7 ?        SW     0:00 [khubd]
>    8 ?        SW     1:45 [kjournald]
>   60 ?        SW     0:00 [kapmd]
>         
>
>          4. Indeed, a kernel thread can only be created by another
>             kernel thread. The interface for spawning a new kernel
>             thread from an existing one is: int kernel_thread (int
>             (*fn)(void *), void * arg, unsigned long flags) flags:
>             most kernel threads pass CLONE_FS | CLONE_FILES |
>             CLONE_SIGHAND
>   12. P.27 Process Termination
>          1. Typically, processes destruction occurs when the process
>             calls the exit() system call: exit() or return from main().
>          2. A process can also terminate when it receives a signal or
>             exception it cannot handle or ignore.
>          3. do_exit() in kernel complete a number of chores(in
>             kernel/exit.c):
>                1. Set the PF_EXITING flag in the flags member of the
>                   task_struct.
>                2. If BSD process accounting is enabled, call
>                   acct_process() to write out accounting information.
>                3. Call __exit__mm() to release the mm_struct held by
>                   this process.
>                4. Call sem_exit(). Dequeue its IPC semaphor if it
>                   queued waiting for.
>                5. Call __exit_files(), __exit_fs(), exit_namespace(),
>                   and exit_sighand() to decrement the usage count of
>                   objects related to file descriptors, filesystem
>                   data, the process namespace, and signal handlers,
>                   respectively.
>                6. Set the task's exit code, stored in the exit_code
>                   member of the task_struct, to the code provided by
>                   exit() or whatever kernel mechanism forced the
>                   termination.
>                7. Call exit_notify() to send signals to the task's
>                   parent, reparent any of the task's children to
>                   another thread in their thread group or the init
>                   process, and set the task's state to TASK_ZOMBIE.
>                8. Finally, call schedule() to switch to a new process,
>                   this is the last code the task will ever execute.
>                9. ==> The only memory it occupies is its kernel stack
>                   and slab object, which contain its thread_info and
>                   task_struct structures. The task exists solely to
>                   provide information to its parent.
>   13. P.28 Removal of the Process Descriptor
>          1. The acts of cleaning up after a process and removing its
>             process descriptor are separate.
>          2. When it is time to finally deallocate the process
>             descriptor, release_task() is invoked. It does the following:
>                1. Call free_uid() to decrement the usage count of the
>                   process's user.
>                2. Call unhash_process() to remove the process from the
>                   pidhash and remove the process from the task list.
>                3. If the task was ptraced, reparent it to its original
>                   parent and remove it from the ptrace list.
>                4. Call put_task_struct() to free the pages containing
>                   the process's kernel stack and thread_info structure
>                   and deallocate the slab cache containing the
>                   task_struct.
>   14. P.28 The Dilemma of the Parentless Task
>          1. http://www.qnx.com/developers/docs/qnx_4.25_docs/qnx4/sysarch/proc.html
>
>          2. The solution, hinted upon previously, is to reparent a
>             task's children on exit to either another process in the
>             current thread group or, if that fails, the init process.
>
>    1. Commands for Process
>       Command  Description
>       ps  report a snapshot of the current processes.
>       top  display Linux tasks
>       nice  run a program with modified scheduling priority
>       renice  alter priority of running processes
>       kill  
>       send a signal to a process Name  Num  Action  Description
>       0  0  n/a  exit code indicates if a signal may be sent
>       ALRM  14  exit  
>       HUP  1  exit  
>       INT  2  exit  
>       KILL  9  exit  this signal may not be blocked
>       PIPE  13  exit  
>       POLL   exit  
>       PROF   exit  
>       TERM  15  exit  
>       USR1   exit  
>       USR2   exit  
>       VTALRM   exit  
>       STKFLT   exit  may not be implemented
>       PWR   ignore  may exit on some systems
>       WINCH   ignore  
>       CHLD   ignore  
>       URG   ignore  
>
>       killall  kill processes by name
>       skill, snice  send a signal or report process status
>       pstree  display a tree of processes
>       pgrep, pkill  look up or signal processes based on name and
>       other attributes
>       free  Display amount of free and used memory in the system
>       uptime  Tell how long the system has been running.
>       w  Show who is logged on and what they are doing.
>       pidof  find the process ID of a running program.
>       fuser  identify processes using files or sockets
>       sleep, usleep  delay for a specified amount of time
>       time  counting running time of a command
>       jobs  show stopped processes list
>       history  command history
>       nohup  run a command immune to hangups, with output to a non-tty
>       at, batch, atq, atrm  queue, examine or delete jobs for later
>       execution
>       crontab  maintain crontab files for individual users
>       bg, fg  let stopped process run on background, foreground
>       procinfo  display system status gathered from /proc
>       reboot, halt, shutdown, poweroff  stop the system
>
>    2. /proc
>          1. apm: advanced power manager
>             1.16 1.2 0x03 0x01 0xff 0x80 -1% -1 ?
>          2. bus: such pci, usb bus.
>          3. cmdline: boot parameter for kernel
>             root=/dev/hda4 vga=785 rootfstype=ext3
>          4. cpuinfo
>             attribute  value
>             processor  0
>             vendor_id  GenuineIntel
>             cpu family  6
>             model  7
>             model name  Pentium III (Katmai)
>             stepping  3
>             cpu MHz  451.028
>             cache size  512 KB
>             fdiv_bug  no
>             hlt_bug  no
>             f00f_bug  no
>             coma_bug  no
>             fpu  yes
>             fpu_exception  yes
>             cpuid level  2
>             wp  yes
>             flags  fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca
>             cmov pat pse36 mmx fxsr sse
>             bogomips  897.84
>
>          5. devices
>                 * Character devices:
>                   Major Number  Device Name
>                   1  mem
>                   2  pty
>                   3  ttyp
>                   4  ttyS
>                   5  cua
>                   7  vcs
>                   10  misc
>                   14  sound
>                   29  fb
>                   81  video_capture
>                   108  ppp
>                   128  ptm
>                   136  pts
>                   162  raw
>                   180  usb
>
>                 * Block devices:
>                   Major Number  Device Name
>                   2  fd
>                   3  ide0
>                   7  loop
>                   114  ataraid
>
>          6. dma
>          7. driver
>          8. execdomains
>          9. fb
>         10. filesystems
>             /dev node?  filesystem
>             nodev  rootfs
>             nodev  bdev
>             nodev  proc
>             nodev  sockfs
>             nodev  tmpfs
>             nodev  shm
>             nodev  pipefs
>              ext3
>              ext2
>             nodev  ramfs
>              iso9660
>             nodev  devpts
>             nodev  usbdevfs
>             nodev  usbfs
>             nodev  autofs
>             nodev  nfs
>              vfat
>              reiserfs
>
>         11. fs
>         12. ide
>         13. interrupts
>             IRQ  Number in CPU0  ???  Name
>             0  20684227  XT-PIC  timer
>             1  74567  XT-PIC  keyboard
>             2  0  XT-PIC  cascade
>             4  153184  XT-PIC  serial
>             8  4  XT-PIC  rtc
>             10  0  XT-PIC  usb-uhci, ESS Solo1
>             11  4078280  XT-PIC  eth0
>             12  301378  XT-PIC  PS/2 Mouse
>             14  1182857  XT-PIC  ide0
>             NMI  0   
>             ERR  0   
>
>         14. iomem
>             Address  Description
>             00000000-0009fbff  System RAM
>             0009fc00-0009ffff  reserved
>             000a0000-000bffff  Video RAM area
>             000c0000-000c7fff  Video ROM
>             000f0000-000fffff  System ROM
>             00100000-07ffcfff  System RAM
>             00100000-00245388  Kernel code
>             00245389-002d0c03  Kernel data
>             07ffd000-07ffefff  ACPI Tables
>             07fff000-07ffffff  ACPI Non-volatile Storage
>             e1000000-e100007f  3Com Corporation 3c905B 100BaseTX
>             [Cyclone]
>             e1800000-e3dfffff  PCI Bus #01
>             ?1800000-e1800fff  ATI Technologies Inc 3D Rage Pro AGP 1X/2X
>             ?2000000-e2ffffff  ATI Technologies Inc 3D Rage Pro AGP 1X/2X
>             ?2000000-e212bfff  vesafb
>             e3f00000-e3ffffff  PCI Bus #01
>             e4000000-e7ffffff  Intel Corp. 440BX/ZX/DX - 82443BX/ZX/DX
>             Host bridge
>             ffff0000-ffffffff  reserved
>
>         15. ioports
>             IO-Address  Description
>             0000-001f  dma1
>             0020-003f  pic1
>             0040-005f  timer
>             0060-006f  keyboard
>             0070-007f  rtc
>             0080-008f  dma page reg
>             00a0-00bf  pic2
>             00c0-00df  dma2
>             00f0-00ff  fpu
>             01f0-01f7  ide0
>             02f8-02ff  serial(set)
>             03c0-03df  vesafb
>             03f6-03f6  ide0
>             03f8-03ff  serial(set)
>             0cf8-0cff  PCI conf1
>             9400-947f  3Com Corporation 3c905B 100BaseTX [Cyclone]
>             9400-947f  00:0c.0
>             9800-9803  ESS Technology ES1969 Solo-1 Audiodrive
>             9800-9803  ESS Solo1
>             a000-a003  ESS Technology ES1969 Solo-1 Audiodrive
>             ?000-a003  ESS Solo1
>             a400-a40f  ESS Technology ES1969 Solo-1 Audiodrive
>             ?400-a40f  ESS Solo1
>             a800-a80f  ESS Technology ES1969 Solo-1 Audiodrive
>             ?804-a80f  ESS Solo1
>             b000-b03f  ESS Technology ES1969 Solo-1 Audiodrive
>             ?000-b00f  ESS Solo1
>             b400-b41f  Intel Corp. 82371AB/EB/MB PIIX4 USB
>             ?400-b41f  usb-uhci
>             b800-b80f  Intel Corp. 82371AB/EB/MB PIIX4 IDE
>             ?800-b807  ide0
>             ?808-b80f  ide1
>             d000-dfff  PCI Bus #01
>             ?800-d8ff  ATI Technologies Inc 3D Rage Pro AGP 1X/2X
>             e400-e43f  Intel Corp. 82371AB/EB/MB PIIX4 ACPI
>             e800-e81f  Intel Corp. 82371AB/EB/MB PIIX4 ACPI
>
>         16. irq
>         17. kcore
>         18. kmsg
>         19. ksyms : refer to System.map after build kernel source
>         20. loadavg : refer to "uptime" command
>         21. locks
>         22. meminfo : refer to "free", "top" command
>         23. misc
>         24. modules : modules in kernel : build in + insmod
>         25. mounts : refer to "mount", normally ln -s /proc/mounts
>             /etc/mtab
>         26. mtrr
>         27. net
>         28. partitions : refer to "fdisk -l"
>             major  minor  #blocks  name
>             3  0  19551168  hda
>             3  1  4192933  hda1
>             3  2  88357  hda2
>             3  3  249007  hda3
>             3  4  15020775  hda4
>
>         29. pci
>         30. scsi
>         31. self : "self" process, symbolic to /proc/#PID
>         32. slabinfo
>         33. stat : cpu, swap, interrupt, diskio, boot time
>         34. swaps :
>
>Filename   Type       Size    Used    Priority
>/dev/hda3  partition  248996  26540   -1
>         
>
>         35. sys
>         36. sysvipc : IPC
>         37. tty
>         38. uptime
>         39. version : refer to "uname -a" command
>         40. video
>

本文地址:http://com.8s8s.com/it/it23588.htm