|
Home > Archive > Unix Programming > August 2004 > pb with semaphore
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| naourez 2004-08-22, 6:08 pm |
| hi
i am trying to synchronize acces to a shared memory the pb is that in
the first prog it's alright(i create the semaphore ,and can increase
and decrease its value) but in the second one i can't increase or
decrease the value of the semaphore and i have the error permission
denied
here is the function of creation of the semaphore and the ones used to
increase and decrease its value
int sem_create (key_t cle, int initval) {
int semid ;
union semun{
int val ;
struct semid_ds *buf ; /* les deux champs suivants ne sont */
ushort *array; /* pas utilis\'es dans notre situation
nous ne les d\'etaillons donc pas */
} arg_ctl ;
// On commence par cr\'eer le s\'emaphore
semid = semget(ftok("dijkstra.h",cle),1,IPC_CREAT|IPC_EXCL|0666);
if (semid == -1){
semid = semget(ftok("dijkstra.h",cle),1,0666) ;
if (semid == -1) {
perror("Erreur semget()") ;
exit(1) ;
}
}
// Maintenant on affecte une valeur initiale au s\'emaphore
arg_ctl.val = initval ;
if (semctl(semid,0,SETVAL,arg_ctl) == -1){
perror("Erreur lors de l'initialisation du semaphore") ;
exit(1) ;
}
printf("On cr\\'ee un s\\'emaphore dont le num\\'ero est %d\n",semid)
;
return semid ;
}
void down(int semid) {
struct sembuf sempar ;
sempar.sem_num = 0 ;
sempar.sem_op = -1 ;
sempar.sem_flg = 0 ;
if ( semop(semid,&sempar,1) == -1)
perror("Erreur lors du down") ;
}
void up(int semid) {
struct sembuf sempar ;
sempar.sem_num = 0 ;
sempar.sem_op = 1 ;
sempar.sem_flg = 0 ;
if (semop(semid,&sempar,1) ==-1)
perror("Erreur lors du up") ;
}
thanks for your help
| |
| Michael Kerrisk 2004-08-23, 8:01 am |
| Salut,
>i am trying to synchronize acces to a shared memory the pb is that in
>the first prog it's alright(i create the semaphore ,and can increase
>and decrease its value) but in the second one i can't increase or
>decrease the value of the semaphore and i have the error permission
>denied
>here is the function of creation of the semaphore and the ones used to
>increase and decrease its value
I could see no obvious problem in the functions. Some suggestions:
-- Could you post minimal, complete programs that demonstrate
the problem.
-- Likewise a shell session showing the observed error would
be useful.
-- When you inspect the semaphore set with ipcs, what does
it say the permissions on the set are?
-- It probabaly doesn't matter, but it might be good to mention
what implementation you are working on.
Cheers,
Michael
>int sem_create (key_t cle, int initval) {
>
> int semid ;
> union semun{
> int val ;
> struct semid_ds *buf ; /* les deux champs suivants ne sont */
> ushort *array; /* pas utilis'es dans notre situation
> nous ne les d'etaillons donc pas */
> } arg_ctl ;
>
> // On commence par cr'eer le s'emaphore
> semid = semget(ftok("dijkstra.h",cle),1,IPC_CREAT|IPC_EXCL|0666);
> if (semid == -1){
> semid = semget(ftok("dijkstra.h",cle),1,0666) ;
> if (semid == -1) {
> perror("Erreur semget()") ;
> exit(1) ;
> }
> }
> // Maintenant on affecte une valeur initiale au s'emaphore
> arg_ctl.val = initval ;
> if (semctl(semid,0,SETVAL,arg_ctl) == -1){
> perror("Erreur lors de l'initialisation du semaphore") ;
> exit(1) ;
> }
>
> printf("On cr\'ee un s\'emaphore dont le num\'ero est %d\n",semid)
>;
>
> return semid ;
>}
>
>
>void down(int semid) {
>
> struct sembuf sempar ;
>
> sempar.sem_num = 0 ;
> sempar.sem_op = -1 ;
> sempar.sem_flg = 0 ;
>
> if ( semop(semid,&sempar,1) == -1)
> perror("Erreur lors du down") ;
>}
>
>void up(int semid) {
>
> struct sembuf sempar ;
>
> sempar.sem_num = 0 ;
> sempar.sem_op = 1 ;
> sempar.sem_flg = 0 ;
>
> if (semop(semid,&sempar,1) ==-1)
> perror("Erreur lors du up") ;
>}
>
>thanks for your help
| |
| naourez 2004-08-24, 3:18 am |
| //the writer
#include <memoire_include.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 4096 /* make it a 4K shared memory segment */
int main()
{
key_t key,key_flag;
int shmid,id_flag;
char *data,*message;
int mode,*flag,c;
/*int mutex;*/
/* On se fait un semaphore representant le mutex*/
mutex = sem_create(46,1) ;
// entree en section critique
down(mutex) ;
printf("entree en section critique pour ecrire en memoire\n");
/* make the key: */
if ((key_flag = ftok("shm.c", 'A')) == -1) {
perror("ftok");
exit(1);
}
if ((id_flag= shmget(key_flag,sizeof(int), 0644 | IPC_CREAT)) == -1)
{
perror("shmget");
exit(1);
}
flag =(int*) shmat(id_flag, NULL, 0);
if (flag ==(int*) -1) {
perror("shmat");
exit(1);
}
printf("quel est votre choix \n");
scanf("%d",&c);
(*flag)=c;
printf("le drapeau ecrit dans le segment a pour numero:
\"%d\"\n",c);
.....
// here do treatement according the value of drapeau
.....
// sortie de section critique
up(mutex) ;
printf("le process lecteur peut acceder la memoire partagee
\n");
return 0;
}
//the reader
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <memoire_include.h>
#define SHM_SIZE 4096 /* make it a 4K shared memory segment */
int main()
{
key_t key,key_flag;
int shmid,id_flag;
char *data;
int mode,*drapeau;
// entree en section critique
down(mutex) ;
printf("entree en section critique pour lire de la memoire\n");
/* make the key: */
if ((key_flag = ftok("shm.c", 'A')) == -1) {
perror("ftok");
exit(1);
}
if ((id_flag = shmget(key_flag,sizeof(int), 0644 | IPC_CREAT)) == -1)
{
perror("shmget");
exit(1);
}
drapeau =(int*) shmat(id_flag,NULL, 0);
if (drapeau ==(int*) -1) {
perror("shmat");
exit(1);
}
switch((*drapeau))
{
....
//do stuff here
.....
}
if (shmdt((int*)drapeau) == -1) {
perror("shmdt2");
exit(1);
}
// sortie de section critique
up(mutex) ;
printf("le process ecrivain peut acceder la memoire partagee
\n");
return 0;
}
N.B:
mutex is declared static int mutex in the include file
"memoire_include.h"
which contains the functions of creation and manipulation of the
semaphors
the pb is that in the first prog everything works
but in the second i obtain the next errors
Erreur lors du down:permission denied
Erreur lors du up:permission denied
despite of this the second prog can read the shared memory and outputs
the correct value it contains
|
|
|
|
|