Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1158223
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2279
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-27 19:53
个人简介

JustForFun

文章分类

全部博文(241)

文章存档

2023年(8)

2022年(2)

2021年(3)

2020年(30)

2019年(11)

2018年(27)

2017年(54)

2016年(83)

2015年(23)

我的朋友
最近访客

分类: LINUX

2016-09-08 09:48:05

Linux中,每个进程有一个pid,类型pid_t,由getpid()取得。Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库维护,其id空间是各个进程独立的(即不同进程中的线程可能有相同的id)。Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。
有时候我们可能需要知道线程的真实pid。比如进程P1要向另外一个进程P2中的某个线程发送信号时,既不能使用P2的pid,更不能使用线程的pthread id,而只能使用该线程的真实pid,称为tid
有一个函数gettid()可以得到tid,但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取,实现了
in main(int argc, char *argv[]
{
   
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查看守护进程
/////////////////////////////////////////////////////////////////
阅读(579) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~