[英]print 3 4 5 in sequence for 50 times using semaphore synchronization using three threads in C Linux
My programming is going in dead lock.I am trying to print three numbers 3 4 5 sequentially for 50 times using three threads using semaphore synchronization. 我的编程陷入僵局。我试图使用信号量同步的三个线程依次打印三个数字3 4 5 50次。 Please help me. 请帮我。
Below is the code 下面是代码
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
sem_t sem1;
sem_t sem2;
sem_t sem3;
void * fun1(void *)
{
for(int i = 0; i < 50 ; i++)
{
sem_wait(&sem1);
sem_wait(&sem3);
cout<<"3"
sem_post(&sem2);
sem_post(&sem3);
}
}
void * fun2(void *)
{
for(int i = 0; i < 50 ; i++)
{
sem_wait(&sem2);
sem_wait(&sem3);
cout<<"4";
sem_post(&sem3);
sem_post(&sem1);
}
}
void * fun3 (void *)
{
for(int i = 0; i< 50; i++)
{
sem_wait(&sem2);
sem_wait(&sem3);
cout<<"5";
sem_post(&sem1);
sem_post(&sem2);
}
}
int main()
{
pthread_t t1;
pthread_t t2;
pthread_t t3;
sem_init(&sem1,0,1);
sem_init(&sem2,0,0);
sem_init(&sem3,0,1);
pthread_create(&t1,NULL,&fun1,NULL);
pthread_create(&t2,NULL,&fun2,NULL);
pthread_create(&t3,NULL,&fun3,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
return 1;
}
Please help me to understand and solve this deadlock.Provide suggestions also i can do this for example 3 4 5 6 using 4 etc threads 请帮助我理解和解决此死锁。也可以提供一些建议,例如我可以使用4个etc线程来实现3 4 5 6
Please help me to understand and solve this deadlock. 请帮助我了解并解决此僵局。
There is indeed a deadlock in your code. 您的代码中确实存在死锁。 Consider at the beginning, thread 1 first gets 2 semaphores and call cout << "3"
. 首先考虑,线程1首先获得2个信号量,并调用cout << "3"
。 After posting sem2
and sem3
, it is possible that thread 3 immediately gets these 2 sem, then call cout << "5"
. 发布sem2
和sem3
之后,线程3可能立即获得这2个sem,然后调用cout << "5"
。 However, after thread 3 posting sem1
and sem2
, no one can reach a cout <<
statement, because sem3
's value is 0 and everyone needs to pass a wait of sem3
. 但是,在线程3发布sem1
和sem2
,没有人可以到达cout <<
语句,因为sem3
的值为0并且每个人都需要传递sem3
的等待。
If you are wondering why there is totally no output, it's because the buffer inside iostream
. 如果您想知道为什么完全没有输出,那是因为iostream
的缓冲区。 For console output, "\\n"
will flush buffer, so if you replace "3"
by "3\\n"
, you can see the output. 对于控制台输出, "\\n"
将刷新缓冲区,因此,如果将"3"
替换为"3\\n"
,则可以看到输出。
Provide suggestions also i can do this for example 3 4 5 6 using 4 etc threads 提供建议,我也可以使用4个其他线程执行此操作,例如3 4 5 6
In the following code, you should see the symmetry, which can be easily generalized to any number of thread. 在以下代码中,您应该看到对称性,可以很容易地将其推广到任意数量的线程。 And you should always call sem_destroy
after using semaphore, otherwise you might get system level resource leak. 而且,在使用信号量后,应始终调用sem_destroy
,否则可能会导致系统级资源泄漏。
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
sem_t sem1;
sem_t sem2;
sem_t sem3;
void * fun1(void *)
{
for(int i = 0; i < 50 ; i++)
{
sem_wait(&sem1);
cout<<"3\n";
sem_post(&sem2);
}
}
void * fun2(void *)
{
for(int i = 0; i < 50 ; i++)
{
sem_wait(&sem2);
cout<<"4\n";
sem_post(&sem3);
}
}
void * fun3 (void *)
{
for(int i = 0; i< 50; i++)
{
sem_wait(&sem3);
cout<<"5\n";
sem_post(&sem1);
}
}
int main()
{
pthread_t t1;
pthread_t t2;
pthread_t t3;
sem_init(&sem1,0,1);
sem_init(&sem2,0,0);
sem_init(&sem3,0,0);
pthread_create(&t1,NULL,&fun1,NULL);
pthread_create(&t2,NULL,&fun2,NULL);
pthread_create(&t3,NULL,&fun3,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.