Single semare critical region problem???


 
Thread Tools Search this Thread
Top Forums Programming Single semare critical region problem???
# 1  
Old 01-10-2012
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 have a look at my code


Code:
 
$ cat practise.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 sem1;
sem_t sem2;
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(&sem1,count,1);
        sem_init(&sem2,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 = 0;
        while(i < 3)
        {
        sem_wait(&sem1);
        count++;
        printf("Count = %d\n",count);
        sem_post(&sem2);
        i++;
        }
        pthread_exit(0);
}
void *thread2(void *n)
{
        int i = 0;
        while(i<3)
        {
        sem_wait(&sem2);
        count--;
        printf("Count = %d\n",count);
        sem_post(&sem1);
        i++;
        }
        pthread_exit(0);
}

And more thing, which semaphore am i using in this program, is it binary or counting semaphore???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Mean score value by ID over a defined genomic region

Hi, I would like to know how can I get a mean score value by ID over a defined genomic region. Here it is an example: file1 12 100 103 id1 12 110 112 id1 12 200 203 id2 file2 12 100 101 1 12 101 102 0.8 12 102 103 0.7 12 110 111 2.5 12 111 112 2.8 12 200 201 10.1 12 201 202... (7 Replies)
Discussion started by: fadista
7 Replies

2. Programming

Merge two strings by overlapped region

Hello, I am trying to concatenate two strings by merging the overlapped region. E.g. Seq1=ACGTGCCC Seq2=CCCCCGTGTGTGT Seq_merged=ACGTGCCCCCGTGTGTGTFunction strcat(char *dest, char *src) appends the src string to the dest string, ignoring the overlapped parts (prefix of src and suffix of dest).... (30 Replies)
Discussion started by: yifangt
30 Replies

3. Shell Programming and Scripting

Need a command to change the port region

portsuf=25 port=20925 I need to replace 09 with 25 It should be like 22525. Can some please help with command or script. (4 Replies)
Discussion started by: bhas85
4 Replies

4. AIX

Change lv REGION in HDISK1

Dears my rootvg is missed up i can not extend the /opt as soon as i try to extend the Filesystem its give me that there is not enough space . as there any way to change the REGION of the LVs in HDISK1 ? lspv -p hdisk0 hdisk0: PP RANGE STATE REGION LV NAME TYPE ... (8 Replies)
Discussion started by: thecobra151
8 Replies

5. Shell Programming and Scripting

Region between lines

How can I find the regions between specific lines? I have a file which contains lines like this: chr1 0 17388 0 chr1 17388 17444 1 chr1 17444 17599 2 chr1 17599 17601 1 chr1 17601 569791 0 chr1 569791 569795 1 chr1 569795 569808 2 chr1 569808 569890 3 chr1 569890 570047 4 ... (9 Replies)
Discussion started by: linseyr
9 Replies

6. Programming

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,... (1 Reply)
Discussion started by: gabam
1 Replies

7. UNIX for Advanced & Expert Users

Best practice - determining what region you are on

Hello all, I have a question about what you think the best practice is to determine what region you are running on when you have a system setup with a DEV/TEST, QA, and PROD regions running the same scripts in all. So, when you run in DEV, you have a different directory structure, and you... (4 Replies)
Discussion started by: Rediranch
4 Replies

8. Shell Programming and Scripting

Critical problem in merging lines

Hi all, I am having a very critical problem in merging lines in my file as, let my file contents are: cat test1.txt name1....... address1....... phone1...... <blank> name2...... address2..... phone2..... <blank> and so on. Now i have to merge these lines by which my desired output... (2 Replies)
Discussion started by: shadow25
2 Replies

9. Solaris

How can i take private region backup in veritas

Hello experts, I am using Veritas Volume Manager 5.0. How can i take private region backup and restoration. thanks in advance... (3 Replies)
Discussion started by: younus_syed
3 Replies

10. UNIX for Advanced & Expert Users

stack region

how can i determine that what percentage of stack region is currently is used? (i am using tru64 unix) (2 Replies)
Discussion started by: yakari
2 Replies
Login or Register to Ask a Question