pb with semaphore
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > pb with semaphore




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    pb with semaphore  
naourez


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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





[ Post a follow-up to this message ]



    Re: pb with semaphore  
Michael Kerrisk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-04 01:01 PM

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






[ Post a follow-up to this message ]



    Re: pb with semaphore  
naourez


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-24-04 08: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:32 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register