+ All Categories
Home > Documents > Chapter06 Deadlocks

Chapter06 Deadlocks

Date post: 07-Nov-2015
Category:
Upload: le-huu-tai
View: 291 times
Download: 0 times
Share this document with a friend
Description:
Tình trạng deadlock và giải thuật phát hiện deadlock
46
Khoa KTMT 1 Chöông 6 : Taéc ngheõn(Deadlock) Moâ hình heä thoáng Ñònh nghóa Ñieàu kieän caàn cuûa deadlock Resource Allocation Graph (RAG) Phöông phaùp giaûi quyeát deadlock Deadlock prevention Deadlock avoidance Deadlock detection Deadlock recovery Phöông phaùp keát hôïp ñeå giaûi quyeát Deadlock
Transcript
  • Khoa KTMT 1

    Chng 6 : Tac nghen(Deadlock)

    Mo hnh he thong

    nh ngha

    ieu kien can cua deadlock

    Resource Allocation Graph (RAG)

    Phng phap giai quyet deadlock

    Deadlock prevention

    Deadlock avoidance

    Deadlock detection

    Deadlock recovery

    Phng phap ket hp e giai quyet Deadlock

  • Khoa KTMT 2

    Van e deadlock trong he thong

    Tnh huong: mot tap cac process b blocked, moi process gi tai

    nguyen va ang ch tai nguyen ma process khac trong tap ang gi.

    V du 1

    Gia s he thong co 2 file tren a.

    P1 va P2 moi process ang m mot file va yeu cau m file kia.

    V du 2

    Semaphore A va B, khi tao bang 1

    P0 P1

    wait(A); wait(B);

    wait(B); wait(A);

  • Khoa KTMT 3

    Mo hnh hoa he thong

    He thong gom cac loai tai nguyen, k hieu R1, R

    2,, R

    m , bao gom:

    CPU cycle, khong gian bo nh, thiet b I/O, file, semaphore,

    Moi loai tai nguyen Rico W

    ithc the (instance).

    Gia s tai nguyen tai s dung theo ky (Serially Reusable

    Resources)

    Yeu cau (request): process phai ch neu yeu cau khong c ap ng ngay

    S dung (use): process s dung tai nguyen

    Hoan tra (release): process hoan tra tai nguyen

    Cac tac vu yeu cau (request) va hoan tra (release) eu la system

    call. V du

    request/release device

    open/close file

    allocate/free memory

    wait/signal

  • Khoa KTMT 4

    nh ngha

    Mot tien trnh goi la deadlocked neu no ang i mot

    s kien ma se khong bao gi xay ra.

    Thong thng, co nhieu hn mot tien trnh b lien quan trong

    mot deadlock.

    Mot tien trnh goi la tr hoan vo han nh (indefinitely

    postponed) neu no b tr hoan mot khoang thi gian dai

    lap i, lap lai trong khi he thong ap ng cho nhng

    tien trnh khac .

    i.e. Mot tien trnh san sang e x ly nhng no khong bao gi nhan c CPU.

  • Khoa KTMT 5

    ieu kien can e xay ra deadlock

    Bon ieu kien can (necessary condition) e xay ra

    deadlock

    1. Loai tr ho tng (Mutual exclusion): t nhat mot tai

    nguyen c gi theo nonsharable mode (v du: printer;

    v du sharable resource: read-only files).

    2. Gi va ch cap them tai nguyen (Hold and wait): mot

    process ang gi t nhat mot tai nguyen va i them tai

    nguyen do qua trnh khac ang gi.

  • Khoa KTMT 6

    ieu kien can e xay ra deadlock (tt)

    3. Khong trng dung (No preemption): (= no resource

    preemption) tai nguyen khong the b lay lai, ma ch co

    the c tra lai t process ang gi tai nguyen o khi

    no muon.

    4. Chu trnh i (Circular wait): ton tai mot tap {P0,,P

    n} cac

    qua trnh ang i sao cho

    P0

    i mot tai nguyen ma P1

    ang gi

    P1

    i mot tai nguyen ma P2

    ang gi

    Pn

    i mot tai nguyen ma P0

    ang gi

  • Khoa KTMT 7

    Resource Allocation Graph (tt)

    Ky hieu

    Process:

    Loai tai nguyen vi 4 thc the:

    Piyeu cau mot thc the cua R

    j:

    Piang gi mot thc the cua R

    j:

    Pi

    Pi

    Pi

    Rj

    Rj

    Rj

  • Khoa KTMT 8

    o th cap phat tai nguyen

    Resource Allocation Graph

    Resource allocation graph (RAG) la o th co hng, vi

    tap nh V va tap canh E

    Tap nh V gom 2 loai:

    P = {P1, P

    2,, P

    n } (Tat ca process trong he thong)

    R = {R1, R

    2,, R

    m } (Tat ca cac loai tai nguyen trong he thong)

    Tap canh E gom 2 loai:

    Canh yeu cau (Request edge): PiR

    j

    Canh cap phat (Assignment edge): RjP

    i

  • Khoa KTMT 9

    V du ve RAG

    R1 R3

    P1 P2 P3

    R2 R4

  • Khoa KTMT 10

    V du ve RAG (tt)

    R1 R3

    P1 P2 P3

    R2 R4

    Deadlock xay ra!

  • Khoa KTMT 11

    RAG va deadlock

    V du mot RAG cha chu trnh nhng khong xay ra

    deadlock: P4

    co the tra lai instance cua R2.

    R1

    P1

    P2

    P3R2

    P4

    L do v sao khng

    xy ra deadlock ?

  • Khoa KTMT 12

    V d

    R1 R3

    P1 P2 P3

    R2R4

    Deadlock ?

    s lng P, R?

    Xc nh Pi dang nm gi ti nguyn

    no? Yu cu ti nguyn no?

    Xt iu kin cn deadlock? K 1, 2, 4

  • Khoa KTMT 13

    RAG va deadlock (tt)

    RAG khong cha chu trnh (cycle) khong co deadlock

    RAG cha mot (hay nhieu) chu trnh

    Neu moi loai tai nguyen ch co mot thc the deadlock

    Neu moi loai tai nguyen co nhieu thc the co the xay ra deadlock

  • Khoa KTMT 14

    Cac phng phap giai quyet deadlock (1)

    Ba phng phap

    1) Bao am rang he thong khong ri vao tnh trang

    deadlock bang cach ngan (preventing) hoac tranh

    (avoiding) deadlock.

    Khac biet

    Ngan deadlock: khong cho phep (t nhat) mot trong 4 ieu kien can cho deadlock

    Tranh deadlock: cac qua trnh can cung cap thong tin ve tai nguyen no can e he thong cap phat tai nguyen mot

    cach thch hp

  • Khoa KTMT 15

    Cac phng phap giai quyet deadlock (2)

    2) Cho phep he thong vao trang thai deadlock,

    nhng sau o phat hien deadlock va phuc hoi he

    thong.

    3) Bo qua moi van e, xem nh deadlock khong

    bao gi xay ra trong he thong.

    Kha nhieu he ieu hanh s dung phng phap nay.

    Deadlock khong c phat hien, dan en viec giam hieu suat cua he thong. Cuoi cung, he thong co the

    ngng hoat ong va phai c khi ong lai.

  • Khoa KTMT 16

    1. Ngan deadlock (deadlock prevention)

    Ngan deadlock bang cach ngan mot trong 4 ieu kien

    can cua deadlock

    1. Ngan mutual exclusion

    oi vi nonsharable resource (vd: printer): khong lam c

    oi vi sharable resource (vd: read-only file): khong can thiet

  • Khoa KTMT 17

    Ngan deadlock (tt)

    2. Ngan Hold and Wait

    Cach 1: moi process yeu cau toan bo tai nguyen can thiet mot lan. Neu co u tai nguyen th he thong se cap phat, neu khong

    u tai nguyen th process phai b blocked.

    Cach 2: khi yeu cau tai nguyen, process khong c gi bat ky tai nguyen nao. Neu ang co th phai tra lai trc khi yeu cau.

    V du e so sanh hai cach tren: mot qua trnh copy d lieu t tape drive sang disk file, sap xep disk file, roi in ket qua ra

    printer.

    Khuyet iem cua cac cach tren:

    Hieu suat s dung tai nguyen (resource utilization) thap

    Qua trnh co the b starvation

  • Khoa KTMT 18

    Ngan deadlock (tt)

    3. Ngan No Preemption: neu process A co gi tai nguyen va ang

    yeu cau tai nguyen khac nhng tai nguyen nay cha cap phat ngay

    c th

    Cach 1: He thong lay lai moi tai nguyen ma A ang gi

    A ch bat au lai c khi co c cac tai nguyen a b lay lai cung vi tai nguyen ang yeu cau

    Cach 2: He thong se xem tai nguyen ma A yeu cau

    Neu tai nguyen c gi bi mot process khac ang i them tai nguyen, tai nguyen nay c he thong lay lai va

    cap phat cho A.

    Neu tai nguyen c gi bi process khong i tai nguyen, A phai i va tai nguyen cua A b lay lai. Tuy nhien he

    thong ch lay lai cac tai nguyen ma process khac yeu cau

  • Khoa KTMT 19

    Ngan deadlock (tt)

    4. Ngan Circular Wait: gan mot th t cho tat ca cac tai nguyen trong

    he thong.

    Tap hp loai tai nguyen: R={R1, R

    2,,R

    m }

    Ham anh xa: F: R->N

    V du: F(tape drive) = 1, F(disk drive) = 5, F(printer) = 12

    F la ham nh ngha th t tren tap cac loai tai nguyen.

  • Khoa KTMT 20

    Ngan deadlock (tt)

    4. Ngan Circular Wait (tt)

    Moi process ch co the yeu cau thc the cua mot loai tai nguyen theo th t tang dan (nh ngha bi ham F) cua loai tai nguyen. V du

    Chuoi yeu cau thc the hp le: tape drive disk drive printer

    Chuoi yeu cau thc the khong hp le: disk drive tape drive

    Khi mot process yeu cau mot thc the cua loai tai nguyen Rjth no phai

    tra lai cac tai nguyen Rivi F(R

    i) > F(R

    j).

    Chng minh gia s ton tai mot chu trnh deadlock

    F(R4) < F(R

    1)

    F(R1) < F(R

    2)

    F(R2) < F(R

    3)

    F(R3) < F(R

    4)

    Vay F(R4) < F(R

    4), mau thuan!

    P1

    R1

    P2

    P4 P3

    R3

    R2R4

  • Khoa KTMT 21

    2. Tranh tac nghen

    Deadlock avoidance

    Deadlock prevention s dung tai nguyen khong hieu qua.

    Deadlock avoidance van am bao hieu suat s dung tai

    nguyen toi a en mc co the.

    Yeu cau moi process khai bao so lng tai nguyen toi a can e thchien cong viec

    Giai thuat deadlock-avoidance se kiem tra trang thai cap phat tai nguyen(resource-allocation state) e bao am he thong khong ri vao deadlock.

    Trang thai cap phat tai nguyen c nh ngha da tren so tai nguyencon lai, so tai nguyen a c cap phat va yeu cau toi a cua cac

    process.

  • Khoa KTMT 22

    Trang thai safe va unsafe

    Mot trang thai cua he thong c goi la an toan (safe)

    neu ton tai mot chuoi (th t) an toan (safe sequence).

    Mot chuoi qua trnh la mot chuoi an toan

    neu

    Vi moi i = 1,,n, yeu cau toi a ve tai nguyen cua Pico the

    c thoa bi

    tai nguyen ma he thong ang co san sang (available)

    cung vi tai nguyen ma tat ca Pj, j < i, ang gi.

    Mot trang thai cua he thong c goi la khong an toan

    (unsafe) neu khong ton tai mot chuoi an toan.

  • Khoa KTMT 23

    Chuoi an toan (tt)

    V du: He thong co 12 tape drives va 3 qua trnh P0, P

    1, P

    2

    Tai thi iem t0

    Con 3 tape drive san sang.

    Chuoi la chuoi an toan he thong la an toan

    Maximum

    needs

    Current

    needs

    P0

    10 5

    P1

    4 2

    P2

    9 2

  • Khoa KTMT 24

    Chuoi an toan (tt)

    Gia s tai thi iem t1, P

    2yeu cau va c cap phat 1

    tape drive

    con 2 tape drive san sang

    He thong con an toan khong?

    P0

    10 5

    P1

    4 2

    P2

    9 3

    can toi a ang gi

  • Khoa KTMT 25

    Trang thai safe/unsafe va deadlock

    Neu he thong ang trang thai safe khong deadlock.

    Neu he thong ang trang thai unsafe co the dan en deadlock.

    Tranh deadlock bang cach bao am he thong khong i en trang

    thai unsafe.

    safe

    deadlock unsafe

  • Khoa KTMT 26

    Giai thuat o th cap phat tai nguyen

    Khai niem canh thnh cau

    P1

    P2

    P1

    P2

    R1

    R2

    R1

    R2

  • Khoa KTMT 27

    Giai thuat banker

    Ap dung cho he thong cap phat tai nguyen trong o moi

    loai tai nguyen co the co nhieu instance.

    Bat chc nghiep vu ngan hang (banking)

    ieu kien

    Moi process phai khai bao so lng thc the (instance) toi a

    cua moi loai tai nguyen ma no can

    Khi process yeu cau tai nguyen th co the phai i mac du tai

    nguyen c yeu cau ang co san

    Khi process a co c ay u tai nguyen th phai hoan tra trong

    mot khoang thi gian hu han nao o.

  • Khoa KTMT 28

    Giai thuat banker (tt)

    n: so process, m: so loai tai nguyen

    Cac cau truc d lieu

    Available: vector o dai m

    Available[ j ] = k loai tai nguyen Rjco k instance san sang

    Max: ma tran n m

    Max[ i, j ] = k qua trnh Piyeu cau toi a k instance cua loai

    tai nguyen Rj

    Allocation: ma tran n m

    Allocation[i, j] = k Pi a c cap phat k instance cua Rj

    Need: ma tran n m

    Need[i, j] = k Pi can them k instance cua Rj

    Nhan xet: Need[i, j] = Max[i, j] Allocation[i, j]

    Ky hieu Y X Y[i] X[i], v du (0, 3, 2, 1) (1, 7, 3, 2)

  • Khoa KTMT 29

    Giai thuat banker (tt)

    1.Giai thuat an toan

    Tm mot chuoi an toan

    1. Goi Work va Finish la hai vector o dai la m va n. Khi tao

    Work := Available

    Finish[ i ] := false, i = 1,, n

    2. Tm i thoa

    (a) Finish[ i ] = false

    (b) Needi

    Work (hang th i cua Need)

    Neu khong ton tai i nh vay, en bc 4.

    3. Work := Work + Allocationi

    Finish[ i ] := true

    quay ve bc 2.

    4. Neu Finish[ i ] = true, i = 1,, n, th he thong ang trang thai safe

    Thi gian chay cua giai thuat la O(mn2)

  • Khoa KTMT 30

    Giai thuat banker (tt)

    2. Giai thuat yeu cau (cap phat) tai nguyen

    Goi Requestila request vector cua process P

    i.

    Requesti[ j ] = k P

    i can k instance cua tai nguyen R

    j.

    1. Neu Requesti Need

    ith en bc 2. Neu khong, bao

    loi v process a vt yeu cau toi a.

    2. Neu Requesti Available th qua bc 3. Neu khong, P

    i

    phai ch v tai nguyen khong con u e cap phat.

    3. Gia nh cap phat tai nguyen ap ng yeu cau cua Pi

    bang cach cap nhat trang thai he thong nh sau:

    Available := Available Requesti

    Allocationi:= Allocation

    i+ Request

    i

    Needi

    := Needi Request

    i

  • Khoa KTMT 31

    Giai thuat banker (tt)

    2.Giai thuat yeu cau tai nguyen

    Ap dung giai thuat kiem tra trang thai an toan len trang thai tren

    Neu trang thai la safe th tai nguyen c cap thc s cho Pi.

    Neu trang thai la unsafe th Piphai i, va

    phuc hoi trang thai:

    Available := Available + Requesti

    Allocationi:= Allocation

    i Request

    i

    Needi

    := Needi+ Request

    i

  • Khoa KTMT 32

    Giai thuat kiem tra trang thai an toan V du

    Co 5 process P0

    ,, P4

    Co 3 loai tai nguyen: A (co 10 instance), B (5 instance) va C (7

    instance).

    S o cap phat trong he thong tai thi iem T0

    Allocation Max Available Need

    A B C A B C A B C A B C

    P0

    0 1 0 7 5 3 3 3 2 7 4 3

    P1

    2 0 0 3 2 2 1 2 2

    P2

    3 0 2 9 0 2 6 0 0

    P3

    2 1 1 2 2 2 0 1 1

    P4

    0 0 2 4 3 3 4 3 1

  • Khoa KTMT 33

    GT (kiem tra trang thai)an toan Vd (tt)

    Allocation Need Work

    A B C A B C A B C

    P0 0 1 0 7 4 3 3 3 2

    P1 2 0 0 1 2 2

    P2 3 0 2 6 0 0

    P3 2 1 1 0 1 1

    P4 0 0 2 4 3 1

    Chuoi an toan

    7 4 3

    7 4 5

    10 4 7 10 5 7

    5 3 2

    P1

    P3

    P4

    P2

  • Khoa KTMT 34

    GT cap phat tai nguyen V du

    Yeu cau (1, 0, 2) cua P1

    co thoa c khong?

    Kiem tra ieu kien Request1 Available:

    (1, 0, 2) (3, 3, 2) la ung

    Gia nh thoa yeu cau, kiem tra trang thai mi co phai la safe hay khong.

    Trang thai mi la safe (chuoi an toan la ), vay co

    the cap phat tai nguyen cho P1.

    Allocation Need Available

    A B C A B C A B C

    P0

    0 1 0 7 4 3 2 3 0

    P1

    3 0 2 0 2 0

    P2

    3 0 2 6 0 0

    P3

    2 1 1 0 1 1

    P4

    0 0 2 4 3 1

    P4 (3, 3, 0) ?

    P0 (0, 2, 0) ?

    P3 (0, 2, 1)?

  • Khoa KTMT 35

    3. Phat hien deadlock (Deadlock detection)

    Chap nhan xay ra deadlock trong he thong, kiem tra

    trang thai he thong bang giai thuat phat hien deadlock.

    Neu co deadlock th tien hanh phuc hoi he thong

    Cac giai thuat phat hien deadlock thng s dung mo

    hnh RAG.

    He thong cap phat tai nguyen c khao sat trong moi

    trng hp sau

    1. Moi loai tai nguyen ch co mot thc the (instance)

    2. Moi loai tai nguyen co the co nhieu thc the

  • Khoa KTMT 36

    Moi loai tai nguyen ch co mot thc the

    S dung wait-for graph

    Wait-for graph c dan xuat t RAG bang cach bo cac node bieu dien tai nguyen va ghep cac canh tng ng.

    Co canh t Pien P

    j P

    iang ch tai nguyen t P

    j

    Mot giai thuat kiem tra co ton tai chu trnh trong wait-for graph hay

    khong se c goi nh ky. Giai thuat phat hien chu trnh co thi

    gian chay la O(n2), vi n la so nh cua graph.

    R1 R3 R4

    P2P1 P3

    P5

    R2 R5P4

    P2P1 P3

    P5

    P4

  • Khoa KTMT 37

    Moi loai tai nguyen co nhieu thc the

    Phng phap dung wait-for graph khong ap dung c cho trng

    hp moi loai tai nguyen co nhieu instance.

    Cac cau truc d lieu dung trong giai thuat phat hien deadlock

    Available: vector o dai m

    so instance san sang cua moi loai tai nguyen

    Allocation: ma tran n m

    so instance cua moi loai tai nguyen a cap phat cho moi process

    Request: ma tran n m

    yeu cau hien tai cua moi process.

    Request [i, j ] = k Pi ang yeu cau them k instance cua Rj

  • Khoa KTMT 38

    Giai thuat phat hien deadlock

    1. Goi Work va Finish la vector kch thc m va n. Khi tao:

    Work := Available

    i = 1, 2,, n, neu Allocationi 0 th Finish[ i ] := false

    con khong th Finish[ i ] := true

    2. Tm i thoa man:

    Finish[ i ] := false va

    Requesti Work

    Neu khong ton tai i nh the, en bc 4.

    3. Work := Work + Allocationi

    Finish[ i ] := true

    quay ve bc 2.

    4. Neu Finish[ i ] = false, vi mot i = 1,, n, th he thong ang trang

    thai deadlock. Hn the na, Finish[ i ] = false th Pib deadlocked.

    thi gian chay

    cua giai thuat

    O(mn2)

  • Khoa KTMT 39

    Giai thuat phat hien deadlock V du

    He thong co 5 qua trnh P0

    ,, P4

    3 loai tai nguyen: A (7 instance), B (2 instance), C (6 instance).

    Allocation Request Available

    A B C A B C A B C

    P0

    0 1 0 0 0 0 0 0 0

    P1

    2 0 0 2 0 2

    P2

    3 0 3 0 0 0

    P3

    2 1 1 1 0 0

    P4

    0 0 2 0 0 2

    Chay giai thuat, tm c chuoi vi Finish[ i ]

    = true, i = 1,, n, vay he thong khong b deadlocked.

  • Khoa KTMT 40

    Giai thuat phat hien deadlock V du (tt)

    P2

    yeu cau them mot instance cua C. Ma tran Request nh sau:

    Request

    A B C

    P0

    0 0 0

    P1

    2 0 2

    P2

    0 0 1

    P3

    1 0 0

    P4

    0 0 2

    Trang thai cua he thong la g?

    Co the thu hoi tai nguyen ang s hu bi process P0

    nhng van

    khong u ap ng yeu cau cua cac process khac.

    Vay ton tai deadlock, bao gom cac process P1, P

    2, P

    3, va P

    4 .

  • Khoa KTMT 41

    Phuc hoi deadlock (Deadlock Recovery)

    Khi deadlock xay ra, e phuc hoi

    bao ngi van hanh (operator)

    hoac

    he thong t ong phuc hoi bang cach be gay chu trnh deadlock:

    cham dt mot hay nhieu qua trnh

    lay lai tai nguyen t mot hay nhieu qua trnh

  • Khoa KTMT 42

    Deadlock Recovery: Cham dt qua trnh

    Phuc hoi he thong b deadlock bang cach cham dt qua

    trnh

    Cham dt tat ca process b deadlocked, hoac

    Cham dt lan lt tng process cho en khi khong con deadlock

    S dung giai thuat phat hien deadlock e xac nh con deadlock hay khong

    Da tren yeu to nao e chon process can c cham

    dt?

    o u tien cua process

    Thi gian a thc thi cua process va thi gian con lai

    Loai tai nguyen ma process a s dung

    Tai nguyen ma process can them e hoan tat cong viec

    So lng process can c cham dt

    Process la interactive process hay batch process

  • Khoa KTMT 43

    Deadlock recovery: Lay lai tai nguyen

    Lay lai tai nguyen t mot process, cap phat cho process

    khac cho en khi khong con deadlock na.

    Cac van e trong chien lc thu hoi tai nguyen:

    Chon nan nhan e toi thieu chi ph (co the da tren so tai nguyen s hu, thi gian CPU a tieu ton,...)

    Tr lai trang thai trc deadlock (Rollback): rollback process b lay lai tai nguyen tr ve trang thai safe, tiep tuc process t trang

    thai o. He thong can lu gi mot so thong tin ve trang thai cac

    process ang thc thi.

    oi tai nguyen (Starvation): e tranh starvation, phai bao am khong co process se luon luon b lay lai tai nguyen moi khi

    deadlock xay ra.

  • Khoa KTMT 44

    Phng phap ket hp e giai quyet Deadlock

    Ket hp 3 phng phap c ban

    Ngan chan (Prevention)

    Tranh (Avoidance)

    Phat hien (Detection)

    Cho phep s dung cach giai quyet toi u cho moi lp tai

    nguyen trong he thong.

    Phan chia tai nguyen thanh cac lp theo th bac.

    S dung ky thuat thch hp nhat cho viec quan ly deadlock trong moi lp nay.

  • Khoa KTMT 45

    Bai tap

    Bai 01: Liet ke 3 trng hp xay ra deadlock trong i

    song

    Bai 02:

    R1 R3

    P1 P2 P3

    R2R4

    Deadlock ?

  • Khoa KTMT 46

    Bai tap

    Bai 03:

    A) Tm Need

    B) He thong co an toan khong

    C)Neu P1

    yeu cau (0,4,2,0) th co the cap phat cho no

    ngay khong?


Recommended