IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Boost C++ Discussion :

Interprocess : déclaration passage de son propre type


Sujet :

Boost C++

  1. #1
    Membre actif Avatar de Masmeta
    Homme Profil pro
    Ing. R&D informatique industrielle
    Inscrit en
    Mai 2006
    Messages
    472
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Ing. R&D informatique industrielle
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2006
    Messages : 472
    Points : 221
    Points
    221
    Par défaut Interprocess : déclaration passage de son propre type
    Bonjour,
    Je cherche à comprendre le principe de IPC sous Linux. J'utilise pour cela la librairie Boost avec interprocess.
    J'ai réussi à faire 1 exo où je calcule des racines carrés à travers une mémoire partagées. Je me suis basé sur cette exemple pour pouvoir transférer un peu plus qu'un float.
    Voici le code du process 1

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    #include <boost/interprocess/sync/interprocess_semaphore.hpp>
    #include <boost/interprocess/shared_memory_object.hpp>
    #include <boost/interprocess/mapped_region.hpp>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <time.h>
    #include "semaphore_shared_data.hpp"
     
    using namespace boost::interprocess;
     
     
    int main(){
     
     
      //utiliser la mémoire partagé si existante sinon la créer, le tout lecture/écriture
      shared_memory_object shm(open_or_create, "shared_memory", read_write);
      //réserve la taille mémoire 
      shm.truncate(sizeof(shared_memory_buffer));
      //Vérifie la mémoire partagé pour le processus
      mapped_region region(shm,read_write);
     
      //fourni l'adresse de la région
      void *addr = region.get_address();
     
     
      //crée le buffer de mémoire
      shared_memory_buffer *data = new (addr) shared_memory_buffer;
     
     
      while(true){
     
        //En attente d'une écriture
        data->writer.wait();
     
             //lit l'écriture 
          std::cout << "Process 1 read: " << data->Variable.Type << ":" <<    data->Variable.Value << std::endl;
     
          //Affecte les données
          data->Variable.Type = "ACK";
          data->Variable.Value = 1;
     
     
        sleep(1);
     
        //écris dans la mémoire
        data->reader.post();
     
      }
     
     
      return 0;
     
    }

    Voici le code du process 2
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
     
    #include <boost/interprocess/shared_memory_object.hpp>
    #include <boost/interprocess/mapped_region.hpp>
    #include <iostream>
    #include <boost/interprocess/sync/interprocess_semaphore.hpp>
    #include <math.h>
    #include <string>
    #include "semaphore_shared_data.hpp"
     
    using namespace boost::interprocess;
     
     
    int main (){
     
       //créer l'objet mémoire partagée.
       shared_memory_object shm(open_only, "shared_memory",read_write);
     
      //Vérifie la mémoire partagé pour le processus
       mapped_region region(shm,read_write);
     
      //Donne l'adress
       void *addr = region.get_address();
      //Obtientn la structure partagée
       shared_memory_buffer *data = static_cast<shared_memory_buffer*>(addr);
     
     
       while(true)
      {
          //Attent une lecture 
          data->reader.wait();
     
          //lit la valeur lu
          std::cout << "Process 2 read: " << data->Variable.Type << ":" <<    data->Variable.Value << std::endl;
     
          //Affecte une valeur
          data->Variable.Type = "ACK";
          data->Variable.Value = 2;
     
          //Ecrit la valeur
          data->writer.post();
       }
     
       return 0;
     
    }

    Code de ma structure partagée (semaphore_shared_data.hpp) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    #include <boost/interprocess/sync/interprocess_semaphore.hpp>
    #include <string>
    using namespace boost::interprocess;
     
    typedef allocator<char, managed_shared_memory::segment_manager>  CharAllocator;
    typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;
     
    struct shared_Struct
    {
    	shared_Struct():Type("ACK"), Value(0){}
    	shared_string Type;
    	float Value;
     
    };
     
     
    struct shared_memory_buffer 
    {
     
    	//writer initialized with one to start
     
      	//reader have to wait
      	shared_memory_buffer(): writer(1), reader(0), Variable(){}
    	interprocess_semaphore writer, reader;
     
    	shared_Struct Variable;
     
    };
    J'ai les erreurs suivante :
    semaphore_shared_data.hpp:8:25: error: incomplete type ‘boost::interprocess::managed_shared_memory {aka boost::interprocess::basic_managed_shared_memory<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>}’ used in nested name specifier
    typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
    ^
    semaphore_shared_data.hpp:8:25: error: incomplete type ‘boost::interprocess::managed_shared_memory {aka boost::interprocess::basic_managed_shared_memory<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>}’ used in nested name specifier
    semaphore_shared_data.hpp:8:63: error: template argument 2 is invalid
    typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
    ^
    semaphore_shared_data.hpp:8:79: error: invalid type in declaration before ‘;’ token
    typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
    ^
    semaphore_shared_data.hpp:9:9: error: ‘basic_string’ does not name a type
    typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;
    ^
    semaphore_shared_data.hpp:14:2: error: ‘shared_string’ does not name a type
    shared_string Type;
    ^
    semaphore_shared_data.hpp: In constructor ‘shared_Struct::shared_Struct()’:
    semaphore_shared_data.hpp:13:18: error: class ‘shared_Struct’ does not have any field named ‘Type’
    shared_Struct():Type("ACK"), Value(0){}
    ^
    process1.cpp: In function ‘int main()’:
    process1.cpp:38:57: error: ‘struct shared_Struct’ has no member named ‘Type’
    std::cout << "Process 1 read: " << data->Variable.Type << ":" << data->Variable.Value << std::endl;
    ^
    process1.cpp:41:22: error: ‘struct shared_Struct’ has no member named ‘Type’
    data->Variable.Type = "ACK";
    Je n'arrive pas à comprendre ses erreurs, un problème d'include? Avez vous une idée?
    "Nulla Tenaci Invia Est Via"
    Aux persévérants aucune route n'est interdite

  2. #2
    Membre expert
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2011
    Messages
    739
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2011
    Messages : 739
    Points : 3 627
    Points
    3 627
    Par défaut
    Il manque #include <boost/interprocess/managed_shared_memory.hpp>.
    Après il y a une autre erreur concernant l'allocateur qui n'a pas de paramètre par défaut. Et visiblement en interne, dans std::basic_string, certaines fonctions créées un allocateur sans paramètre. J'ai bien peur que ça ne puisse jamais compiler. Au pire il faut envisager un autre conteneur (std::vector ?).

    Par contre, attention avec les using namespace boost::interprocess / std, tu risques d'avoir des conflits de noms (allocator par exemple).

  3. #3
    Membre actif Avatar de Masmeta
    Homme Profil pro
    Ing. R&D informatique industrielle
    Inscrit en
    Mai 2006
    Messages
    472
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Ing. R&D informatique industrielle
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2006
    Messages : 472
    Points : 221
    Points
    221
    Par défaut
    Citation Envoyé par jo_link_noir Voir le message
    Il manque #include <boost/interprocess/managed_shared_memory.hpp>. .
    Oui je l'ai vu mais cela n'a pas changé grand chose

    Citation Envoyé par jo_link_noir Voir le message
    Après il y a une autre erreur concernant l'allocateur qui n'a pas de paramètre par défaut. Et visiblement en interne, dans std::basic_string, certaines fonctions créées un allocateur sans paramètre. [...]
    Par contre, attention avec les using namespace boost::interprocess / std, tu risques d'avoir des conflits de noms (allocator par exemple).
    Je ne comprend pas ce que tu veux dire.
    "Nulla Tenaci Invia Est Via"
    Aux persévérants aucune route n'est interdite

  4. #4
    Membre actif Avatar de Masmeta
    Homme Profil pro
    Ing. R&D informatique industrielle
    Inscrit en
    Mai 2006
    Messages
    472
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Ing. R&D informatique industrielle
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2006
    Messages : 472
    Points : 221
    Points
    221
    Par défaut
    A force de regarder les principes dans des forums , on ne va pas à l'essentiel.

    J'ai modifié ma structure de la façon suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    #include <boost/interprocess/containers/string.hpp>
     
    struct shared_Struct
    {
    	shared_Struct():type("ACK"), value(0){}
    	boost::interprocess::string type;
    	float value;
     
    };
    Le programme fonctionne.
    "Nulla Tenaci Invia Est Via"
    Aux persévérants aucune route n'est interdite

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Créer son propre type de sauvegrade
    Par Ninored dans le forum C++
    Réponses: 17
    Dernier message: 18/11/2013, 10h52
  2. Réponses: 15
    Dernier message: 04/01/2007, 11h15
  3. créer son propre protocole
    Par matthew_a_peri dans le forum Développement
    Réponses: 11
    Dernier message: 04/03/2005, 14h16
  4. Création de son propre message dans un formulaire
    Par androme dans le forum C++Builder
    Réponses: 17
    Dernier message: 06/02/2005, 23h13
  5. [C#] Ajouter son propre composant dans Design de VS.Net
    Par yannick_sch dans le forum Windows Forms
    Réponses: 2
    Dernier message: 26/08/2004, 11h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo