Sponsored Content
Top Forums Programming sem_wait, sem_post confusion? Post 302582761 by gabam on Sunday 18th of December 2011 04:18:28 AM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
PTHREAD_ATTR_SETDETACHSTATE(3)				     Linux Programmer's Manual				    PTHREAD_ATTR_SETDETACHSTATE(3)

NAME
pthread_attr_setdetachstate, pthread_attr_getdetachstate - set/get detach state attribute in thread attributes object SYNOPSIS
#include <pthread.h> int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate); Compile and link with -pthread. DESCRIPTION
The pthread_attr_setdetachstate() function sets the detach state attribute of the thread attributes object referred to by attr to the value specified in detachstate. The detach state attribute determines whether a thread created using the thread attributes object attr will be created in a joinable or a detached state. The following values may be specified in detachstate: PTHREAD_CREATE_DETACHED Threads that are created using attr will be created in a detached state. PTHREAD_CREATE_JOINABLE Threads that are created using attr will be created in a joinable state. The default setting of the detach state attribute in a newly initialized thread attributes object is PTHREAD_CREATE_JOINABLE. The pthread_attr_getdetachstate() returns the detach state attribute of the thread attributes object attr in the buffer pointed to by detachstate. RETURN VALUE
On success, these functions return 0; on error, they return a nonzero error number. ERRORS
pthread_attr_setdetachstate() can fail with the following error: EINVAL An invalid value was specified in detachstate. CONFORMING TO
POSIX.1-2001. NOTES
See pthread_create(3) for more details on detached and joinable threads. A thread that is created in a joinable state should eventually either be joined using pthread_join(3) or detached using pthread_detach(3); see pthread_create(3). It is an error to specify the thread ID of a thread that was created in a detached state in a later call to pthread_detach(3) or pthread_join(3). EXAMPLE
See pthread_attr_init(3). SEE ALSO
pthread_attr_init(3), pthread_create(3), pthread_detach(3), pthread_join(3), pthreads(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2010-02-03 PTHREAD_ATTR_SETDETACHSTATE(3)
All times are GMT -4. The time now is 04:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy