Hot Downloads

Welcome, Guest
Username: Password: Remember me

TOPIC: What's the reason for the deadlock here?

What's the reason for the deadlock here? 9 years 10 months ago #19502

  • chantal
  • chantal's Avatar
  • Offline
  • New Member
  • Posts: 3
  • Karma: 0
hello,

i have written this linux code that executes multiple processes.
it is running well, but the problem is that when i run it for the first few times it runs great without any fault, but after that it runs with deadlocks in it

here it is

[code:1]
semaphore pr = 3;
semaphore mutex = 1;
int printer[3];
int i = 0;

void Init_Print()
{
printer[0] = printer[1] = printer[2] = -1;
}

void Obtain_Printer(int process_id)
{
wait(pr);
wait(mutex);
if((printer[0] != -1) && (printer[1] != -1) && (printer[2] != -1))
cout<< "There is no printer found" << endl;
else
{
if(printer[0] == -1)
printer[0] = process_id;
else
if(printer[1] == -1)
printer[1] = process_id;
else
if(printer[2] == -1)
printer[2] == process_id;
cout<< "printer is obtained" << endl;
}
signal(mutex);
}

void Release_printer(int process_id)
{
wait(mutex);
while((i != 2) && (printer[i] != process_id))
i++;
if(printer[i] == process_id)
{
cout<< "releasing printer with process id: " << process_id
<< endl;
printer[i] = -1;
signal(pr);
}
else
cout<< "Error: there is no printer obtained" << endl;
signal(mutex);
}

void A(int id)
{
Obtain_Printer(id);
Release_Printer(id);
}

void B(int id)
{
Obtain_Printer(id);
Release_Printer(id);
}

void C(int id)
{
Obtain_Printer(id);
Release_Printer(id);
}

void D(int id)
{
Obtain_Printer(id);
Release_Printer(id);
}

void E(int id)
{
Obtain_Printer(id);
Release_Printer(id);
}

main()
{
cobegin
{
Init_Print();
A(3); B(23); C(13); D(9); E(15);
}
}

[/code:1]

can any one the reason for the deadlock here?
The administrator has disabled public write access.
Time to create page: 0.073 seconds

CCENT/CCNA

Cisco Routers

  • SSL WebVPN
  • Securing Routers
  • Policy Based Routing
  • Router on-a-Stick

VPN Security

  • Understand DMVPN
  • GRE/IPSec Configuration
  • Site-to-Site IPSec VPN
  • IPSec Modes

Cisco Help

  • VPN Client Windows 8
  • VPN Client Windows 7
  • CCP Display Problem
  • Cisco Support App.

Windows 2012

  • New Features
  • Licensing
  • Hyper-V / VDI
  • Install Hyper-V

Linux

  • File Permissions
  • Webmin
  • Groups - Users
  • Samba Setup