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) :
J'ai les erreurs suivante :
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; };
Je n'arrive pas à comprendre ses erreurs, un problème d'include? Avez vous une idée?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";
Partager