LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   semaphore program (https://www.linuxquestions.org/questions/programming-9/semaphore-program-4175469088/)

answerme 07-09-2013 11:44 PM

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


a4z 07-10-2013 02:35 AM

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.

NevemTeve 07-10-2013 05:25 AM

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.


All times are GMT -5. The time now is 02:21 AM.