mirror of
https://github.com/zeromq/libzmq.git
synced 2025-05-02 07:31:39 +02:00
test_fork: update test to verify communication between parent and child
This commit is contained in:
parent
ff2900fd52
commit
a3d9d01af8
@ -26,49 +26,90 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
const char* address = "tcp://127.0.0.1:6571";
|
const char* address = "tcp://127.0.0.1:6571";
|
||||||
void my_handler(int, siginfo_t *info, void *uap);
|
|
||||||
volatile int child_exited = 0;
|
#define NUM_MESSAGES 5
|
||||||
volatile int child_exit_code = 0;
|
|
||||||
|
|
||||||
int main (void)
|
int main (void)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
setup_test_environment();
|
setup_test_environment();
|
||||||
|
|
||||||
printf("parent: process id = %d\n", (int)getpid());
|
printf("parent: process id = %d\n", (int)getpid());
|
||||||
void *ctx = zmq_ctx_new ();
|
void *ctx = zmq_ctx_new ();
|
||||||
assert (ctx);
|
assert (ctx);
|
||||||
|
|
||||||
void* sock = zmq_socket(ctx, ZMQ_PULL);
|
void* sock = zmq_socket(ctx, ZMQ_PULL);
|
||||||
assert(sock);
|
assert(sock);
|
||||||
|
|
||||||
rc = zmq_connect(sock, address);
|
rc = zmq_bind(sock, address);
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
|
|
||||||
// wait for io threads to be running
|
// wait for io threads to be running
|
||||||
usleep(100000);
|
usleep(100000);
|
||||||
|
|
||||||
|
printf("about to fork()\n");
|
||||||
int pid = fork();
|
int pid = fork();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
sleep(1);
|
// this is the child process
|
||||||
|
|
||||||
|
usleep(100000);
|
||||||
printf("child: process id = %d\n", (int)getpid());
|
printf("child: process id = %d\n", (int)getpid());
|
||||||
printf("child: terminating inherited context...\n");
|
printf("child: terminating inherited context...\n");
|
||||||
// close the socket, or the context gets stuck indefinitely
|
// close the socket, or the context gets stuck indefinitely
|
||||||
zmq_close(sock);
|
// zmq_close(sock);
|
||||||
zmq_term(ctx);
|
zmq_term(ctx);
|
||||||
printf("child done\n");
|
|
||||||
|
// create new context, socket, connect and send some messages
|
||||||
|
void *ctx2 = zmq_ctx_new ();
|
||||||
|
assert (ctx2);
|
||||||
|
|
||||||
|
void* push = zmq_socket(ctx2, ZMQ_PUSH);
|
||||||
|
assert(push);
|
||||||
|
|
||||||
|
rc = zmq_connect(push, address);
|
||||||
|
assert(rc == 0);
|
||||||
|
|
||||||
|
const char* message = "hello";
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < NUM_MESSAGES; i += 1) {
|
||||||
|
printf("child: send message #%d\n", i);
|
||||||
|
zmq_send(push, message, strlen(message), 0);
|
||||||
|
usleep(100000);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("child: closing push socket\n");
|
||||||
|
zmq_close(push);
|
||||||
|
printf("child: destroying child context\n");
|
||||||
|
zmq_ctx_destroy(ctx2);
|
||||||
|
printf("child: done\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
} else {
|
} else {
|
||||||
|
// this is the parent process
|
||||||
|
printf("parent: waiting for %d messages\n", NUM_MESSAGES);
|
||||||
|
|
||||||
|
char buffer[1024];
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < NUM_MESSAGES; i += 1) {
|
||||||
|
int num_bytes = zmq_recv(sock, buffer, 1024, 0);
|
||||||
|
assert(num_bytes > 0);
|
||||||
|
buffer[num_bytes] = 0;
|
||||||
|
printf("parent: received #%d: %s\n", i, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
int child_status;
|
int child_status;
|
||||||
while (true) {
|
while (true) {
|
||||||
rc = waitpid(pid, &child_status, 0);
|
rc = waitpid(pid, &child_status, 0);
|
||||||
if (rc == -1 && errno == EINTR) continue;
|
if (rc == -1 && errno == EINTR) continue;
|
||||||
printf("parent: child exit code = %d, rc = %d, errno = %d\n", WEXITSTATUS(child_status), rc, errno);
|
printf("parent: child exit code = %d, rc = %d, errno = %d\n", WEXITSTATUS(child_status), rc, errno);
|
||||||
|
assert(rc > 0);
|
||||||
|
// verify the status code of the child was zero.
|
||||||
assert(WEXITSTATUS(child_status) == 0);
|
assert(WEXITSTATUS(child_status) == 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printf("parent done.\n");
|
printf("parent: done.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user