2.A Quaûn lyù quaù trình Khaùi nieäm cô baûn Ñònh thôøi CPU

Slides:



Advertisements
Similar presentations
Chapter 3: Processes.
Advertisements

Chapter 3 Processes.
Chapter 3: Processes. Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Interprocess Communication. Process Concepts Last class.
Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Inter-process Communication Communication in Client-Server.
Operating Systems Part III: Process Management (Process States and Transitions)
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 3 Operating Systems.
Chapter 3: Process-Concept. Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
3.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Overview: Process Concept Process Scheduling Operations on Processes.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Jan 19, 2005 Chapter 3: Processes Process Concept.
Chapter 3: Processes. 2 Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server.
 Process Concept  Process Scheduling  Operations on Processes  Cooperating Processes  Interprocess Communication  Communication in Client-Server.
4.1 Operating System Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
CÁC QUY TRÌNH CỦA CHUỖI CUNG ỨNG Hoạch định và thu mua
Chapter 3: Processes.
Lecture 3 Process.
Chapter 3: Process Concept
Chapter 3:Processes Book: Operating System Principles , 9th Edition , Abraham Silberschatz, Peter Baer Galvin, Greg Gagne.
Chapter 4: Processes Process Concept Process Scheduling
Processes Overview: Process Concept Process Scheduling
Chapter 3: Process Concept
ÖÙNG SUAÁT TRONG NEÀN ÑAÁT
SỐT XUẤT HUYẾT DENGUE ThS.BS. Dương Hồng phúc Bộ môn Vi sinh- Trường Đại học Y Dược – Cần Thơ.
Chapter 3: Processes.
Mieãn dòch treân caù Da, vaåy, mang, tuyeán nhôùt
PHÖÔNG TIEÄN THANH TOAÙN QUOÁC TEÁ
M A R K E T I N G Th.S Nguyễn Ngọc Hạnh 1.
TÌM HIỂU VỀ WEB SERVICES VÀ XÂY DỰNG MỘT WEB SERVICE
ĐỊNH THỜI CPU.
Hệ Điều Hành (Operating Systems)
Chapter 3: Processes.
KIỂM ĐỊNH GIẢ THUYẾT THỐNG KÊ
CHƯƠNG 2 TRUY VẤN DỮ LIỆU.
Chapter 4: Processes Process Concept Process Scheduling
Lecture 2: Processes Part 1
CÔNG DÂN VỚI TÌNH YÊU HÔN NHÂN VÀ GIA ĐÌNH
ThS Nguyễn Thị Ngọc Hương
Tổng Quan Về Lập Trình Hướng Đối Tượng
Chöông 10: Heä Thoáng File
SINH THÁI CÁ.
Chapter 3: Processes.
PHÂN TÍCH PHÂN TỬ TRONG CHẨN ĐÓAN LÂM SÀNG
THÁI ĐỘ XỬ TRÍ BƯỚU GIÁP NHÂN
Moân hoïc Heä Ñieàu Haønh
Caùc pheùp chieáu TINH THEÅ
Chapter 3: Processes.
GVHD: GS.TS Lê Khắc Bình HVTH: Nguyễn Thanh Lâm
CHÀO MỪNG QUÝ THẦY, CÔ GIÁO ĐẾN DỰ GIỜ MÔN CÔNG NGHỆ LỚP 8A
CHƯƠNG III MÔI TRƯỜNG KINH TẾ.
Chapter 3: Processes.
Trường CĐ CNTT HN Việt Hàn
Moân hoïc Heä Ñieàu Haønh
May be not.... Con kieán Moät truyeän nguï ngoân... Hay laø
Kyõ naêng Laäp Keá hoaïch
Chapter 3: Processes.
CAÂU HOÛI OÂN THI 1.Röøng ngaäp maën Vieät Nam ñöôïc phaân chia thaønh maáy vuøng? Ñaëc ñieåm moâi tröôøng cuûa caùc vuøng röøng ngaäp maën ôû Vieät Nam?
Processes in Unix and Windows
Chapter 3: Processes.
Chapter 3: Processes.
Processes August 10, 2019 OS:Processes.
Presentation transcript:

2.A Quaûn lyù quaù trình Khaùi nieäm cô baûn Ñònh thôøi CPU Caùc taùc vuï cô baûn: taïo/keát thuùc quaù trình Söï coäng taùc giöõa caùc quaù trình Giao tieáp giöõa caùc quaù trình

