* test/fork_delay.c: Like test/fork.c, but with 1 second delay in child
to see parent-child interaction more clearly.
* test/vfork_delay.c: Likewise.  This shows vfork not returning until
the child has exited, if it's not getting changed to fork.
* test/vfork_exec.c: Like test/vfork_delay.c, but instead of exiting
the child called execve() and the exec'd process waits another second.
This shows vfork not returning until the child calls execve(), and then
returning while the child continues to run for another second.

Signed-off-by: Jamie Lokier <ja...@shareable.org>
---
 test/Makefile      |    2 +-
 test/fork_delay.c  |   24 ++++++++++++++++++++++++
 test/vfork_delay.c |   21 +++++++++++++++++++++
 test/vfork_exec.c  |   36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 82 insertions(+), 1 deletions(-)
 create mode 100644 test/fork_delay.c
 create mode 100644 test/vfork_delay.c
 create mode 100644 test/vfork_exec.c

diff --git a/test/Makefile b/test/Makefile
index 3e7236b..3123707 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -2,7 +2,7 @@
 # $Id$
 #
 
-all: vfork fork sig skodic clone leaderkill childthread
+all: vfork vfork_delay vfork_exec fork fork_delay sig skodic clone leaderkill 
childthread
 
 leaderkill: LDFLAGS += -pthread
 
diff --git a/test/fork_delay.c b/test/fork_delay.c
new file mode 100644
index 0000000..e8302a7
--- /dev/null
+++ b/test/fork_delay.c
@@ -0,0 +1,24 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/select.h>
+
+int main(int argc, char *argv[])
+{
+  int pid;
+  if ((pid = fork())== 0) {
+               struct timeval tv;
+               write(1, "child before sleep\n", 19);
+               tv.tv_sec = 1;
+               tv.tv_usec = 0;
+               select(0, NULL, NULL, NULL, &tv);
+               write(1, "child after sleep\n", 18);
+       } else {
+               wait(0);
+               write(1, "parent\n", 7);
+               fprintf(stderr, "pid = %d\n", pid);
+       }
+
+       exit(0);
+}
diff --git a/test/vfork_delay.c b/test/vfork_delay.c
new file mode 100644
index 0000000..acc14e9
--- /dev/null
+++ b/test/vfork_delay.c
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/select.h>
+
+int main(int argc, char *argv[])
+{
+       if (vfork() == 0) {
+               struct timeval tv;
+               write(1, "child before sleep\n", 19);
+               tv.tv_sec = 1;
+               tv.tv_usec = 0;
+               select(0, NULL, NULL, NULL, &tv);
+               write(1, "child after sleep\n", 18);
+       } else {
+               wait(0);
+               write(1, "parent\n", 7);
+       }
+
+       exit(0);
+}
diff --git a/test/vfork_exec.c b/test/vfork_exec.c
new file mode 100644
index 0000000..78b719d
--- /dev/null
+++ b/test/vfork_exec.c
@@ -0,0 +1,36 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/select.h>
+
+int main(int argc, char *argv[], char *envp[])
+{
+       struct timeval tv;
+       char *args[3];
+       if (argc == 2 && argv[1][0] == 'c') {
+               write(1, "exec'd child after exec\n", 24);
+               write(1, "exec'd child before sleep\n", 26);
+               tv.tv_sec = 1;
+               tv.tv_usec = 0;
+               select(0, NULL, NULL, NULL, &tv);
+               write(1, "exec'd child after sleep\n", 25);
+               exit(0);
+       } else if (vfork() == 0) {
+               write(1, "child before sleep\n", 19);
+               tv.tv_sec = 1;
+               tv.tv_usec = 0;
+               select(0, NULL, NULL, NULL, &tv);
+               write(1, "child after sleep\n", 18);
+               write(1, "child before exec\n", 18);
+               args[0] = argv[0];
+               args[1] = "child";
+               args[2] = NULL;
+               execve(argv[0], args, envp); /* execve is super vfork safe. */
+               _exit(1);
+       } else {
+               wait(0);
+               write(1, "parent\n", 7);
+       }
+
+       exit(0);
+}
-- 
1.7.0.4

_______________________________________________
uClinux-dev mailing list
uClinux-dev@uclinux.org
http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
This message was resent by uclinux-dev@uclinux.org
To unsubscribe see:
http://mailman.uclinux.org/mailman/options/uclinux-dev

Reply via email to