Download presentation
Presentation is loading. Please wait.
Published byVerawati Sutedja Modified over 6 years ago
1
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
2
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 Quaù trình (process) moät chöông trình ñang thöïc thi Moät quaù trình ñöôïc ñònh nghóa bôûi Traïng thaùi CPU (trò cuûa caùc thanh ghi) Khoâng gian ñòa chæ (noäi dung boä nhôù) Moâi tröôøng (environment, xaùc ñònh thoâng qua caùc baûng cuûa heä ñieàu haønh)
3
Khaùi nieäm cô baûn (tt)
Traïng thaùi CPU Processor Status Word (PSW) Instruction Register (IR) Program Counter (PC) Stack Pointer (SP) Caùc general purpose register Khoâng gian ñòa chæ Text (code) Data Heap Stack Moâi tröôøng Vd: terminal ñang duøng, caùc open file, caùc keânh giao tieáp vôùi caùc quaù trình khaùc Ñöôïc lieät keâ trong caùc baûng cuûa heä ñieàu haønh
4
Process control block Heä ñieàu haønh löu thoâng tin veà quaù trình trong process control block (PCB) D.Feitelson
5
Caùc böôùc naïp chöông trình vaøo boä nhôù
6
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 Executable binary file (load module) Process image trong main memory start address program code program code data stack heap data
7
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 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,…)
8
Caùc traïng thaùi cuûa quaù trình (1/2)
Caùc traïng thaùi cô baûn cuûa moät quaù trình: 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
9
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 ready running dispatch interrupt I/O or event completion I/O or event wait new terminated waiting admit exit
10
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
11
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) Moâi tröôøng
12
Caùc tröôøng tieâu bieåu cuûa PCB
Tanenbaum
13
Chuyeån ngöõ caûnh (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ø tình traïng hoaït ñoäng cuûa quaù trình Trò cuûa caùc thanh ghi, trò cuûa program counter, boä nhôù,… Ngöõ caûnh cuûa quaù trình ñöôïc löu trong PCB cuûa noù Chuyeån ngöõ caûnh (context switch) laø coâng vieäc ngöng quaù trình ñang thöïc thi vaø chaïy moät 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
14
Chuyeån ngöõ caûnh (2/2)
15
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 Khi naøo choïn vaø choïn quaù trình naøo ñeå thöïc thi coù theå tuøy thuoäc vaøo tieâu chí 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ù Vaán ñeà deadlock, trì hoaõn voâ haïn ñònh,… Cung caáp cô cheá hoã trôï user taïo/keát thuùc quaù trình Cung caáp cô cheá ñoàng boä vaø giao tieáp giöõa caùc quaù trình
16
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
17
Ñò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 (ví duï): cöïc ñaïi hieäu suaát söû duïng cuûa CPU Time-sharing User töông taùc vôùi quaù trình 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)
18
Caùc haøng ñôïi ñònh thôøi
Job queue Ready queue Caùc device queue …
19
Caùc taùc vuï ñoái vôùi quaù trình (1/4)
Taïo quaù trình môùi Quaù trình coù theå taïo moät 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.
20
Caây quaù trình trong Linux/Unix
Ví duï root pagedaemon swapper init bash bash bash gcc ls mkdir grep
21
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: caùc 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 Cha vaø con khoâng chia seû taøi nguyeân Trình töï thöïc thi: hai khaû naêng Quaù trình cha vaø con thöïc thi ñoàng thôøi (concurrently) Quaù trình cha chaïy khi quaù trình con keát thuùc Trong Unix, quaù trình goïi fork seõ ñöôïc taïo moät quaù trình con hoaøn toaøn gioáng noù vaøo thôøi ñieåm goïi --- cuøng traïng thaùi CPU, khoâng gian ñòa chæ, moâi tröôøng chæ khaùc nhau ôû process ID vaø trò traû veà töø fork In Unix, new processes are not created from scratch. Rather, any process can create a new process by duplicating itself. This is done by calling the fork system call. The new process will be identical to its parent process: it has the same data, executes the same program, and in fact is at exactly the same place in the execution. The only differences are their process IDs and the return value from the fork.
22
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æ: tuøy heä ñieàu haønh UNIX: Khoâng gian ñòa chæ cuûa quaù trình con ñöôïc nhaân baûn töø khoâng gian ñòa chæ cuûa cha vaøo thôøi ñieåm goïi Windows: phöùc taïp hôn, Win32 API CreateProcess() caàn hôn 10 tham soá
23
Veà quan heä cha/con Ví duï aùp duïng fork trong UNIX/Linux
Quaù trình goïi fork() ñeå taïo moät quaù trình con Quaù trình 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… hay goïi wait() ñeå ñôïi con xong ñoàng boä
24
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); exit(1); else { printf(“Fork error\n”); exit(1);}
25
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,…)
26
Coäng taùc giöõa caùc quaù trình
Caùc quaù trình coù theå coäng taùc (cooperate) ñeå hoaøn thaønh coâng vieäc Vd Sensor data buffer Sensor Sampler Process data Shared memory Display
27
Coäng taùc giöõa caùc quaù trình
Thieát keá öùng duïng Phaân chia moät öùng duïng lôùn thaønh caùc process coäng taùc nhau kieán truùc client-server AÙp duïng coäng taùc giöõa caùc quaù trình ñeå Baøi toaùn producer-consumer Modul hoùa 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 ñoøi hoûi 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
28
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 döõ lieäu ñöôïc 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 döõ lieäu khi buffer ñaày
29
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 coù theå treân cuøng maùy hoaëc khaùc maùy Hai kyõ thuaät IPC Truyeàn thoâng ñieäp (message passing) Duøng boä nhôù chia seû (shared memory)
30
Moâ hình giao tieáp Truyền thông điệp Dùng bộ nhớ chia sẻ 3.4 Fig 3.12
31
Truyeàn thoâng ñieäp 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/nonblocking send blocking/nonblocking receive
32
Example of shared memory for IPC
POSIX Shared Memory Process first creates shared memory segment segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR); Process wanting access to that shared memory must attach to it shared_memory = (char *) shmat(id, NULL, 0); Now the process could write to the shared memory sprintf(shared_memory, "Writing to shared memory"); When done a process can detach the shared memory from its address space shmdt(shared_memory);
33
Giao tieáp trong heä thoáng client-server
Socket Remote Procedure Call (RPC) Remote Method Invocation (RMI)
34
Socket Socket laø moät ñoái töôïng tröøu töôïng duøng ñeå töôïng tröng moät ñaàu cuoái cuûa moät keânh giao tieáp Goàm ñòa chæ IP vaø port number Vd socket :1625 duøng ñeå tham chieáu port 1625 treân maùy coù ñòa chæ IP
35
Socket (tt) ‘well-known’ port xaùc ñònh caùc dòch vuï chuaån
Cung caá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
36
Göûi/nhaän qua socket Haøm thö vieän Dieãn giaûi socket()
Taïo moät socket bind() Gaùn moät ñòa chæ cuïc boä vaøo socket listen() Saün saøng ñeå chaáp nhaän keát noái 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
37
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)
38
Connection-Oriented Transport Service
Server (Google) send(socket, buffer, buffer_length, flags) recv(socket, buffer, buffer_length, flags) socket() bind() listen() accept() recv() send() close() communication socket() connect() send() recv() close() Client (Firefox) 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.
39
Remote procedure call (RPC)
Môû roäng cô cheá goïi thuû tuïc (procedure call) thoâng thöôøng Duøng RPC moät quaù trình coù theå 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áû Bieåu dieãn döõ lieäu Keát noái töø client ñeán server (binding) Giao thöùc vaän chuyeån Xöû lyù loãi (exception handling) An toaøn (security) 3.6.2 of textbook Overview of Remote Procedure Calls (RPC), slides, Douglas C. Schmidt
40
Hieän thöïc RPC: caùc stub
Client Server g D.Feitelson
41
Hieän thöïc RPC: löu ñoà thôøi gian
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) Thời gian
42
Marshalling / Unmarshalling
Marshalling laø coâng vieäc ñoùng goùi döõ lieäu vaøo trong moät thoâng ñieäp döôùi moät ñònh daïng thích hôïp ñeå vaän chuyeån qua maïng Unmarshalling laø coâng vieäc ngöôïc laïi Duøng marshalling / unmarshalling trong hieän thöïc RPC trong caùc coâng ñoaïn Göûi yeâu caàu ñeán server Client stub ñoùng goùi caùc tham soá cuûa thuû tuïc vaøo trong goùi thoâng ñieäp vaø göûi noù ñeán server stub Server stub môû goùi laáy caùc tham soá töø thoâng ñieäp vaø duøng chuùng ñeå goïi thuû tuïc cuûa server Server traû veà keát quaû Server stub ñoùng goùi keát quaû tröôùc khi göûi ñeán client stub Client stub môû goùi laáy keát quaû vaø chuyeån noù ñeán client
43
Marshalling / Unmarshalling
Client Server return reply call request call request return reply Client Stub Server Stub message to result parameters to message message to parameters result to message Transport (OS 1) Transport (OS 2) receive send receive send network
44
Hieän thöïc RPC: bieåu dieãn döõ lieäu
Döõ lieäu treân caùc heä thoáng khaùc nhau coù theå ñöôïc bieåu dieãn khaùc nhau Maõ hoùa kyù töï: ASCII, EBCDIC Ví duï bieåu dieãn 32-bit integer trong boä nhôù Motorola: big-endian most significant byte taïi high memory address () Intel x86: little-endian least significant byte taïi high memory address () Giaûi phaùp: maõ hoùa döõ lieäu duøng daïng bieåu dieãn ñoäc laäp maùy XDR (External Data Representation) khi trao ñoåi döõ lieäu giöõa caùc heä thoáng maùy khaùc nhau
45
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
46
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.