X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=tests%2Fexample_vfork.c;fp=tests%2Fexample_vfork.c;h=35ceaf42f24bb08ac1f3d11d6c3bd18927ed04b7;hb=1c5441c26aa476ffb7ee2a83cea9d73817f83292;hp=0000000000000000000000000000000000000000;hpb=66140e14e485af27f5234ae7b744a50dff3dd4c9;p=coloredstderr%2Fcoloredstderr.git diff --git a/tests/example_vfork.c b/tests/example_vfork.c new file mode 100644 index 0000000..35ceaf4 --- /dev/null +++ b/tests/example_vfork.c @@ -0,0 +1,43 @@ +/* + * Test issues with vfork(). + * + * Copyright (C) 2013 Simon Ruderich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +int main(int argc, char **argv) { + pid_t pid; + + fprintf(stderr, "Before vfork().\n"); + + pid = vfork(); + if (pid == 0) { + /* This violates the POSIX standard! The "child" is only allowed to + * modify the result of vfork(), e.g. the pid variable. Some programs + * (e.g. gdb) do it anyway so we have to workaround it. */ + dup2(STDOUT_FILENO, STDERR_FILENO); + + _exit(2); + } + + fprintf(stderr, "After vfork().\n"); + puts(""); + + return EXIT_SUCCESS; +}