Problem with semaphores


 
Thread Tools Search this Thread
Top Forums Programming Problem with semaphores
# 1  
Old 08-27-2010
Data Problem with semaphores

Hello,
I was doing an exercise of semaphores and shared memory, namely the barbers:
-B number of barbers
-S number of chairs
-C number of customers.
I have done already and I compiled the code, but when run I get an error segment. Can not be and it took several days.
If anyone sees the error and I indicated I would make a great favor.
Thanks
Quote:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <string.h>

char MUTEX[128]="semaforo.txt";
int MAX_CADENA=128;
int i;
int num_barberos;
int num_sillas;
int num_clientes;
int *pid_barberos;
int cespera;
int *clientes_espera;
char menasaje[64];
int CLIENTES;
int BARBEROS;

void mostarMensaje(char *quien, char *que);

void Pb(char *nombre) {
int df;
while((df=open(nombre, O_CREAT | O_EXCL, 0777))==-1);
close(df);
}

void Vb(char *nombre) {
unlink(nombre);
}

int semcall(int semaforo, int operacion);
i
nt inicia(int valor) {
int id;
union semun {
int val;
struct semid_ds *buf;
ushort *array;
} arg;
if((id=semget(IPC_PRIVATE, 1, (IPC_CREAT|0666)))==-1) {
perror("Error al crear el semforo.");
return -1;
}
arg.val=valor;
if (semctl(id,0,SETVAL,arg)== -1) {
perror("Error al inicializar el semaforo.");
return -1;
}
return id;
}

void P(int semaforo) {
if(semcall(semaforo,-1)==-1) {
perror("Error en operacin P.");
}
}

void V(int semaforo) {
if(semcall(semaforo,1)==-1) {
perror("Error en operacin V.");
}
}

int semcall(int semaforo, int operacion) {
struct sembuf sb;
sb.sem_num=0;
sb.sem_op=operacion;
sb.sem_flg=0;
return semop(semaforo,&sb,1);
}

void borra_s(int semaforo) {
if(semctl(semaforo,0,IPC_RMID,0)==-1) {
printf("Error al eliminar semaforo.\n");
}
}

void mostarMensaje(char *quien, char *que) {
char mensaje[128];
time_t t=time(NULL);
sprintf(mensaje, "%s pid %u: %s %s",quien, getpid(), que, ctime(&t));
}

void barbero() {
while (1) {
P(CLIENTES);
Pb(MUTEX);
*clientes_espera--;
V(BARBEROS);
Vb(MUTEX);
mostarMensaje("barbaro","afeitando");
sleep(rand()%4+1);
mostarMensaje("barbaro","termina de afeitar");
}
}

void cliente() {
mostarMensaje("cliente","ha llegado");
Pb(MUTEX);
if (*clientes_espera<num_sillas) {
clientes_espera++;
V(CLIENTES);
Vb(MUTEX);
P(BARBEROS);
mostarMensaje("cliente","siendo afeitado");
} else {
Vb(MUTEX);
mostarMensaje("cliente","no atendido");
}
}
 
int main(int argc, char *argv[])
{
int mpk;
char msg[64];
if (argc<7 || argc>7) {
printf("mal pasados los argumentos");
exit(0);
} else {
if (strcmp("-b",argv[2])) {
num_barberos=atoi(argv[3]);
if(strcmp("-s",argv[4])) {
num_sillas=atoi(argv[5]);
num_clientes=atoi(argv[7]);
} else {
num_sillas=atoi(argv[7]);
num_clientes=atoi(argv[5]);
}
} else if (strcmp("-s",argv[2])) {
num_sillas=atoi(argv[3]);
if(strcmp("-b",argv[4])) {
num_barberos=atoi(argv[5]);
num_clientes=atoi(argv[7]);
} else {
num_barberos=atoi(argv[7]);
num_clientes=atoi(argv[5]);
}
} else {
num_clientes=atoi(argv[3]);
if(strcmp("-b",argv[4])) {
num_barberos=atoi(argv[5]);
num_sillas=atoi(argv[7]);
} else {
num_barberos=atoi(argv[7]);
num_sillas=atoi(argv[5]);
}
}
}
pid_barberos=(int *) malloc(sizeof(int)*num_barberos);
if((cespera=shmget(IPC_PRIVATE,sizeof(int)*1,0660))==-1) {
printf("Error en acceso memoria compartida.");
exit(-1);
}
clientes_espera=(int *) shmat(cespera, (char *) 0, 0);
*clientes_espera=0;
CLIENTES=inicia(0);
BARBEROS=inicia(1);
for(i=0;i<num_barberos; i++) {
switch(pid_barberos[i]=fork()) {
case -1: printf("error fork barbero nmero %d\n",i);exit(1);
case 0: barbero(); exit(0);
}
}
for(i=0;i<num_clientes; i++) {
switch(fork()) {
case -1: printf("eror fork clienet nmero %d\n",i); exit(1);
case 0: srand(getpid()); sleep(rand()%7+1); cliente(); exit(0);
}
}
for(i=0;i<num_clientes; i++) {
sprintf(msg, "termino el hijo %u\n", wait((int *) NULL));
write(STDOUT_FILENO, msg, strlen(msg));
}
for(i=0; i<num_barberos; i++) {
mpk=kill(pid_barberos[i],SIGKILL);
}
return EXIT_SUCCESS;
}

Last edited by ciudadwifi; 08-27-2010 at 08:12 AM..
# 2  
Old 08-27-2010
You can tell what line the segmentation fault occurs on by compiling with -ggdb, then running with gdb. i.e.

Code:
gdb
run ./filename
(text informing you the program crashed)
bt f

# 3  
Old 08-28-2010
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. Programming

Semaphores 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... (0 Replies)
Discussion started by: whatevernever
0 Replies

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

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

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

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

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

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

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