Hệ điều hành - Bài 8: Tiến trình - Process

 Các khái niệm cơbản.

• System Call Interface

• /proc file system

• Interprocesses Communication

pdf13 trang | Chia sẻ: Mr Hưng | Lượt xem: 1601 | Lượt tải: 0download
Nội dung tài liệu Hệ điều hành - Bài 8: Tiến trình - Process, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
1Bài 8. Tiến trình - Process Ngô Duy Hoa – KHMT - CNTT Nội dung bài học • Các khái niệm cơ bản. • System Call Interface • /proc file system • Interprocesses Communication 1. Các khái niệm cơ bản Program - Process Program - Process • Khi chương trình được kích hoạt: – OS cung cấp 1 framework để chạy chương trình (virtual memory framework). – Nạp code, data và các thư viện cần thiết cho chương trình vào các vùng địa chỉ xác định trong framework. – Thông tin của tiến trình được lưu trữ trong khối quản lý tiến trình (PCB – process control block) nằm ở kernel area. Program - Process 2Process Life Cycle Process Control Block • OS quản lý process thông qua các khối PCB • PCB chứa các thông tin: – Trạng thái tiến trình. – Định danh tiến trình. – Thông tin về người dùng. – Các thanh ghi hệ thống. – Danh sách các đơn vị bộ nhớ sử dụng. – Các file đang làm việc. – Process Table • OS dùng 1 bảng quản lý tiến trình (Process Table) chung cho tất cả tiến trình. • Mỗi 1 phần tử trong bảng là một con trỏ tương ứng với khối quản lý tiến trình PCB. – Khi tiến trình bắt đầu được tạo Æ PCB được đưa vào PT. – Khi tiến trình kết thúc Æ các tài nguyên tiến trình được giải phóng và PCB bị xóa khỏi PT. Process Control Blocks (PCBs)/Process Descriptors Running double list prio_array: priority array of running processes 3parent & childrens process call ptrace() vm_next vm_next Process’s Virtual Memory task_struct mm_struct pgdmm mmap vm_area_struct vm_end vm_prot vm_start vm_end vm_prot vm_start vm_end vm_prot vm_next vm_start process virtual memory text data shared libraries 0 0x08048000 0x0804a020 0x40000000 – vm_prot: • read/write permissions for this area – vm_flags • shared with other processes or private to this process vm_flags vm_flags vm_flags Process’s file fs: creating files files: reading file Process’s Shell Commands • jobs: Liệt kê các công việc đang thực hiện làm việc ở chế độ nền (background) và trả lại thứ tự các công việc trong danh sách. • Trong ví dụ trên: – [1] là số thứ tự công việc đang thực hiện. – 11494 : PID của tiến trình. ƒ Tạm ngưng một công việc (tương đương với Ctrl+Z).suspend ƒ Hủy một công việc hay tiến trình theo chỉ số công việc hoặc PID tiến trình. ƒ Cú pháp: # kill %N hoặc kill PID với N là chỉ số công việc. kill ƒ Lệnh này sẽ ngưng công việc (script) đang chạy cho đến khi tất cả các công việc trong chế độ nền đều kết thúc. wait ƒ Lệnh fg chuyển một công việc từ chế độ nền sang chế độ tích cực (foreground). Lệnh bg chuyển từ foreground về background. ƒ Các lệnh này chỉ làm việc với số thứ tự công việc (job number). Nếu không có tham số nào thì các lệnh này tác động vào công việc hiện tại. fg, bg Một số câu lệnh hữu ích khác • Liệt kê các process bằng $ ps [option]: – $ps –ef : liệt kê tất cả các process đang chạy theo format đầy đủ. – $ ps –af: chỉ liệt kê các process liên quan đến terminal đang làm việc. – $ ps –eo pid, ppid, stat,..: hiển thị theo format của người dùng • Hiển thị theo cây: $ pstree • Hiển thị thông tin hệ thống: – $ top : hiển thị thông tin hệ thống ở chế độ real-time. – $ top –p PID: hiển thị thông tin cụ thể của tiến trình. 42. Process’s System Call 2.1 Process creation Unix/Linux Process Tree Process Creation • Các tiến trình trong Unix/Linux là các nút trong 1 cây tiến trình. – Tiến trình được sinh ra từ tiến trình khác được gọi là con (child) – Tiến trình tạo ra tiến trình khác được gọi là cha (parent). • Unix/Linux hỗ trợ 2 hàm system call: – Tạo 1 tiến trình mới: fork() – Thay code thực hiện trong tiến trình: exec() fork() system call Thay code thực hiện: exec() • System call exec(new_prog) cho phép: – Định danh, vị trí của tiến trình trong cây tiến trình không thay đổi. – Nội dung các segments: code, data,stack, heap sẽ được thay đổi bởi chương trình mới. – Chạy tại điểm bắt đầu ( _main ) của chương trình mới. 5wait() & exit() system call • Hàm exit(exit_code) dùng để kết thúc tiến trình và trả lại giá trị là exit_code • Hàm wait() dùng trong trường hợp: – Tạm ngưng tiến trình hiện tại, đợi tiến trình con kết thúc. – Xóa PCB tiến trình con đó khỏi PT. • Macro làm việc với stat_val: WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, WSTOPSIG. Các hàm liên quan fork() & exec () family Các hàm liên quan (tiếp) fork() & exec()Æ system() Ví dụ: fork() & execvp() Ví dụ: fork() & execvp() (tiếp) Sử dụng system() • Hàm system() có thể viết từ fork() và exec() như sau: – B1. gọi hàm fork() để tạo framework process. – B2. gọi execvp() để nạp code vào tiến trình con. 6Sử dụng wait() Zombie process Orphan process 2.2 Process description Process ID & related Process ID & related 7User’s infomation Process’s enviroment Truy cập các biến môi trường thông qua environ Tự tạo các biến môi trường: Lấy thời gian thực hiện Kết quả chạy: Æ 8Process’s resource information Ví dụ lấy thông tin tài nguyên • minor fault: khi Process truy cập bộ nhớ trong RAM. • major fault:khi Process đọc dữ liệu từ đĩa cứng (code, data) • swap: số trang phải đọc từ HDD khi major fault xảy ra. Kết quả chạy chương trình 3. Truy cập Process từ FileSystem 9/proc file system • proc – virtual file system: – Không gắn với 1 block device thực sự nào. – Chạy ở chế độ real-time, nằm trong Main memory khi hệ thống khởi động. – Lưu trữ thông tin của các process đang chạy trong hệ thống. – Cho phép người dùng truy nhập tới để đọc các thông tin hệ thống. Files & Directory in /proc ƒ buddyinfo ƒ cmdline ƒ cpuinfo ƒ crypto ƒ devices ƒ diskstats ƒ dma ƒ execdomains ƒ fb ƒ filesystems ƒ interrupts ƒ iomem „ ioports „ kcore „ kmsg „ loadavg „ locks „ mdstat „ meminfo „ misc „ modules „ mounts „ mtrr „ partitions „ pci „ self „ slabinfo „ stat „ swaps „ sysrq-trigger „ uptime „ version „ vmstat + numerical named directory: mỗi 1 process ↔ 1 directory có tên là PID của process Details of some files in /proc • buddyinfo Contains the number of free areas of each order for the kernel buddy system • cmdline Kernel command line • cpuinfo Information about the processor(s).(Human readable) • devices List of device drivers configured into the currently running kernel (block and character). • dma Shows which DMA channels are being used at the moment. • execdomains Execdomains, related to security Details of some files in /proc • fb Frame Buffer devices. • filesystems Filesystems configured/supported into/by the kernel. • interrupts Number of interrupts per IRQ on the x86 architecture. • iomem This file shows the current map of the system's memory for its various devices • ioports provides a list of currently registered port regions used for input or output communication with a device /proc/kcore • This file represents the physical memory of the system and is stored in the core file format. • Unlike most /proc files, kcore does display a size. This value is given in bytes and is equal to the size of physical memory (RAM) used plus 4KB. • Its contents are designed to be examined by a debugger, such as gdb, the GNU Debugger. • Only the root user has the rights to view this file. 10 Details of some files in /proc • kmsg Used to hold messages generated by the kernel. These messages are then picked up by other programs, such as klogd • loadavg – Provides a look at load average – The first three columns measure CPU utilization of the last 1, 5, and 10 minute periods. – The fourth column shows the number of currently running processes and the total number of processes. – The last column displays the last process ID used. • locks Displays the files currently locked by the kernel Details of some files in /proc • mdstat contains the current information for multiple-disk, RAID configurations • meminfo – One of the more commonly used /proc files – It reports back plenty of valuable information about the current utilization of RAM on the system • misc This file lists miscellaneous drivers registered on the miscellaneous major device, which is number 10 • modules Displays a list of all modules that have been loaded by the system • mounts This file provides a quick list of all mounts in use by the system Details of some files in /proc • mtrr This file refers to the current Memory Type Range Registers (MTRRs) in use with the system • partitions Very detailed information on the various partitions currently available to the system • pci Full listing of every PCI device on your system • slabinfo Information about memory usage on the slab level • stat Keeps track of a variety of different statistics about the system since it was last restarted Details of some files in /proc • swap Measures swap space and its utilization • uptime Contains information about how long the system has on since its last restart • version Tells the versions of the Linux kernel and gcc, as well as the version of Red Hat Linux installed on the system. Kernel Information • cmdline: hiển thị các tham số được truyền vào tiến trình đang chạy hiện tại (1) (1): KERNEL command line at system start up. đọc trong file: /boot/grub/grub.conf phần kernel = .. Liệt kê các filesystem được mount vào hệ thống Liệt kê các phân vùng đang làm việc trong hệ thống 11 System Information Continue Hardware Information address perms offset dev inode Path name 12 Process’s Information • mem: sử dụng để truy cập đến các trang bộ nhớ (/dev/mem) khi thực hiện các hàm open, read, lseek • root: liên kết mềm trỏ đến thư mục gốc. • stat: thông tin trạng thái tiến trình, được sử dụng trong lệnh ps Một số ví dụ • Sử dụng /proc/self để lấy thông tin về tiến trình đang chạy. – Bản chất: /proc/self là liên kết mềm đến thư mục của tiến trình đang làm việc được tạo trong /proc. Lấy tốc độ CPU Đọc file /proc/cpuinfo như file text bình thường 13 Kết quả Chương trình chính Liệt kê các tham số của tiến trình Xem giải thích hình sau! Kết quả chạy: Chú ý các giá trị kết thúc xâu: NUL = ‘\0’ Thank you!

Các file đính kèm theo tài liệu này:

  • pdfsv_bai_8_2734.pdf
Tài liệu liên quan