- Posts: 3
- Thank you received: 0
What's the reason for the deadlock here?
17 years 9 months ago #19502
by chantal
What's the reason for the deadlock here? was created by chantal
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?
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?
Time to create page: 0.132 seconds