27 #include <utils/ipc/shm.h> 28 #include <utils/ipc/shm_exceptions.h> 34 #include <sys/types.h> 40 #define MAGIC_TOKEN "FawkesShmemQAApp" 43 for ( unsigned int i = 0; i < 50000000; i++) { \ 57 QASharedMemoryHeader(
unsigned int type)
65 QASharedMemoryHeader *qs =
new QASharedMemoryHeader(header.type);
71 const QASharedMemoryHeader *qs =
dynamic_cast<const QASharedMemoryHeader *
>(&s);
72 return (qs && (header.type == qs->header.type));
75 virtual bool matches(
void *memptr)
77 return (memcmp(memptr, &header,
sizeof(qashmem_header_t)) == 0);
82 return sizeof(qashmem_header_t);
90 virtual void initialize(
void *memptr)
92 memcpy(memptr, (
char *)&header,
sizeof(qashmem_header_t));
95 virtual void set(
void *memptr)
97 memcpy((
char *)&header, memptr,
sizeof(qashmem_header_t));
104 virtual size_t data_size()
110 qashmem_header_t header;
117 signal_handler(
int signum)
124 do_child(
unsigned int child_id, QASharedMemoryHeader *header)
126 cout <<
"Child " << child_id <<
" is alive" << endl;
136 int *mc = (
int *)sr->
memptr();
138 cout <<
"Child " << child_id <<
" entering loop" << endl;
148 cout <<
"Child " << child_id <<
": locking (read)" << endl;
150 cout <<
"Child " << child_id <<
": locked (read)" << endl;
154 cout <<
"Child " << child_id <<
": unlocking (read)" << endl;
157 cout <<
"Child " << child_id <<
": locking (write)" << endl;
159 cout <<
"Child " << child_id <<
": locked (write)" << endl;
161 cout <<
"Child " << child_id <<
": unlocking (write)" << endl;
169 cout <<
"Child " << child_id <<
" exiting" << endl;
176 main(
int argc,
char **argv)
179 signal(SIGINT, signal_handler);
181 QASharedMemoryHeader *h1 =
new QASharedMemoryHeader(1);
185 cout <<
"Use the locking/locked comments to verify!" << endl;
188 cout <<
"Creating shared memory segment" << endl;
196 cout <<
"Adding semaphore set for protection" << endl;
207 if ((child_pid = fork()) == 0) {
211 if ((child_pid = fork()) == 0) {
216 cout <<
"Father (Writer) is alive" << endl;
217 int *mf = (
int *)sw->
memptr();
225 cout <<
"Father: locking" << endl;
227 cout <<
"Father: locked" << endl;
233 std::cout <<
"Father: unprotected: " << mf[1] <<
" protected: " << mf[0] << endl;
237 cout <<
"Father: Waiting for child to exit" << endl;
239 waitpid(child_pid, &status, 0);
void lock_for_write()
Lock shared memory segment for writing.
Fawkes library namespace.
void lock_for_read()
Lock shared memory segment for reading.
void * memptr() const
Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared me...
Could not attach to shared memory segment.
void add_semaphore()
Add semaphore to shared memory segment.
void print_trace()
Prints trace to stderr.
void unlock()
Unlock memory.