27 #include <sys/types.h>
29 #include <utils/ipc/shm.h>
30 #include <utils/ipc/shm_exceptions.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);
72 const QASharedMemoryHeader *qs =
dynamic_cast<const QASharedMemoryHeader *
>(&s);
73 return (qs && (header.type == qs->header.type));
79 return (memcmp(memptr, &header,
sizeof(qashmem_header_t)) == 0);
85 return sizeof(qashmem_header_t);
95 initialize(
void *memptr)
97 memcpy(memptr, (
char *)&header,
sizeof(qashmem_header_t));
103 memcpy((
char *)&header, memptr,
sizeof(qashmem_header_t));
118 qashmem_header_t header;
124 signal_handler(
int signum)
130 do_child(
unsigned int child_id, QASharedMemoryHeader *header)
132 cout <<
"Child " << child_id <<
" is alive" << endl;
142 int *mc = (
int *)sr->
memptr();
144 cout <<
"Child " << child_id <<
" entering loop" << endl;
155 cout <<
"Child " << child_id <<
": locking (read)" << endl;
157 cout <<
"Child " << child_id <<
": locked (read)" << endl;
162 cout <<
"Child " << child_id <<
": unlocking (read)" << endl;
165 cout <<
"Child " << child_id <<
": locking (write)" << endl;
167 cout <<
"Child " << child_id <<
": locked (write)" << endl;
169 cout <<
"Child " << child_id <<
": unlocking (write)" << endl;
177 cout <<
"Child " << child_id <<
" exiting" << endl;
183 main(
int argc,
char **argv)
186 signal(SIGINT, signal_handler);
188 QASharedMemoryHeader *h1 =
new QASharedMemoryHeader(1);
192 cout <<
"Use the locking/locked comments to verify!" << endl;
195 cout <<
"Creating shared memory segment" << endl;
204 cout <<
"Adding semaphore set for protection" << endl;
214 if ((child_pid = fork()) == 0) {
218 if ((child_pid = fork()) == 0) {
223 cout <<
"Father (Writer) is alive" << endl;
224 int *mf = (
int *)sw->
memptr();
233 cout <<
"Father: locking" << endl;
235 cout <<
"Father: locked" << endl;
242 std::cout <<
"Father: unprotected: " << mf[1] <<
" protected: " << mf[0] << endl;
246 cout <<
"Father: Waiting for child to exit" << endl;
248 waitpid(child_pid, &status, 0);