Single Semaphore problem???


 
Thread Tools Search this Thread
Top Forums Programming Single Semaphore problem???
# 1  
Old 01-10-2012
Single Semaphore problem???

Hi friends,
I have this very famous Operating System book titiled, "Operating System Concepts" by Abaraham Silbertschatz 7th edition.
Regarding the semaphores, here is a paragraph from the book which says,

We can also use semaphores to solve various synchronization problems. For example, consider two concurrently running proceses: P1 with a statement S1 and P2 with a statement S2. Suppose we require that S2 be executed only after S1 has completed. We can implement this cheme readily by letting P1 and P2 share a common semaphore sync, initialized to 0, and by inserting the statements

Code:
 
S1:
signal(synch);

in process P1, and the statements

Code:
 
wait(synch);
S2;

in process P2. Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked signal(synch), which is after S1 has been executed.
Now according to the pseudocode that has been provided, I have written this small program, but it fails. You can have a look at it.


Code:
 
$ cat practise2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/sem.h>
int count = 0;
int pargc;
char **pargv;
sem_t synch;
void *thread1(void *);
void *thread2(void *);
int main(int argc, char *argv[])
{
        pargc = argc;
        pargv = argv;
        if(argc != 3)
        {
                printf("Invalid no. of arguments!\n");
                exit(-1);
        }
        else
        {
        sem_init(&synch,count,0);
        pthread_t tid1;
        pthread_t tid2;
        int t1 = 1;
        int t2 = 2;
        pthread_attr_t attr1;
        pthread_attr_t attr2;
        pthread_attr_init(&attr1);
        pthread_attr_init(&attr2);
        {
        pthread_create(&tid1,&attr1,thread1,(void *)t1);
        pthread_create(&tid2,&attr2,thread2,(void *)t2);
        }
        {
        pthread_join(tid1,NULL);
        pthread_join(tid2,NULL);
        }
        printf("\nThreads finished!\n");
        }
        return 0;
}
void *thread1(void *n)
{
        int i;
        for(i = 0;i<5;i++)
        {
        printf("0\n");
        sem_post(&synch);
        }
        pthread_exit(0);
}
void *thread2(void *n)
{
        int i;
        for(i = 0;i<5;i++)
        {
        sem_wait(&synch);
        printf("1\n");
        }
        pthread_exit(0);
}

the output should be like this

Code:
 
0
1
0
1
0
1
0
1
0
1

but it fails and gives the output

Code:
 
0
0
0
0
0
1
1
1
1
1

Waiting for your wonderful replies!
# 2  
Old 01-11-2012
The code you implemented only ensures thread 2 writes to stdout after thread 1 has posted to the semaphore at least once. It doesn't restrict thread 1 from posting to the semaphore multiple times before thread 2 does anything.
This User Gave Thanks to achenle For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Single semare critical region problem???

Hi guys, I hope everybody is doing fine. I have written this small program which solves the critical region problem. Only on of the two threads can make changes to a common variable called counter. I am using two semaphores, is it possible to write the same program using only one semaphore? Here... (0 Replies)
Discussion started by: gabam
0 Replies

2. Programming

semaphore problem???

Hi friends, I want to print the the following sequence of numbers using sempahores. 2 3 2 3 2 3 2 3 But something seems to be wrong regarding my concept of semaphores, I am using a single function, a single semaphore, and two threads, but I am not succeeding to achieve my goal, it prints as... (1 Reply)
Discussion started by: gabam
1 Replies

3. Shell Programming and Scripting

remsh problem with single quote (')

Hi All, Im executing the shell script remotely. here is one statement from that: remsh $rHost -l $rUser "java -jar $TARGET/toolkit/apps/bin/toolkit-stm.jar network -m -d1 /abmusr06/abm/users/dywrk01/$package/DYP_Execution/data/configuration/network_aus/network -u2... (1 Reply)
Discussion started by: AB10
1 Replies

4. Shell Programming and Scripting

Problem renaming a file with single quotes

Hi, I am trying to create a script which validates the incoming source files. The script has File name Pattern as Argument. The First part of the script validates if there are any files available if then echo "\n Files are available to process \n" else echo "\n File does not... (9 Replies)
Discussion started by: dsshishya
9 Replies

5. UNIX for Advanced & Expert Users

Problem: Single Sign On for linux

Hi gurus, I'd like to know your opions about Single Sign On (SSO) for linux (Debian). In my company, clients want to access to different services (FTP, HTTP, Mail, Web Applications ). I think about OpenLDAP and Proxy (Squid, Vulture) to resolve this problem but i'm not sure if they can. Are there... (0 Replies)
Discussion started by: thanhdat
0 Replies

6. Shell Programming and Scripting

single quote problem with rsync

Hi everybody, I'm a newbie and hope that someone help me in this problem. I have a filename in LINUX with single quote like this: abs@hosttest:~/ABS/BETY/cygdrive/C/DECLARANOT 1.1.4/02 - ROCK/052 - GUNSROSES> dir You* -rw-r--r-- 1 abs users 2365881 2008-08-25 09:16 You're Crazy.mp3 ... (9 Replies)
Discussion started by: mr_boysito
9 Replies

7. Shell Programming and Scripting

facing problem in getting output on single line

i am using these two grep commands grep "^tipo" dump3_out.dat|awk -F"," '{for(i=1;i<=NF;i++){A=$i;B=substr($i, index($i, "=")+1);{print B }}}'|paste -s -d"||\n"|sed -e 's/^ //g' -e 's/| /|/g' >> result.dat grep -n "^ind" dump3_out.dat|cut -d "=" -f2|cut -d "," -f2|cut -d "n" -f1|tr -d '^ ' >>... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies

8. UNIX for Advanced & Expert Users

Semaphore problem....

I'm having an issue implementing my semaphores.... The following is how I'm setting up the semaphore. First: I get the semkey (which I've wrapped in an IF statement using perror() but in an attempt to keep the code clutter free I've removed it here) semkey = ftok("./request", 'S' ) ... (1 Reply)
Discussion started by: Dreams in Blue
1 Replies

9. Shell Programming and Scripting

problem with single quotes in a string and findbug

I'm having trouble manipulating a string that contains single quotes (') in it. I'm writing a ksh script to parse in a few queries from a config file, such as this: findbug \(\(Project 'in' "Deployment,HDRCI,LHS,LSS,WUCI" '&&' Status 'in' "N" '&&' New_on 'lessthan' "070107" \)\) '&&' \(Class... (9 Replies)
Discussion started by: bob122480
9 Replies

10. Programming

Problem with releasing semaphore lock

Hi, I am trying to write stuff to a shared memory using a writer, and reading the corresponding stuff using a reader. I am facing problems while releasing the lock, as a result of which I am having segmentation faults. The code is as follows... /********** writer.c ***********/ ... (1 Reply)
Discussion started by: jacques83
1 Replies
Login or Register to Ask a Question