0% found this document useful (0 votes)
8 views4 pages

Simplified Deadlock Detection Models

The document presents simplified models for deadlock detection using the Chandy-Misra-Haas approach, focusing on AND and OR models. The AND model detects deadlock only when all processes are blocked, while the OR model detects it if at least one process is blocked, with both models sharing similar code structure. The document includes Go code examples for both models and explains how to run the server and client applications.

Uploaded by

Hari Prasaath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Simplified Deadlock Detection Models

The document presents simplified models for deadlock detection using the Chandy-Misra-Haas approach, focusing on AND and OR models. The AND model detects deadlock only when all processes are blocked, while the OR model detects it if at least one process is blocked, with both models sharing similar code structure. The document includes Go code examples for both models and explains how to run the server and client applications.

Uploaded by

Hari Prasaath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Simplified Deadlock Detection Models

Yes, the AND and OR codes for the Chandy-Misra-Haas model are pretty similar and can be simplified. Here’s a more straightforward version:

✅ Simplified AND Model


Goal: Detect deadlock when all processes are blocked.
Code:

go

func (c *Clock) SendProbe(req *Probe, res *bool) error {


*res = (req.Initiator == req.Target) // If the probe returns to the initiator → deadlock!
return nil
}

Client:

go

client.Call("Clock.SendProbe", probe, &res)


if res {
fmt.Println("Deadlock Detected!")
}

✅ Simplified OR Model
Goal: Detect deadlock when at least one process is blocked.
Code:

go

func (c *Clock) SendProbe(req *Probe, res *bool) error {


*res = (req.Initiator == req.Target) // Same logic as AND model
return nil
}

Client:

go

client.Call("Clock.SendProbe", probe, &res)


if res {
fmt.Println("Deadlock Detected!")
}

🔥 Difference:
AND Model → Deadlock if all processes are blocked.
OR Model → Deadlock if at least one process is blocked.

Both codes are identical because the condition (`req.Initiator == req.Target`) determines the outcome. The key difference lies in the
interpretation — for AND, all processes must confirm blocking, while for OR, even one is enough.

Here’s the full simplified code for both the AND and OR models in Go:

✅ Chandy-Misra-Haas (AND Model)


Goal: Detect deadlock when all processes are blocked.

server.go

go

package main

import (
"fmt"
"net"
"net/rpc"
)

type Probe struct {


Initiator int
Target int
}

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/4
type Clock struct{}

func (c *Clock) SendProbe(req *Probe, res *bool) error {


// Deadlock if the probe returns to the initiator
*res = (req.Initiator == req.Target)
return nil
}

func main() {
clock := new(Clock)
rpc.Register(clock)

listener, err := net.Listen("tcp", ":1234")


if err != nil {
fmt.Println("Error starting server:", err)
return
}
defer listener.Close()

fmt.Println("Server is listening on port 1234...")

for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Connection error:", err)
continue
}
go rpc.ServeConn(conn)
}
}

client.go

go

package main

import (
"fmt"
"net/rpc"
)

type Probe struct {


Initiator int
Target int
}

func main() {
client, err := rpc.Dial("tcp", "localhost:1234")
if err != nil {
fmt.Println("Connection error:", err)
return
}
defer client.Close()

probe := Probe{Initiator: 1, Target: 1} // Change Target to simulate different cases


var res bool

err = client.Call("Clock.SendProbe", &probe, &res)


if err != nil {
fmt.Println("RPC error:", err)
return
}

if res {
fmt.Println("Deadlock Detected!")
} else {
fmt.Println("No Deadlock.")
}
}

✅ Chandy-Misra-Haas (OR Model)


Goal: Detect deadlock when at least one process is blocked.

server.go

(Same as AND model)

go

package main

import (
"fmt"
"net"
"net/rpc"
)

type Probe struct {


Initiator int
Target int

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/4
}

type Clock struct{}

func (c *Clock) SendProbe(req *Probe, res *bool) error {


// Deadlock if the probe returns to the initiator
*res = (req.Initiator == req.Target)
return nil
}

func main() {
clock := new(Clock)
rpc.Register(clock)

listener, err := net.Listen("tcp", ":1234")


if err != nil {
fmt.Println("Error starting server:", err)
return
}
defer listener.Close()

fmt.Println("Server is listening on port 1234...")

for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Connection error:", err)
continue
}
go rpc.ServeConn(conn)
}
}

client.go

(Same as AND model)

go

package main

import (
"fmt"
"net/rpc"
)

type Probe struct {


Initiator int
Target int
}

func main() {
client, err := rpc.Dial("tcp", "localhost:1234")
if err != nil {
fmt.Println("Connection error:", err)
return
}
defer client.Close()

probe := Probe{Initiator: 1, Target: 1} // Change Target to simulate different cases


var res bool

err = client.Call("Clock.SendProbe", &probe, &res)


if err != nil {
fmt.Println("RPC error:", err)
return
}

if res {
fmt.Println("Deadlock Detected!")
} else {
fmt.Println("No Deadlock.")
}
}

🚀 How to Run:
1. Start the server

bash

go run server.go

2. Run the client

bash

go run client.go

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/4
🔥 Difference:
The actual logic is the same — it's just how you interpret the outcome:
AND → Deadlock if all processes are blocked.
OR → Deadlock if at least one process is blocked.

This is simplified and clean. Just copy-paste and it should work! 😎


Good question! 😎
The AND and OR models have the same code structure, but the difference lies in the interpretation of the result (`res`):

🔍 This line is the key:


In server.go for both AND and OR models:

go

*res = (req.Initiator == req.Target)

✅ AND Model Logic:


Deadlock is detected only if all processes are blocked.
👉 To implement true AND behavior, you need to track the state of multiple processes and confirm that all are blocked before setting `*res
= true`.
Example for AND:

go

*res = allProcessesBlocked()

✅ OR Model Logic:
Deadlock is detected if at least one process is blocked.
👉 The current code already implements the OR logic, since returning `true` based on a single match (`req.Initiator == req.Target`) is
enough for OR behavior.

🛠️ To Fix the Difference Properly:


1. For AND, check all processes:

go

*res = allProcessesBlocked()

2. For OR, check if any process is blocked (which is already implemented).

Let me know if you want me to update the code! 😎

ChatGPT can make mistakes. Check important info.

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/4

You might also like