summaryrefslogtreecommitdiff
path: root/demo/conclusion.c
blob: 3807efd787f112d5671ffd9105923c4d01f7df22 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h> 
#include <errno.h> 
#include <unistd.h> 
#include <string.h> 
#include <time.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "md5.h"
#include "utils.h"

static const char MY_NAME[] = "Mrs. Conclusion";

// Set up a Mrs. Premise & Mrs. Conclusion conversation.

int main() { 
    sem_t *the_semaphore = NULL;
    int rc;
    char s[1024];
    int i;
    int done;
    int fd;
    void *pSharedMemory = NULL;
    char last_message_i_wrote[256];
    char md5ified_message[256];
    struct param_struct params;
    
    say(MY_NAME, "Oooo 'ello, I'm Mrs. Conclusion!");

    read_params(&params);

    // Mrs. Premise has already created the semaphore and shared memory. 
    // I just need to get handles to them.
    the_semaphore = sem_open(params.semaphore_name, 0);
    
    if (the_semaphore == SEM_FAILED) {
        the_semaphore = NULL;
        sprintf(s, "Getting a handle to the semaphore failed; errno is %d", errno);
        say(MY_NAME, s);
    }
    else {
        // get a handle to the shared memory
        fd = shm_open(params.shared_memory_name, O_RDWR, params.permissions);
        
        if (fd == -1) {
            sprintf(s, "Couldn't get a handle to the shared memory; errno is %d", errno);
            say(MY_NAME, s);
        }
        else {
            // mmap it.
            pSharedMemory = mmap((void *)0, (size_t)params.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
            if (pSharedMemory == MAP_FAILED) {
                sprintf(s, "MMapping the shared memory failed; errno is %d", errno);
                say(MY_NAME, s);
            }
            else {
                sprintf(s, "pSharedMemory = %p", pSharedMemory);
                say(MY_NAME, s);

                i = 0;
                done = 0;
                last_message_i_wrote[0] = '\0';
                while (!done) {
                    sprintf(s, "iteration %d", i);
                    say(MY_NAME, s);

                    // Wait for Mrs. Premise to free up the semaphore.
                    rc = acquire_semaphore(MY_NAME, the_semaphore, params.live_dangerously);
                    if (rc)
                        done = 1;
                    else {
                        while ( (!rc) && \
                                (!strcmp((char *)pSharedMemory, last_message_i_wrote)) 
                              ) {
                            // Nothing new; give Mrs. Premise another change to respond.
                            sprintf(s, "Read %zu characters '%s'", strlen((char *)pSharedMemory), (char *)pSharedMemory);
                            say(MY_NAME, s);
                            say(MY_NAME, "Releasing the semaphore");
                            rc = release_semaphore(MY_NAME, the_semaphore, params.live_dangerously);
                            if (!rc) {
                                say(MY_NAME, "Waiting to acquire the semaphore");
                                rc = acquire_semaphore(MY_NAME, the_semaphore, params.live_dangerously);
                            }
                        }
                        
                        md5ify(last_message_i_wrote, md5ified_message);

                        // I always accept the first message (when i == 0)
                        if ( (i == 0) || (!strcmp(md5ified_message, (char *)pSharedMemory)) ) {
                            // All is well
                            i++;
                    
                            if (i == params.iterations)
                                done = 1;

                            // MD5 the reply and write back to Mrs. Premise.
                            md5ify((char *)pSharedMemory, md5ified_message);

                            // Write back to Mrs. Premise.
                            sprintf(s, "Writing %zu characters '%s'", strlen(md5ified_message), md5ified_message);
                            say(MY_NAME, s);

                            strcpy((char *)pSharedMemory, md5ified_message);

                            strcpy(last_message_i_wrote, md5ified_message);
                        }
                        else {
                            sprintf(s, "Shared memory corruption after %d iterations.", i);
                            say(MY_NAME, s);                            
                            sprintf(s, "Mismatch; rc = %d, new message is '%s', expected '%s'.", rc, (char *)pSharedMemory, md5ified_message);
                            say(MY_NAME, s);
                            done = 1;
                        }                        
                    }

                    // Release the semaphore.
                    rc = release_semaphore(MY_NAME, the_semaphore, params.live_dangerously);
                    if (rc)
                        done = 1;
                }
            }
            // Un-mmap the memory
            rc = munmap(pSharedMemory, (size_t)params.size);
            if (rc) {
                sprintf(s, "Unmapping the memory failed; errno is %d", errno);
                say(MY_NAME, s);
            }
            
            // Close the shared memory segment's file descriptor            
            if (-1 == close(fd)) {
                sprintf(s, "Closing memory's file descriptor failed; errno is %d", errno);
                say(MY_NAME, s);
            }
        }
        rc = sem_close(the_semaphore);
        if (rc) {
            sprintf(s, "Closing the semaphore failed; errno is %d", errno);
            say(MY_NAME, s);
        }
    }
                    
  
    return 0; 
}