Semaphores Urgent


 
Thread Tools Search this Thread
Top Forums Programming Semaphores Urgent
# 1  
Old 05-06-2010
Semaphores Urgent

Hello,
Iam trying to implement the sleeping barber problem using semaphores and running on UNIX machine. Iam linking it to the thread libraries :
bash-2.03$ g++ sleepingBarber.cpp -lpthread -o sleeping

but when i execute it i get the following error:
bash-2.03$ sleeping
Starting Program
Segmentation Fault (core dumped)
:



Please Help. Here is the code:

Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<synch.h>
#include <pthread.h> 
#include<iostream>
#include<pthread.h>

const int totalChairs = 15;
int waiting = 0;
int finished = 0;
//Binary Semaphores   - mutex,barber
//Counting Semaphores - customers, cutting
struct semaphores
{
    sema_t mutex, barber, customers, cutting;
};
static semaphores * Semaphores;
void cut_hair()
{
    //P(cutting)
    sema_wait(&(Semaphores->cutting));
}
void get_haircut(int custNum)
{
    //V(cutting)
    cout<<"[b] BARBER CUTTING HAIR OF CUSTOMER "<<custNum<<endl;
    sema_post(&(Semaphores->cutting));
    sleep(3);
}
void *Barber(void* ignore)
{
    while(!finished)
    {
        sema_wait(&(Semaphores->customers)); //Sleep if no customers P(Customers)
        cout<<"[b] Barber Sleeping!"<<endl;
            sema_wait(&(Semaphores->mutex));        //Get access to waiting V(mutex)
            waiting--;                //Choose a customer
            sema_post(&(Semaphores->barber));    //barber busy V(barber)
            sema_post(&(Semaphores->mutex));        //Release access to waiting V(mutex)
            sleep(3);
            cut_hair();
        
    }

}
void *Customer(void *num) 
{ 
    int custNum = *(int *)num; 
    
    cout<<"Customer "<<custNum<<" Arrives"<<endl;

    sema_wait(&(Semaphores->mutex));            //Enter Critical Section P(mutex)
    
    if(waiting < totalChairs)
    {
        waiting++;
        sema_post(&(Semaphores->customers)); //Wake up barber V(Customers)
        sema_post(&(Semaphores->mutex));        //Release access to waiting V(mutex)
        sema_wait(&(Semaphores->barber));    //Wait if barber busy P(barber)
        get_haircut(custNum);                        //Get in chair be serviced
        cout<<"[c] Customer "<<custNum<<" got a haircut"<<endl;
        exit(1);
    }
    else
    {
        cout<<"[c] Customer "<<custNum<<" Returns as shop is full"<<endl;
        sema_post(&(Semaphores->mutex));        //Release access to waiting V(mutex)
        exit(1);
    }
    
}
void initializeSemaphores()
{
    sema_init(&(Semaphores->mutex),1,USYNC_PROCESS,NULL);
    sema_init(&(Semaphores->barber),0,USYNC_PROCESS,NULL);
    sema_init(&(Semaphores->customers),0,USYNC_PROCESS,NULL);
    sema_init(&(Semaphores->cutting),0,USYNC_PROCESS,NULL);
}
int main()
{
    
    cout<<"Starting Program"<<endl;
    //Initialize the semaphores
    initializeSemaphores();

    pthread_t barberThread,customerThread[totalChairs];
    pthread_create(&barberThread, NULL, Barber, NULL); 
    for (int i=0; i<totalChairs; i++) 
    { 
        pthread_create(&customerThread[i], NULL, Customer, (void *)&i); 
    } 
    for (int i=0; i<totalChairs; i++) 
    { 
        pthread_join(customerThread[i],NULL); 
    } 
    pthread_join(barberThread,NULL);
    finished = 1; 
    sema_post(&(Semaphores->barber)); // Wake the barber so he will exit. 
    pthread_join(barberThread,NULL);

    
}

