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 18: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 Sergey Alexandrovich Kryukov 205
1 Zoltán Zörgő 200
2 Peter Leow 160
3 BillWoodruff 85
4 bling 70
0 Sergey Alexandrovich Kryukov 9,543
1 OriginalGriff 6,901
2 Peter Leow 4,737
3 Zoltán Zörgő 4,404
4 CHill60 2,932


Advertise | Privacy | Mobile
Web02 | 2.8.150129.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2015
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