test_fork.cpp 3.2 KB

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