Really appreciate it.
# 2  
Old 05-06-2010
Code:
$ gdb a.out
...
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0xff3b0878 in memset () from /usr/platform/SUNW,Sun-Fire-V240/lib/libc_psr.so.1
(gdb) bt
#0  0xff3b0878 in memset () from /usr/platform/SUNW,Sun-Fire-V240/lib/libc_psr.so.1
#1  0xff1de7d4 in sema_init () from /usr/lib/libthread.so.1
#2  0x0003db28 in initializeSemaphores () at t.cxx:82
#3  0x0003dbe4 in main () at t.cxx:92
(gdb) list
81      {
82          sema_init(&(Semaphores->mutex),1,USYNC_PROCESS,NULL);
83          sema_init(&(Semaphores->barber),0,USYNC_PROCESS,NULL);

(gdb) p Semaphores
$1 = (semaphores *) 0x0
(gdb) quit

# 3  
Old 05-07-2010
Thank you for reviewing it binlib,
But iam not able to get the error. Could you please elaborate.
# 4  
Old 05-07-2010
"semaphores" is a null pointer. You never allocate any memory for it.
# 5  
Old 05-07-2010
Yes i figured that one out. thank you though
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Semaphores Program in C

Hi everybody, :o I hope you can help me to solve this problem I have. I'm trying to write a program with Linux in C which uses semaphores and shared memory. In particular I need to create a leader process and three more processes "calculators" (1, 2 and 3) that make mathematical operations.... (4 Replies)
Discussion started by: IgorGest
4 Replies

2. Solaris

Semaphores

Hi, Can somebody please explain me what semaphores are? there purpose? and there effects? Thanks in advance:) (0 Replies)
Discussion started by: Laxxi
0 Replies

3. Programming

about Semaphores

Hello Everybody, I am building a server. this server contains some data. Clients may modify this data or read this data. If a client is reading the data and at the same time another client is modifying the data then at this case the reading client may read some false data (some old mixed with... (1 Reply)
Discussion started by: Omar_Mokhtar
1 Replies

4. UNIX for Dummies Questions & Answers

semaphores

I am having problem with semaphores. I am trying to protect line where process prints so that every process with print in proper order.This is the code.. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/types.h> union... (3 Replies)
Discussion started by: joker40
3 Replies

5. UNIX for Advanced & Expert Users

How many semaphores?

Hello, first of all I apologize if this thread is not in the correct section of this forum, but this one just seemed the most appropriate. The question I have does not concern Unix specifically, it applies to virtually any OS, however it is in Unix where I learned about this problem. So, the... (8 Replies)
Discussion started by: Watto86
8 Replies

6. Programming

semaphores using up and down

been searching around on how to use an up and down function with semaphores but i can't find an example. i looked into using: "semop" but i have no idea how to use it. I have been able to declared the semaphores using semget and initializing them using semctl. (7 Replies)
Discussion started by: ddx08
7 Replies

7. Shell Programming and Scripting

semaphores

Hi Friends, If i execute this command it comes back with 300 lines: ipcs|grep cerebrus >>> i would like to clear the semaphores but ipcrm can remove one id at a time. is there a quicker way of removing semaphores maybe using awk? Regards, (1 Reply)
Discussion started by: kekanap
1 Replies

8. Programming

semaphores

Hi there, Could someone please confirm which POSIX semaphore routines should be used for a multiprocess (and not multithreaded) environment? sys/sem.h definitely works. but the routines, semget, semctl, semop are pretty unwieldy. So, I am looking for an easier way out. From the man pages... (2 Replies)
Discussion started by: qntmteleporter
2 Replies

9. UNIX for Dummies Questions & Answers

Semaphores

Hi all, I am using HP 10.20 on A 9000/785. My question is: If I am the only person logged in as root at the moment, how many "semaphore proccesses" should I have?? Is it only one, or it is relevant to other system proccesses? Here is what I get listing the current semaphores # ipcs -sp... (1 Reply)
Discussion started by: guest100
1 Replies

10. Programming

Semaphores

Dear Reader, I'm in a multiprocess environment working with shared mem and semaphores as mutex.. The problem is -- If one of the process hooked up with the semaphore and accessing the shared mem, terminates abruptly ( or got killed ), other process which are in want of the semaphore are... (1 Reply)
Discussion started by: joseph_shibu
1 Replies
Login or Register to Ask a Question