builds/zos/test_fork.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include <unistd.h> // For alarm()
5 
6 const char *address = "tcp://127.0.0.1:6571";
7 
8 #define NUM_MESSAGES 5
9 #define TIMEOUT_SECS 5 // Global timeout
10 
11 int main (void)
12 {
14  void *ctx = zmq_ctx_new ();
15  assert (ctx);
16 
17  // Create and bind pull socket to receive messages
18  void *pull = zmq_socket (ctx, ZMQ_PULL);
19  assert (pull);
20  int rc = zmq_bind (pull, address);
21  assert (rc == 0);
22 
23  int pid = fork ();
24  if (pid == 0) {
25  // Child process
26  // Immediately close parent sockets and context
27  zmq_close (pull);
28  zmq_ctx_term (ctx);
29 
30  // Create new context, socket, connect and send some messages
31  void *child_ctx = zmq_ctx_new ();
32  assert (child_ctx);
33  void *push = zmq_socket (child_ctx, ZMQ_PUSH);
34  assert (push);
35  rc = zmq_connect (push, address);
36  assert (rc == 0);
37  int count;
38  for (count = 0; count < NUM_MESSAGES; count++)
39  zmq_send (push, "Hello", 5, 0);
40 
41  zmq_close (push);
42  zmq_ctx_destroy (child_ctx);
43  exit (0);
44  }
45  else {
46  // Parent process
47  alarm(TIMEOUT_SECS); // Set upper limit on runtime
48 
49  int count;
50  for (count = 0; count < NUM_MESSAGES; count++) {
51  char buffer [5];
52  int num_bytes = zmq_recv (pull, buffer, 5, 0);
53  assert (num_bytes == 5);
54  }
55  int child_status;
56  while (true) {
57  rc = waitpid (pid, &child_status, 0);
58  if (rc == -1 && errno == EINTR)
59  continue;
60  assert (rc > 0);
61  // Verify the status code of the child was zero
62  assert (WEXITSTATUS (child_status) == 0);
63  break;
64  }
65  exit (0);
66  }
67  return 0;
68 }
EINTR
#define EINTR
Definition: errno.hpp:7
zmq_ctx_new
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:109
errno
int errno
address
const char * address
Definition: builds/zos/test_fork.cpp:6
zmq_ctx_destroy
ZMQ_EXPORT int zmq_ctx_destroy(void *context_)
Definition: zmq.cpp:212
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
TIMEOUT_SECS
#define TIMEOUT_SECS
Definition: builds/zos/test_fork.cpp:9
ZMQ_PUSH
#define ZMQ_PUSH
Definition: zmq.h:266
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
buffer
Definition: buffer_processor.h:43
zmq_close
ZMQ_EXPORT int zmq_close(void *s_)
Definition: zmq.cpp:241
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
push
static void push(tarjan *t, const upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5890
main
int main(void)
Definition: builds/zos/test_fork.cpp:11
zmq_ctx_term
ZMQ_EXPORT int zmq_ctx_term(void *context_)
Definition: zmq.cpp:128
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
ZMQ_PULL
#define ZMQ_PULL
Definition: zmq.h:265
count
GLint GLsizei count
Definition: glcorearb.h:2830
NUM_MESSAGES
#define NUM_MESSAGES
Definition: builds/zos/test_fork.cpp:8


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59