intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Hệ điều hành Unix: Chương 8 - Ngô Duy Hòa

Chia sẻ: Năm Tháng Tĩnh Lặng | Ngày: | Loại File: PDF | Số trang:13

77
lượt xem
7
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Chương 8 của bài giảng Hệ điều hành Unix trình bày về tiến trình - Process. Các nội dung chính cần tìm hiểu trong chương này gồm: Các khái niệm cơ bản, system call interface, truy cập process từ file system.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Hệ điều hành Unix: Chương 8 - Ngô Duy Hòa

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

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2