LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2013, 11:44 PM   #1
answerme
Member
 
Registered: Apr 2008
Posts: 97

Rep: Reputation: 24
semaphore program


Hi All
I running one simple semaphore application. I have 2 threads , where one thread is waiting for sem post to happen so I have put sem_wait & other thread is doing sem_post .When I come keeping delay (sleep of 1 sec) I find scheduling is happening properly .But without delay I am finding it strange behaviour i.e. scheduling is not in the proper way as it was when I kept delay of 1 second. I feel sem_wait should have been blocked & the print "after wait" should only execute when sem post happens I have kept both program as well as output also .
Can anyone tell me why is that so i.e its not scheduling properly .
Or I have done something wrong in application.

Code:
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>
#include<semaphore.h>

void *start_routine1();
void *start_routine2();


sem_t flag1;
static int cnt=0;
main()
{
	pthread_t tid1;
	pthread_t tid2;
	int ret;
	sem_init(&flag1,1,0);
        ret = pthread_create(&tid1,NULL, start_routine1, NULL);
	ret = pthread_create(&tid2,NULL, start_routine2, NULL);
	pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);
   

}
void *start_routine1()
{
	
	while(1)
	{	
        // printf("before wait\n");
	 sem_wait(&flag1);
		 printf("after wait\n");
	}
}


void *start_routine2()
{
	while(1)
	{	
	// printf("before post\n");
	  sem_post(&flag1);
	  printf("after post\n");
	}
}
Code:
OUTPUT:
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after wait
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
after post
 
Old 07-10-2013, 02:35 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
you can not assume that the output on the console keep the order in which the events happen.
if you whant this you need a own syncronisation mechanism.
printf only helps you to not mix the paralell output of the text 'after post' and 'after wait' to something like 'aaffter te rpposotst' (with one new line somewhere in between)

if you want to see that start_routine1 realy waits, put a getchar() in front of sem_post than you can see that nothing happens until you hit return.
 
Old 07-10-2013, 05:25 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
As a start, add fflush(stdout) after every printf.

Plus, you should state what do you really what to achieve with this. If you want to synchronize your threads I suggest you use mutex variables.

Last edited by NevemTeve; 07-10-2013 at 05:35 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Semaphore atulkatrajkar Linux - Newbie 4 10-12-2010 12:56 PM
Semaphore issues...example program included kamputty Programming 4 04-16-2007 07:06 PM
Semaphore tzzdvd Programming 1 05-24-2005 09:23 PM
semaphore beginner_84 Programming 5 08-25-2004 06:54 PM
about semaphore dummyagain Programming 1 11-11-2003 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration