File: posix-mq2.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (169 lines) | stat: -rw-r--r-- 3,870 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <sys/types.h>
#include <mqueue.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

struct msgbuf {
  mqd_t mqdes;
  int   expected;
};

void msg_snd(mqd_t mqdes, int i);
void msg_rcv(mqd_t mqdes, int i);
static int msg_notify(mqd_t mqdes, int i);

static void                     /* Thread start function */
tfunc(union sigval sv)
{
  struct msgbuf *m = (struct msgbuf *) sv.sival_ptr;

  msg_rcv(m->mqdes, m->expected);

  printf("Server: Notification received for msg: %d\n", m->expected);
  free(m);
  return;
}

void msg_snd(mqd_t mqdes, int i)
{
  char buf[16];
  sprintf(buf, "%d", i);

  errno = 0;
  if (mq_send(mqdes, buf, strlen(buf) + 1, 0) == -1) {
    perror("mq_send");
    fflush(stdout);
    sleep(1);
    exit(1);
  }
}

void msg_rcv(mqd_t mqdes, int i)
{
  struct mq_attr attr;

  if (mq_getattr(mqdes, &attr) == -1) {
    perror("mq_getattr");
    exit(1);
  }
  char *buf = malloc(attr.mq_msgsize);

  if (mq_receive(mqdes, buf, attr.mq_msgsize, NULL) == -1) {
    perror("mq_receive");
    fflush(stdout);
    sleep(1);
    exit(1);
  }
  if (i != atoi(buf)) {
    printf("Msg mismatch: expected: %d, got: %s\n", i, buf);
    fflush(stdout);
    sleep(1);
    exit(1);
  }
  free(buf);
}

static int msg_notify(mqd_t mqdes, int i)
{
  struct msgbuf *m = malloc(sizeof(struct msgbuf));
  m->mqdes = mqdes;
  m->expected = i;
  struct sigevent sev;
  sev.sigev_notify = SIGEV_THREAD;
  sev.sigev_notify_function = tfunc;
  sev.sigev_notify_attributes = NULL;
  sev.sigev_value.sival_ptr = m;   /* Arg. to thread func. */
  return mq_notify(mqdes, &sev);
}

void parent(const char *mqname, const char *mqname2)
{
  mqd_t mqdes = mq_open(mqname, O_RDWR | O_CREAT, 0666, 0);
  // Unfortunately, DMTCP doesn't yet support unlinking while others use it:
  // mq_unlink(mqname); /* parent and child will continue to use mqname */
  if (mqdes == -1) {
    perror("mq_open");
    exit(1);
  }

  mqd_t mqdes2 = mq_open(mqname2, O_RDWR | O_CREAT, 0666, 0);
  if (mqdes == -1) {
    perror("mq_open");
    exit(1);
  }

  int i = 1;
  while (1) {
    int rc;
    // Will call mq_notify, causing a thread to run tfunc and do mq_recv()
    // Notification will only occur after the queue is emptied
    //   and a new message arrives.
    do {
      // It's possible for parent to iterate twice before child receives.
      // Keep trying.  We need a notify after each successful receive.
      sleep(1);
      rc = msg_notify(mqdes2, i+1);
      // printf("Was notified: rc, errno: %d %d\n", rc, errno);
    } while ((rc == -1) && (errno == EBUSY));
    if (rc == -1) {
      perror("mq_notify");
      exit(1);
    }
    printf("Server: sending %d\n", i);
    fflush(stdout);
    msg_snd(mqdes, i);
    sleep(1);
    i += 2;
  }
  exit(0);
}

void child(const char *mqname, const char *mqname2)
{
  mqd_t mqdes = mq_open(mqname, O_RDWR | O_CREAT, 0666, 0);
  // Unfortunately, DMTCP doesn't yet support unlinking while others use it:
  // mq_unlink(mqname); /* parent and child will continue to use mqname */
  if (mqdes == -1) {
    perror("mq_open");
    exit(1);
  }

  mqd_t mqdes2 = mq_open(mqname2, O_RDWR | O_CREAT, 0666, 0);
  if (mqdes == -1) {
    perror("mq_open");
    exit(1);
  }

  int i=1;
  while (1) {
    msg_rcv(mqdes, i);
    printf("Client: received %d, sending %d\n", i, i + 1);
    fflush(stdout);
    msg_snd(mqdes2, i + 1);
    i += 2;
  }
  exit(0);
}

int main(int argc, char **argv)
{
  char mqname[256];
  char mqname2[256];
  char *user = getenv("USER");
  sprintf(mqname, "/dmtcp-mq-%s", user == NULL ? "" : user);
  sprintf(mqname2, "/dmtcp-mq-2-%s", user == NULL ? "" : user);

  mq_unlink(mqname);
  mq_unlink(mqname2);
  if (fork() == 0) {
    child(mqname, mqname2);
  } else {
    parent(mqname, mqname2);
  }
  return 0;
}