参考https://blog.youkuaiyun.com/wallwind/article/details/6989513, 试试自己写个程序。
功能: 创建一个子线程,比较一下主线程和子线程所在的进程ID和个子的线程ID。
代码部分:
test_pthread.c
#include <stdio.h>
#include <pthread.h>
pthread_t new_tid;
void printThread(const char* s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s, pid is %lu, tid is %x \n", s, (unsigned long)pid, (unsigned long)tid);
}
void* entranThr(void* arg)
{
printf("create a children thread\n");
printThread("children thread");
}
void main()
{
int err = 0;
err = pthread_create(&new_tid, NULL, entranThr, NULL);
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("main thread pid is %lu, tid is %x,new_tid is %x\n", (unsigned long)pid, (unsigned long)tid, (unsigned long)new_tid);
sleep(5);
}
编译: cc test_pthread.c -lpthread
运行结果:
./a.out
main thread pid is 17603, tid is b77ac6c0,new_tid is b77abb70
create a children thread
children thread, pid is 17603, tid is b77abb70
总结:进程17603 下有两个线程,主线程0xb77ac6c0,子线程0xb77abb70.