Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C++
#include<stdio.h>
#include <semaphore.h>

sem_t semA, semB;


main()
{

  sem_init(&semA, 0, 0);
  sem_init(&semB, 0, 0);

int a=0,b=0;
	while(1)
	{
		a++;
		printf("Run aaa  %d\n",a);
		sem_post(&semB);
   		sem_wait(&semA);
		printf("go to bb  %d\n",a);
		sleep(1);

	}
	while(1)
	{
		b++;
		printf("post to bbb %d\n",b);
		sem_post(&semA);
		sem_wait(&semB);
		printf("go to aaa   %d\n",b);
		sleep(1);

	}
}
Posted
Comments
Sandeep Mewara 5-Jan-13 0:39am    
For now, without any detail on what this code should do and your efforts, question just looks like a homework.

Have you even looked at the code? Tried anything? While loop?
Sergey Alexandrovich Kryukov 5-Jan-13 1:21am    
Something tells me: this is not a homework, or, may be, this is a homework, but on the subject OP have no clue about, as if he missed all the lectures and classes, if any. The topic is related to thread, and here there are not threads except one. I answered, please see.
—SA
nv3 5-Jan-13 5:16am    
Convert the two while-loops into separate threads and all of a sudden things start to make sense! But this is your homework :-)

1 solution

From the very beginning, it makes no sense at all. Did you learn what semaphores are for? The are used to synchronize threads in a certain way. And where are your threads? There is nothing to discuss here.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900