有时候我们可能需要知道线程的真实pid。比如进程P1要向另外一个进程P2中的某个线程发送信号时,既不能使用P2的pid,更不能使用线程的pthread id,而只能使用该线程的真实pid,称为tid
有一个函数gettid()可以得到tid,但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取,实现了
in main(int argc, char *argv[]有一个函数gettid()可以得到tid,但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取,实现了
{
pid_t tid;
tid = syscall(SYS_gettid);
printf("tid : %d\n",tid);
tid = syscall(SYS_tgkill, getpid(), tid, SIGHUP);
printf("tid : %d ...\n",tid);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
必需设置inher的属性为 PTHREAD_EXPLICIT_SCHED,否则设置线程的优先级会被忽略.
#include
#include
#include
#include
#include
#include
void * thr_fun(void *arg)
{
int policy, ret;
struct sched_param param;
//获取线程调度参数
ret = pthread_getschedparam(pthread_self(), &policy, ?m);
if(ret!=0)
{
printf("pthread_getschedparam %s/n", strerror(ret) );
exit(1);
}
if (policy == SCHED_FIFO)
{
printf("policy:SCHED_FIFO/n");
}
else if (policy == SCHED_OTHER)
{
printf("policy:SCHED_OTHER/n");
}
else if (policy == SCHED_RR)
{
printf("policy:SCHED_RR/n");
}
printf("param:%d/n", param.sched_priority);
long long i;
sleep(18);
printf("child %u\n",getpid());
pthread_exit(NULL);
}
int main(void)
{
int ret;
pthread_t tid;
pthread_attr_t attr;
int policy, inher;
struct sched_param param;
//初始化线程属性
pthread_attr_init(&attr);
//获取继承的调度策略
ret = pthread_attr_getinheritsched(&attr, &inher);
if (ret!=0)
{
printf("pthread_attr_getinheritsched/n%s/n", strerror(ret));
exit(1);
}
//
if (inher == PTHREAD_EXPLICIT_SCHED)
{
printf("PTHREAD_EXPLICIT_SCHED/n");
}
else if (inher == PTHREAD_INHERIT_SCHED)
{
printf("PTHREAD_INHERIT_SCHED/n");
//inher = PTHREAD_EXPLICIT_SCHED;
}
//设置继承的调度策略
//必需设置inher的属性为 PTHREAD_EXPLICIT_SCHED,否则设置线程的优先级会被忽略
ret = pthread_attr_setinheritsched(&attr, inher);
if (ret!=0)
{
printf("pthread_attr_setinheritsched/n%s/n", strerror(ret));
exit(1);
}
//policy = SCHED_FIFO;//在Ubuntu9.10上需要root权限
policy = SCHED_RR;//在Ubuntu9.10上需要root权限
//设置线程调度策略
ret = pthread_attr_setschedpolicy(&attr, policy);
if (ret!=0)
{
printf(" pthread_attr_setschedpolicy/n%s/n", strerror(ret));
exit(1);
}
param.sched_priority = 3;
//设置调度参数
ret = pthread_attr_setschedparam(&attr, ?m);
if (ret!=0)
{
printf(" pthread_attr_setschedparam/n%s/n", strerror(ret));
exit(1);
}
//创建线程
ret = pthread_create(&tid, &attr, thr_fun, NULL);
if (ret!=0)
{
printf("pthread_create/n%s/n", strerror(ret));
exit(1);
}
printf("main %u\n",getpid());
sleep(18);
pthread_join(tid, NULL);
pthread_exit(NULL);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
//

//////////////////////////////////////////////////
ps -aux和ps -ef查看守护进程
/////////////////////////////////////////////////////////////////