[CRIU] [PATCH 3/3] zdtm/fork: create 10 process concurrently

Andrey Vagin avagin at openvz.org
Sat Nov 8 04:19:04 PST 2014


Currently this test creates one process and wait it. So most part of the
time this test has only one process without children.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 test/zdtm/live/transition/fork.c | 76 ++++++++++++++++++++++++++++------------
 1 file changed, 54 insertions(+), 22 deletions(-)

diff --git a/test/zdtm/live/transition/fork.c b/test/zdtm/live/transition/fork.c
index 9914a2e..b9fc567 100644
--- a/test/zdtm/live/transition/fork.c
+++ b/test/zdtm/live/transition/fork.c
@@ -15,47 +15,79 @@
 const char *test_doc = "Tests that forking tasks are handled properly";
 const char *test_author = "Pavel Emelyanov <xemul at parallels.com>";
 
+char children[] = "0123456789";
+
 int main(int argc, char **argv)
 {
 	int pid, wpid, status;
+	int p[2];
 
 	test_init(argc, argv);
+
+	if (pipe(p)) {
+		err("pipe");
+		return -1;
+	}
+
+	if (write(p[1], children, sizeof(children)) != sizeof(children)) {
+		err("write");
+		return -1;
+	}
+
 	test_daemon();
 
 	while (test_go()) {
-		pid = fork();
-		if (pid < 0) {
-			fail("Can't fork");
-			goto out;
+		char c = 0;
+		int ret;
+
+		ret = read(p[0], &children, sizeof(children));
+		if (ret <= 0) {
+			err("read");
+			return 1;
 		}
 
-		if (pid == 0) {
+		for (; ret > 0; ret--) {
+			pid = fork();
+			if (pid < 0) {
+				fail("Can't fork");
+				goto out;
+			}
+
+			if (pid == 0) {
 #ifdef FORK2
-			usleep(10000);
+				usleep(10000);
 #endif
-			exit(0);
+				if (write(p[1], &c, 1) != 1) {
+					err("write");
+					return 1;
+				}
+				exit(0);
+			}
 		}
 
-		while ((wpid = wait(&status)) == -1 && errno == EINTR);
+		while (1) {
+			wpid = waitpid(-1, &status, WNOHANG);
+			if (wpid < 0) {
+				if (errno == ECHILD)
+					break;
+				err("waitpid");
+				return -1;
+			}
+			if (wpid == 0)
+				break;
 
-		if (wpid != pid) {
-			fail("Pids do not match: expected %d "
-				"instead of %d (errno=%d status=%x)",
-				pid, wpid, errno, status);
-			goto out;
-		}
+			if (!WIFEXITED(status)) {
+				fail("Task %d didn't exit", wpid);
+				goto out;
+			}
 
-		if (!WIFEXITED(status)) {
-			fail("Task didn't exit");
-			goto out;
+			if (WEXITSTATUS(status) != 0) {
+				fail("Task %d exited with wrong code", wpid);
+				goto out;
+			}
 		}
 
-		if (WEXITSTATUS(status) != 0) {
-			fail("Task exited with wrong code");
-			goto out;
-		}
 	}
-	test_waitsig();
 	pass();
 out:
 	return 0;
-- 
1.9.3



More information about the CRIU mailing list