Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Linux
#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 4-Jan-13 17:57pm
Comments
Sandeep Mewara at 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 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 144
2 Rob Philpott 100
3 CHill60 70
4 Peter Leow 60
0 OriginalGriff 6,902
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,479
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100