Khaùi nieäm cô baûn Heä thoáng maùy tính thöïc thi nhieàu chöông trình khaùc nhau Batch system: job Time-shared system: user program, task Job  process Quaù trình (process) moät chöông trình ñang thöïc thi Moät quaù trình bao goàm Text (program code), data (chöùa caùc global variable), heap, stack Hoaït ñoäng hieän thôøi: program counter (PC), caùc register Caùc file ñang duøng

Caùc böôùc naïp chöông trình vaøo boä nhôù

Töø chöông trình ñeán quaù trình Chöông trình thöïc thi coù ñònh daïng load module maø trình naïp (loader) “hieåu” ñöôïc Vd ñònh daïng elf trong Linux Layout luaän lyù cuûa process image program code data Executable binary file (load module) stack Process image trong main memory start address

Khôûi taïo quaù trình Caùc böôùc heä ñieàu haønh khôûi taïo quaù trình Caáp phaùt moät ñònh danh duy nhaát (process number hay process identifier, pid) cho quaù trình Caáp phaùt khoâng gian nhôù ñeå naïp quaù trình Khôûi taïo khoái döõ lieäu Process Control Block (PCB) cho quaù trình PCB laø nôi heä ñieàu haønh löu caùc thoâng tin veà quaù trình Thieát laäp caùc moái lieân heä caàn thieát (vd: saép PCB vaøo haøng ñôïi ñònh thôøi,…)

Caùc traïng thaùi cuûa quaù trình (1/2) new: quaù trình vöøa ñöôïc taïo ready: quaù trình ñaõ coù ñuû taøi nguyeân, chæ coøn caàn CPU running: caùc leänh cuûa quaù trình ñang ñöôïc thöïc thi waiting: hay laø blocked, quaù trình ñôïi I/O hoaøn taát, hay ñôïi tín hieäu terminated: quaù trình ñaõ keát thuùc

Caùc traïng thaùi cuûa quaù trình (2/2) Chuyeån ñoåi giöõa caùc traïng thaùi cuûa quaù trình terminated new admit dispatch exit ready running interrupt I/O or event completion I/O or event wait waiting

