libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_fork.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 const char *address = "tcp://127.0.0.1:6571";
33 
34 #define NUM_MESSAGES 5
35 
36 int main (void)
37 {
38 #if !defined (ZMQ_HAVE_WINDOWS)
40  void *ctx = zmq_ctx_new ();
41  assert (ctx);
42 
43  // Create and bind pull socket to receive messages
44  void *pull = zmq_socket (ctx, ZMQ_PULL);
45  assert (pull);
46  int rc = zmq_bind (pull, address);
47  assert (rc == 0);
48 
49  int pid = fork ();
50  if (pid == 0) {
51  // Child process
52  // Immediately close parent sockets and context
53  zmq_close (pull);
54  zmq_ctx_term (ctx);
55 
56  // Create new context, socket, connect and send some messages
57  void *child_ctx = zmq_ctx_new ();
58  assert (child_ctx);
59  void *push = zmq_socket (child_ctx, ZMQ_PUSH);
60  assert (push);
61  rc = zmq_connect (push, address);
62  assert (rc == 0);
63  int count;
64  for (count = 0; count < NUM_MESSAGES; count++)
65  zmq_send (push, "Hello", 5, 0);
66 
67  zmq_close (push);
68  zmq_ctx_destroy (child_ctx);
69  exit (0);
70  }
71  else {
72  // Parent process
73  int count;
74  for (count = 0; count < NUM_MESSAGES; count++) {
75  char buffer [5];
76  int num_bytes = zmq_recv (pull, buffer, 5, 0);
77  assert (num_bytes == 5);
78  }
79  int child_status;
80  while (true) {
81  rc = waitpid (pid, &child_status, 0);
82  if (rc == -1 && errno == EINTR)
83  continue;
84  assert (rc > 0);
85  // Verify the status code of the child was zero
86  assert (WEXITSTATUS (child_status) == 0);
87  break;
88  }
89  zmq_close (pull);
90  zmq_ctx_term (ctx);
91  exit (0);
92  }
93 #endif
94  return 0;
95 }
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
int main(void)
Definition: test_fork.cpp:36
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_recv(void *s, void *buf, size_t len, int flags)
Definition: zmq.cpp:507
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_PUSH
Definition: zmq.h:254
#define NUM_MESSAGES
Definition: test_fork.cpp:34
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_ctx_destroy(void *context)
Definition: zmq.cpp:236
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
const char * address
Definition: test_fork.cpp:32
#define ZMQ_PULL
Definition: zmq.h:253