Προς το περιεχόμενο

Σημαφόροι σε client/server...


delfini69

Προτεινόμενες αναρτήσεις

Δημοσ.

Καλησπέρα,

Έχω έναν κώδικα client/server και θέλω να χρησιμοποιήσω σημαφόρο για την προσπέλαση της μνήμης..Ο κώδικας όπου δημιουργώ τον σημαφόρο είναι ο ακόλουθος:

 

sem_t *my_sem; /*Dimiourgia simaforou*/

my_sem = sem_open(SEM_NAME, O_CREAT | O_RDWR,S_IRUSR | S_IWUSR, 1);

if (my_sem== SEM_FAILED) {

printf("Could not open semaphore!\n");

exit(0);}

 

Το πρόβλημά μου είναι ότι η συνθήκη, my_sem== SEM_FAILED, είναι πάντα αληθής και έτσι το πρόγραμμα κάνει exit....Ξέρει κανείς τι μπορεί αν φταίει;;

Ευχαριστώ εκ των προτέρων...Κάθε βοήθεια ευπρόσδεκτη!

Δημοσ.

Παιδία αν έχει κάποιος κάποια ειδέα ας γράψει μια απάντηση plz...

Είμαι αρχάριος, και έχω πελαγώσει...

Ευχαριστώ

Δημοσ.

Από http://opengroup.org/onlinepubs/007908799/xsh/sem_open.html

 

>
ERRORS

   If any of the following conditions occur, the sem_open() function will return SEM_FAILED and set errno to the corresponding value:

   [EACCES]
       The named semaphore exists and the permissions specified by oflag are denied, or the named semaphore does not exist and permission to create the named semaphore is denied. 
   [EEXIST]
       O_CREAT and O_EXCL are set and the named semaphore already exists. 
   [EINTR]
       The sem_open() operation was interrupted by a signal. 
   [EINVAL]
       The sem_open() operation is not supported for the given name, or O_CREAT was specified in oflag and value was greater than SEM_VALUE_MAX. 
   [EMFILE]
       Too many semaphore descriptors or file descriptors are currently in use by this process. 
   [ENAMETOOLONG]
       The length of the name string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX while _POSIX_NO_TRUNC is in effect. 
   [ENFILE]
       Too many semaphores are currently open in the system. 
   [ENOENT]
       O_CREAT is not set and the named semaphore does not exist. 
   [ENOSPC]
       There is insufficient space for the creation of the new named semaphore. 
   [ENOSYS]
       The function sem_open() is not supported by this implementation. 

 

Κάνε έλεγχο για τα παραπάνω, λογικά θα δεις γιατί σου χτυπάει.

Δημοσ.

κάτω από την printf("Could not open semaphore!\n"); έβαλα perror(""); για να μου εμφανίσει το λάθος..Και αφτό βγάζει "permission denied"..

Γενικά ο κώδικάς μου έχει την εξής μορφή:

 

...

#define SEM_NAME "my_sem" /*Orismos onomatos simaforou*/

...

...

 

main(){

...

...

sem_t *my_sem; /*Dimiourgia simaforou*/

my_sem = sem_open("/my_sem", O_CREAT | O_RDWR,S_IRUSR | S_IWUSR, 1);

if (my_sem== SEM_FAILED) {

printf("Could not open semaphore!\n");

perror("");

exit(1);

}

...

...

for{

...

sem_unlink(my_sem);

}

sem_close(my_sem);

}

 

Αν κάποιος ξέρει γιατί δεν μπορεί να ανοίξει ο σημαφόρος ας γράψει μια απάντηση....:-)

Ευχαριστώ...

Δημοσ.

>
if (my_sem== SEM_FAILED) {
  switch (errno) {
     case EACCES: printf("this"); break;
     case EEXIST: printf("that"); break;
     ...
     ...
     ...
     case ENOSYS: printf("zomg"); break;
  }
  exit(1);
}

 

Δες από πάνω για τις υπόλοιπες περιπτώσεις λάθους.

Δημοσ.

Φίλε Βιλλμαν σε ευχαριστώ!

Το πρόβλημα χτυπούσε στο "EACCES", άλλαξα το όνομα του σημαφόρου αλλά...........τώρα το πρόγραμμα κολλάει όλο απο την αρχή, δεν μπαίνει στην main()..............!!

Ξέρεις τι άλλο πρέπει να αλλάξω;;;;;

Ευχαριστώ και πάλι, περιμένω απάντηση...!

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...