Ví duï veà traïng thaùi quaù trình /* test.c */ int main(int argc, char** argv) { printf(“Hello world\n"); exit(0); } Bieân dòch chöông trình trong Linux gcc test.c –o test Thöïc thi chöông trình test ./test Trong heä thoáng seõ coù moät quaù trình test ñöôïc taïo ra, thöïc thi vaø keát thuùc. Chuoãi traïng thaùi cuûa quaù trình test nhö sau (tröôøng hôïp toát nhaát): new ready running waiting (do chôø I/O khi goïi printf) terminated

Process Control Block Ñaõ thaáy laø moãi quaù trình trong heä thoáng ñeàu ñöôïc caáp phaùt moät Process Control Block (PCB) PCB laø moät trong caùc caáu truùc döõ lieäu quan troïng nhaát cuûa heä ñieàu haønh Ví duï layout cuûa moät PCB: (tröôøng pointer duøng ñeå lieân keát caùc PCB thaønh moät linked list)

Caùc tröôøng tieâu bieåu cuûa PCB From Tanenbaum

Chuyeån ngöõ caûnh (context switch) (1/2) Laøm theá naøo ñeå chia seû CPU giöõa caùc quaù trình? Ngöõ caûnh (context) cuûa moät quaù trình laø “traïng thaùi” cuûa quaù trình Trò cuûa caùc thanh ghi, trò cuûa program counter,… Ngöõ caûnh cuûa quaù trình coù theå ñöôïc löu trong PCB cuûa noù Chuyeån ngöõ caûnh laø coâng vieäc giao CPU cho quaù trình khaùc. Khi ñoù caàn: löu ngöõ caûnh cuûa quaù trình vaøo PCB cuûa noù naïp ngöõ caûnh töø PCB cuûa quaù trình môùi ñeå quaù trình môùi thöïc thi

Chuyeån ngöõ caûnh (2/2)

Yeâu caàu ñoái vôùi heä ñieàu haønh veà quaûn lyù quaù trình Hoã trôï söï thöïc thi luaân phieân giöõa nhieàu quaù trình Hieäu suaát söû duïng CPU Thôøi gian ñaùp öùng Phaân phoái taøi nguyeân heä thoáng hôïp lyù traùnh deadlock, trì hoaõn voâ haïn ñònh,… Cung caáp cô cheá giao tieáp vaø ñoàng boä hoaït ñoäng caùc quaù trình Cung caáp cô cheá hoã trôï user taïo/keát thuùc quaù trình

Quaûn lyù quaù trình: caùc haøng ñôïi Ví duï caùc PCB running process number 7 ready 11 4 2 17 waiting 19 11 Coù gì sai trong ví duï?

Ñònh thôøi quaù trình Taïi sao phaûi ñònh thôøi? Multiprogramming Coù nhieàu quaù trình thöïc thi luaân phieân nhau Muïc tieâu: cöïc ñaïi hieäu suaát söû duïng cuûa CPU Time-sharing Cho pheùp user töông taùc vôùi quaù trình ñang thöïc thi Muïc tieâu: toái thieåu thôøi gian ñaùp öùng Moät soá khaùi nieäm cô baûn Caùc boä ñònh thôøi (scheduler) Caùc haøng ñôïi ñònh thôøi (scheduling queue)

Caùc haøng ñôïi ñònh thôøi Job queue Ready queue Caùc device queue …

Theâm medium-term scheduling Ñoâi khi heä ñieàu haønh (nhö time-sharing system) coù theâm medium-term scheduling ñeå ñieàu chænh möùc ñoä multiprogramming cuûa heä thoáng Medium-term scheduler chuyeån quaù trình töø boä nhôù sang ñóa (swap out) chuyeån quaù trình töø ñóa vaøo boä nhôù (swap in) memory Silberschatz Tanenbaum, three-level scheduling short term scheduler, CPU scheduler = CPU scheduler medium term scheduler = memory scheduler long term scheduler, job scheduler = admission scheduler

Caùc taùc vuï ñoái vôùi quaù trình (1/4) Taïo quaù trình môùi Moät quaù trình coù theå taïo quaù trình môùi thoâng qua moät system call (vd: haøm fork trong Unix) Ví duï: (Unix) Khi user ñaêng nhaäp heä thoáng, moät command interpreter (shell) seõ ñöôïc taïo ra cho user Quaù trình ñöôïc taïo laø quaù trình con cuûa quaù trình taïo (quaù trình cha). Quan heä cha-con ñònh nghóa moät caây quaù trình.

Caây quaù trình trong Linux/Unix Ví duï root pagedaemon swapper init bash bash bash gcc ls mkdir grep

Caùc taùc vuï ñoái vôùi quaù trình (2/4) Taïo quaù trình môùi (tt) Chia seû taøi nguyeân cuûa quaù trình cha: hai khaû naêng Quaù trình cha vaø con chia seû moïi taøi nguyeân Quaù trình con chia seû moät phaàn taøi nguyeân cuûa cha Trình töï thöïc thi: hai khaû naêng Quaù trình cha vaø con thöïc thi ñoàng thôøi (concurrently) Heä ñieàu haønh chæ cho quaù trình cha chaïy khi quaù trình con keát thuùc.

Caùc taùc vuï ñoái vôùi quaù trình (3/4) Taïo quaù trình môùi (tt) Khoâng gian ñòa chæ: hai khaû naêng UNIX: Khoâng gian ñòa chæ cuûa quaù trình con ñöôïc nhaân baûn töø cha (con coù cuøng code vaø data nhö cha) Windows: phöùc taïp hôn, Win32 API CreateProcess() caàn hôn 10 tham soá

Veà quan heä cha/con Ví duï trong UNIX/Linux Quaù trình goïi fork() ñeå taïo moät quaù trình môùi Quaù trình môùi (con) goïi exec() ñeå naïp vaø thöïc thi moät chöông trình trong khoâng gian nhôù cuûa noù Quaù trình cha laøm vieäc khaùc… nhö goïi wait() ñeå ñôïi con xong ñoàng boä

Ví duï taïo process vôùi fork() #include <stdio.h> #include <unistd.h> int main (int argc, char *argv[]){ int return_code; /* create a new process */ return_code = fork(); if (return_code > 0){ printf(“This is parent process”); wait(NULL); exit(0); } else if (return_code == 0) { printf(“This is child process”); execlp(“/bin/ls”, “ls”, NULL); else { printf(“Fork error\n”); exit(-1);

Caùc taùc vuï ñoái vôùi quaù trình (4/4) Taïo quaù trình môùi Keát thuùc quaù trình Quaù trình töï keát thuùc Quaù trình keát thuùc khi thöïc thi leänh cuoái vaø goïi system routine exit Quaù trình keát thuùc do quaù trình khaùc (coù ñuû quyeàn, vd: quaù trình cha cuûa noù) Goïi system routine abort vôùi tham soá laø pid (process identifier) cuûa quaù trình caàn ñöôïc keát thuùc Heä ñieàu haønh thu hoài taát caû caùc taøi nguyeân cuûa quaù trình keát thuùc (vuøng nhôù, I/O buffer,…)

Coäng taùc giöõa caùc quaù trình Trong quaù trình thöïc thi, caùc quaù trình coù theå coäng taùc (cooperate) ñeå hoaøn thaønh coâng vieäc Module hoùa Xaây döïng moät phaàn meàm phöùc taïp baèng caùch chia thaønh caùc module/process hôïp taùc nhau Caùc quaù trình coäng taùc ñeå Chia seû thoâng tin Taêng toác tính toaùn Neáu heä thoáng coù nhieàu CPU, chia coâng vieäc tính toaùn thaønh nhieàu coâng vieäc tính toaùn nhoû chaïy song song Söï coäng taùc giöõa caùc quaù trình yeâu caàu heä ñieàu haønh cung caáp giaûi phaùp ñoàng boä hoaït ñoäng (chöông 3) vaø giao tieáp cho caùc quaù trình

Baøi toaùn producer-consumer Baøi toaùn tieâu bieåu veà söï coäng taùc giöõa caùc quaù trình: baøi toaùn producer-consumer Producer taïo ra caùc döõ lieäu vaø consumer tieâu thuï, söû duïng caùc döõ lieäu ñoù. Söï trao ñoåi thoâng tin thöïc hieän qua buffer unbounded buffer: kích thöôùc buffer voâ haïn (khoâng thöïc teá). bounded buffer: kích thöôùc buffer coù haïn. Producer vaø consumer phaûi hoaït ñoäng ñoàng boä vì Consumer khoâng ñöôïc tieâu thuï khi producer chöa saûn xuaát Producer khoâng ñöôïc taïo theâm saûn phaåm khi buffer ñaày.

Interprocess communication (IPC) IPC laø caùc kyõ thuaät cung caáp bôûi heä ñieàu haønh nhaèm giuùp caùc quaù trình giao tieáp vôùi nhau. Caùc quaù trình treân cuøng maùy hoaëc khaùc maùy Hai kyõ thuaät IPC Message passing Duøng shared memory

Message passing system Laøm theá naøo ñeå caùc quaù trình giao tieáp nhau? Caùc vaán ñeà: Naming Giao tieáp tröïc tieáp send(P, msg): göûi thoâng ñieäp ñeán quaù trình P receive(Q, msg): nhaän thoâng ñieäp ñeán töø quaù trình Q Giao tieáp giaùn tieáp: thoâng qua mailbox hay port send(A, msg): göûi thoâng ñieäp ñeán mailbox A receive(B, msg): nhaän thoâng ñieäp töø mailbox B Synchronization: blocking send, nonblocking send, blocking receive, nonblocking receive Buffering: duøng queue ñeå taïm chöùa caùc message Zero capacity (no buffering) Bounded capacity: ñoä daøi cuûa queue laø giôùi haïn Unbounded capacity: ñoä daøi cuûa queue laø khoâng giôùi haïn

Giao tieáp trong heä thoáng client-server Socket Remote Procedure Calls (RPC) Remote Method Invocation (RMI)

Socket Socket Ñaàu cuoái cuûa moät keânh giao tieáp Cô cheá giao tieáp möùc thaáp: göûi nhaän moät chuoãi byte döõ lieäu khoâng caáu truùc Hai loaïi giao tieáp qua socket: connectionless vaø connection-oriented Laäp trình socket Berkeley socket (BSD socket), WinSock

Göûi/nhaän qua socket Haøm thö vieän Dieãn giaûi socket() Taïo moät socket bind() Gaén ñòa chæ cuïc boä vaøo moät socket listen() Thieát laäp ñoä daøi queue accept() (server) Chôø keát noái ñeán töø client connect() (client) keát noái ñeán moät server send() sendto() Göûi döõ lieäu qua keânh giao tieáp ñaõ thieát laäp Göûi döõ lieäu ñeán moät ñòa chæ recv() recvfrom() Nhaän döõ lieäu qua keânh giao tieáp ñaõ thieát laäp Nhaän döõ lieäu ñeán töø moät ñòa chæ close() Ñoùng keát noái

Duøng Connectionless Transport Service socket() bind() Process A sendto()/ recvfrom() Process B close() sendto(socket, buffer, buffer_length, flags, destination_address, addr_len) recvfrom(socket, buffer, buffer_length, flags, from_address, addr_len)

Duøng Connection-oriented Transport Service send(socket, buffer, buffer_length, flags) recv(socket, buffer, buffer_length, flags) socket() bind() listen() accept() recv() send() close() Server connect() Client communication The way to set up a connection is asymmetric, and operates as follows. The first process, which is called the server, first creates a socket. This means that the operating system allocates a data structure to keep all the information about this communication channel, and gives the process a file descriptor to serve as a handle to it. The server then binds this socket to a port number. In effect, this gives the socket a name that can be used by clients: the machine’s IP (internet) address together with the port are used to identify this socket (you can think of the IP address as a street address, and the port number as a door or suite number at that address). Common services have predefined port numbers that are well-known to all. For other distributed applications, the port number is typically selected by the programmer. To complete the setup, the server then listens to this socket. This notifies the system that communication requests are expected. The other process, called the client, also creates a socket. It then connects this socket to the server’s socket (by giving the server’s IP address and port). This means that the servers address and port are listed in the local socket data structure, and that a message regarding the communication request is sent to the server. The system on the server side finds the server’s socket by searching according to the port number. To actually establish a connection, the server has to accept it. This creates a new socket that is accessed via a new file descriptor. This new socket (on the server’s side) and the client’s socket are now connected, and data can be read and written to them in both directions. The asymmetry of setting up the connection is forgotten, and both processes now have equal standing. However, the original socket created by the server still exists, and it may accept additional connections from other clients.

Remote procedure call Remote procedure call (RPC) Cho pheùp moät chöông trình goïi moät thuû tuïc naèm treân maùy tính ôû xa qua maïng. Caùc vaán ñeà khi hieän thöïc RPC Truyeàn tham soá vaø keát quaû traû veà cuûa lôøi goïi thuû tuïc Chuyeån ñoåi döõ lieäu khi truyeàn treân maïng (data conversion) Keát noái client ñeán server Bieân dòch chöông trình Kieåm soaùt loãi Baûo maät

Remote Procedure Call RPC, cho pheùp moät quaù trình goïi moät thuû tuïc coù theå ôû treân moät maùy khaùc. g D.Feitelson

Sô ñoà hoaït ñoäng cuûa RPC chôø keát quaû traû veà (suspended) Client Goïi remote procedure Keát quaû traû veà Request Reply Server (blocked) Goïi thuû tuïc cuïc boä vaø traû veà keát quaû (blocked) t

Löu ñoà thöïc hieän RPC (OS 1) (OS 2) Client Server return reply call request call request return reply Client Stub Server Stub message to parameter parameter to message message to parameter parameter to message TRANSPORT (OS 1) TRANSPORT (OS 2) receive send receive send network

Truyeàn tham soá trong RPC (1/2) Marshalling qui taéc truyeàn tham soá vaø chuyeån ñoåi döõ lieäu trong RPC bao goàm caû ñoùng goùi döõ lieäu thaønh daïng thöùc coù theå truyeàn qua maïng maùy tính. Bieåu dieãn döõ lieäu vaø kieåm tra kieåu döõ lieäu Döõ lieäu ñöôïc bieåu dieãn khaùc nhau treân caùc heä thoáng khaùc nhau ASCII, EBCDIC Ví duï bieåu dieãn 32-bit integer trong maùy: big-endian  most significant byte taïi high memory address (Motorola) little-endian  least significant byte taïi high memory address (Intel x86) Daïng bieåu dieãn XDR (External Data Representation): bieåu dieãn döõ lieäu machine-independent

Truyeàn tham soá trong RPC (2/2)

Thöïc hieän RPC vôùi dynamic binding Duøng dynamic binding ñeå xaùc ñònh port number cuûa RPC X

Remote method invocation Remote Method Invocation (RMI) Cho pheùp moät chöông trình Java coù theå goïi moät phöông thöùc (method) cuûa moät ñoái töôïng ôû xa, nghóa laø moät ñoái töôïng ôû taïi moät maùy aûo Java khaùc

Marshalling tham soá trong RMI unmarshalling Phöông thöùc ñöôïc trieäu goïi coù daïng sau: boolean someMethod(Object x, Object y)