sem_wait, sem_post confusion?


 
Thread Tools Search this Thread
Top Forums Programming sem_wait, sem_post confusion?
# 1  
Old 12-18-2011
sem_wait, sem_post confusion?

Hi friends,
I have written this small program using the concept of semaphores. I am a bit confused, hope u can help me with it.
The idea is that

1. Two threads are created
2. First will be displaying 0(zero) on the screen
3. Second will be displaing 1(one) on the screen
4. This process will go on upto three times for each thread, it would look like

Code:
 
0
 
1
 
0
 
1
 
0
 
1

But the program that i have written prints like this
0

0

0

1

1

1

I don't really know what I am missing here, could you pin point it to me, I think my concept of semaphores is corrct.
You can have a look at my code

Code:
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/sem.h>
#include <semaphore.h>
sem_t mysem;
void *thread(void *);
int main()
{
        sem_init(&mysem,0,1);
        pthread_t tid[2];
 
        pthread_attr_t attr[2];
        pthread_attr_init(&attr[0]);
        pthread_attr_init(&attr[1]);
        {
        pthread_create(&tid[0],&attr[0],thread,(void *)0);
        pthread_create(&tid[1],&attr[1],thread,(void *)1);
        }
        {
        pthread_join(tid[0],NULL);
        pthread_join(tid[1],NULL);
        }
        printf("\nThreads finished!\n");
        return 0;
}
void *thread(void *n)
{
        int i;
        i = (int)n;
        int x = 0;
        while(x < 3)
        {
        sem_wait(&mysem);
        printf("%d\n\n",i);
        sem_post(&mysem);
        x++;
        }
        pthread_exit(0);
}

But the same program works when I put a sleep(1) in the middle like this

Code:
void *thread(void *n)
{
        int i;
        i = (int)n;
        int x = 0;
        while(x < 3)
        {
        sem_wait(&mysem);
        printf("%d\n\n",i);
        sem_post(&mysem);
        x++;
        sleep(1);
        }
        pthread_exit(0);
}

But I want to achieve what I want to achieve by using only semaphores, no sleep function,

Thanks
Looking forward to your lovely replies.
# 2  
Old 12-18-2011
define another two semaphore:
Code:
sem_t zero, one;

a thread wait for zero and post one, while the other thread wait for one and post zero.
This User Gave Thanks to vistastar For This Post:
# 3  
Old 12-18-2011
Thanks a ton buddy!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Confusion with the concept of wc -c and wc -m

Is wc -c and wc -m same ? Shellscript::cat file1 hello Shellscript::cat file1 | wc -c 6 Shellscript::cat file1 | wc -m 6 Shellscript::file file1 file1: ASCII text Shellscript::uname -a Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/LinuxAtleast... (5 Replies)
Discussion started by: shellscripting
5 Replies

2. Homework & Coursework Questions

Semaphores sem_wait sem_post problem

hallo! if there are many processes running and I initialize the semaphore like this: my_sem = sem_open(SEM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 10); (the last argument is 10) and then i use sem_wait(my_sem); sleep(5); sem_post; Will 10 processes be able to access the... (1 Reply)
Discussion started by: whatevernever
1 Replies

3. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

4. Programming

Undefined: sem_init, sem_post, sem_wait

Hi friends, I am using semaphores in my program, but when I compile the program, it gives the following error $ gcc sem.c -o sem -lpthread Undefined first referenced symbol in file sem_init ... (1 Reply)
Discussion started by: gabam
1 Replies

5. Shell Programming and Scripting

conditional confusion

Hell Unix.com Community: I am working on a personal project using yad v0.12.4 (zenity fork) and have hit a wall on how to show a progress bar while my function is processing. I have been all over the ABS Guide, googled 21 Linux-specific sites that I revere. I even asked on the yad-common... (4 Replies)
Discussion started by: Habitual
4 Replies

6. Cybersecurity

LDAP; confusion

Hello, I hope all is well. Two issues that I am grappling with. One: Is this a true statement: (AIX, LDAP configured), even if authentication is configured with LDAP, the system would still need to be authenticated against local (/etc/passwd); incase of network failure? Two: I can log... (0 Replies)
Discussion started by: rsheikh
0 Replies

7. Solaris

Printer confusion

I have printer old about 5 year , but I can not determine which driver to use output of ls -al /usr/share/lib/terminfo/h -rw-r--r-- 1 root bin 961 Jan 22 2005 h1000 -rw-r--r-- 1 root bin 1002 Jan 22 2005 h1420 -rw-r--r-- 1 root bin 1009... (2 Replies)
Discussion started by: solaris_user
2 Replies

8. Programming

C fork Confusion :-?

Hi, I was trying to learn forking in C in UNIX. Somehow i still haven't been able to get the concept well. I mean, i do understand that fork creates an exact replica of the parent (other than the fact that parent gets the process id of the child and child gets 0 when fork is called). This is the... (2 Replies)
Discussion started by: ralpheno
2 Replies

9. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies
Login or Register to Ask a Question