Back to index

glibc  2.9
tst-getpid3.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <pthread.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 #include <sys/wait.h>
00008 
00009 
00010 static pid_t pid;
00011 
00012 static void *
00013 pid_thread (void *arg)
00014 {
00015   if (pid != getpid ())
00016     {
00017       printf ("pid wrong in thread: should be %d, is %d\n",
00018              (int) pid, (int) getpid ());
00019       return (void *) 1L;
00020     }
00021 
00022   return NULL;
00023 }
00024 
00025 static int
00026 do_test (void)
00027 {
00028   pid = getpid ();
00029 
00030   pthread_t thr;
00031   int ret = pthread_create (&thr, NULL, pid_thread, NULL);
00032   if (ret)
00033     {
00034       printf ("pthread_create failed: %d\n", ret);
00035       return 1;
00036     }
00037 
00038   void *thr_ret;
00039   ret = pthread_join (thr, &thr_ret);
00040   if (ret)
00041     {
00042       printf ("pthread_create failed: %d\n", ret);
00043       return 1;
00044     }
00045   else if (thr_ret)
00046     {
00047       printf ("thread getpid failed\n");
00048       return 1;
00049     }
00050 
00051   pid_t child = fork ();
00052   if (child == -1)
00053     {
00054       printf ("fork failed: %m\n");
00055       return 1;
00056     }
00057   else if (child == 0)
00058     {
00059       if (pid == getpid ())
00060        {
00061          puts ("pid did not change after fork");
00062          exit (1);
00063        }
00064 
00065       pid = getpid ();
00066       ret = pthread_create (&thr, NULL, pid_thread, NULL);
00067       if (ret)
00068        {
00069          printf ("pthread_create failed: %d\n", ret);
00070          return 1;
00071        }
00072 
00073       ret = pthread_join (thr, &thr_ret);
00074       if (ret)
00075        {
00076          printf ("pthread_create failed: %d\n", ret);
00077          return 1;
00078        }
00079       else if (thr_ret)
00080        {
00081          printf ("thread getpid failed\n");
00082          return 1;
00083        }
00084 
00085       return 0;
00086     }
00087 
00088   int status;
00089   if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
00090     {
00091       puts ("waitpid failed");
00092       kill (child, SIGKILL);
00093       return 1;
00094     }
00095 
00096   if (!WIFEXITED (status))
00097     {
00098       if (WIFSIGNALED (status))
00099        printf ("died from signal %s\n", strsignal (WTERMSIG (status)));
00100       else
00101        puts ("did not terminate correctly");
00102       return 1;
00103     }
00104   if (WEXITSTATUS (status) != 0)
00105     {
00106       printf ("exit code %d\n", WEXITSTATUS (status));
00107       return 1;
00108     }
00109 
00110   return 0;
00111 }
00112 
00113 #define TEST_FUNCTION do_test ()
00114 #include "../test-skeleton.c"