获取线程的Linux PID可以通过以下几种方式:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t pid = getpid();
printf("PID: %d\n", pid);
return 0;
}
#include <pthread.h>
#include <stdio.h>
void* threadFunc(void* arg) {
pthread_t tid = pthread_self();
printf("Thread ID: %lu\n", tid);
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, threadFunc, NULL);
pthread_join(thread, NULL);
return 0;
}
ps -eLf
以上是获取线程的Linux PID的几种常用方法。根据具体的应用场景和需求,选择合适的方法来获取线程的PID。
云+社区技术沙龙[第10期]
云+社区技术沙龙[第21期]
腾讯云数据库TDSQL(PostgreSQL版)训练营
高校公开课
136届广交会企业系列专题培训
第四期Techo TVP开发者峰会
腾讯云数据库TDSQL(PostgreSQL版)训练营
领取专属 10元无门槛券
手把手带您无忧上云