0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #undef NDEBUG
0018 #include <assert.h>
0019 #include <sched.h>
0020 #include <stdio.h>
0021 #include <unistd.h>
0022 #include <sys/mman.h>
0023 #include <sys/wait.h>
0024
0025 #include "proc.h"
0026
0027 int f(void *arg)
0028 {
0029 char buf1[64], buf2[64];
0030 pid_t pid, tid;
0031 ssize_t rv;
0032
0033 pid = sys_getpid();
0034 tid = sys_gettid();
0035 snprintf(buf1, sizeof(buf1), "%u/task/%u", pid, tid);
0036
0037 rv = readlink("/proc/thread-self", buf2, sizeof(buf2));
0038 assert(rv == strlen(buf1));
0039 buf2[rv] = '\0';
0040 assert(streq(buf1, buf2));
0041
0042 if (arg)
0043 exit(0);
0044 return 0;
0045 }
0046
0047 int main(void)
0048 {
0049 const int PAGE_SIZE = sysconf(_SC_PAGESIZE);
0050 pid_t pid;
0051 void *stack;
0052
0053
0054 f((void *)0);
0055
0056 stack = mmap(NULL, 2 * PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
0057 assert(stack != MAP_FAILED);
0058
0059 pid = clone(f, stack + PAGE_SIZE, CLONE_THREAD|CLONE_SIGHAND|CLONE_VM, (void *)1);
0060 assert(pid > 0);
0061 pause();
0062
0063 return 0;
0064 }