2014年6月10日 星期二

The pthread_cond_signal() VS pthread_cond_broadcast()

上一篇說到了 pthread_cond_wati() , 現在說說 pthread_cond_signal() & pthread_cond_brocast() 吧 .

基本上這兩個 都是傳 condiction 給 ptherad , 讓 pthread 由 wait 狀態 wakeup 起來 . 不過有些差別 pthread_cond_signal() 只會讓一個 pthread wakeup (等待同一個 condition 的 pthread) ,至於是哪一個 pthread wakeup 呢 ? 目前我沒有去詳細 研究 , 不過這個signal 不能確定(指定)那一個 pthread , 所以使用上要小心點. 而 boradcast 是讓所有 pthread 都 wakeup 起來 .

所以依照所需選擇合適的function. 我通常都只有一個 ptherad 在等待 , 所以我都用 brocast , 確保我的 pthread 可以 wakeup .

下面有參考範例 ,



Reference :

    http://publib.boulder.ibm.com/iseries/v5r1/ic2987/index.htm?info/apis/users_76.htm
    http://publib.boulder.ibm.com/iseries/v5r1/ic2987/index.htm?info/apis/users_73.htm

PS. 目前比較忙沒有時間按照範例測試.

2014年6月4日 星期三

The pthread_cond_wait() and pthread_cond_timedwait()

最近在弄 power daemon ,需要使用大量 thread , 並且不能讓這些 thread " free running " , 所以再次研究一下 pthread_cond_wait() & pthread_cond_timedwait() !

先來看看 pthread_cond_wait() 的說明吧 ! (下列 藍色文字擷取於 man 說明)

pthread_cond_wait() 的動作會先取得 cond 的內容 , 然後 unlock 讓 ptherad_cond_signal() 可以更改 cond 的內容,接著再 lock , 並判斷是否需要繼續 ptherad , 如果沒有就 unlock 並停下 pthread, 相反就 lock 且接著 pthread 的內容.

        pthread_cond_wait(mutex, cond):
                  value = cond->value; /* 1 */
                  pthread_mutex_unlock(mutex); /* 2 */
                  pthread_mutex_lock(cond->mutex); /* 10 */
                  if (value == cond->value) { /* 11 */
                      me->next_cond = cond->waiter;
                      cond->waiter = me;
                      pthread_mutex_unlock(cond->mutex);
                      unable_to_run(me);
                  } else
                      pthread_mutex_unlock(cond->mutex); /* 12 */
                  pthread_mutex_lock(mutex); /* 13 */


我們再看看 pthread_cond_signal() 的動作吧.
首先會先 lock , 如果 lock 失敗會繼續等在 lock 階段, lock 成功會更動 cond 內容,並且叫醒 pthread. 然後 unlock 讓 pthread 可以繼續執行.


         pthread_cond_signal(cond):
                  pthread_mutex_lock(cond->mutex); /* 3 */
                  cond->value++; /* 4 */
                  if (cond->waiter) { /* 5 */
                      sleeper = cond->waiter; /* 6 */
                      cond->waiter = sleeper->next_cond; /* 7 */
                      able_to_run(sleeper); /* 8 */
                  }
                  pthread_mutex_unlock(cond->mutex); /* 9 */


說這麼多 ,來個範例吧 : 
下列兩個範例 , 一個是 pthread 本身 , 做完事情後等著 pthread_cond_signal() 來進行離開.
所以可以看到  pthread 執行時先 lock , 最後停在 pthread_cond_wait() . 等另外 function 
送出 pthread_cond_signal() , pthread 就會繼續執行,並且離開 pthread.
 

void *ps_event_handler ( void *argc )
{


   pthread_mutex_lock(&thread_mutex);

    /* Loop forever */
    for ( ps_thread_status = 1; ps_thread_status;  )
    {
          //---- TO DO.


            pthread_cond_wait(&thread_cond, &thread_mutex);
    
     }   /* End of for() */

   pthread_mutex_unlock(&thread_mutex);
  
   pthread_exit ( 0 );
}


void ps_thread_destory(void)
{
    ps_thread_status = 0;

    pthread_cond_signal(&thread_cond);      // force weakup thread , then stop itself.

    pthread_join(thread_ps_id, NULL);       // wait for thread end.

    pthread_mutex_destroy(&thread_mutex);
    pthread_cond_destroy(&thread_cond);
}


如果想要讓 pthread 暫停一段時間 後繼續執行 , 那就需要 pthread_cond_timedwait() 了.
範例如下 , 每一秒執行 for loop 一次.(紅色部分 是修改)
利用 pthread_cond_timedwait() return 的 value , 就可以判別是 timeout 還是被 signal 叫醒.


void *ps_event_handler ( void *argc )
{


   pthread_mutex_lock(&thread_mutex);

    /* Loop forever */
    for ( ps_thread_status = 1; ps_thread_status;  )
    {
          //---- TO DO.


            gettimeofday(&tv, &tz);
            ts.tv_sec  = tv.tv_sec + 1 ;
            ts.tv_nsec = tv.tv_usec*1000;
            retval = pthread_cond_timedwait(&thread_cond, &thread_mutex, &ts);


        if ( retval  == ETIMEDOUT)
        {
        //==== time out.

        //---- TO DO.


        }
        else
        {
        //==== Got signal.

        //---- TO DO.


        }
    
     }   /* End of for() */

   pthread_mutex_unlock(&thread_mutex);
  
   pthread_exit ( 0 );
}



這樣的架構下就可以控制 pthread 進行一些流程控制了.





2014年6月3日 星期二

EPERM error of settimeofday() , using capset() & capget() modify CAP_SYS_TIME.

最近發現 settimeofday() 沒有辦法執行 , 回報錯誤為 -1 (EPERM), 看一下 man 說明,發現沒有打開 CAP_SYS_TIME, 所以在 euid >= 1000 的時候不會允許設定系統時間. 
下列是 man settimeofday() 的說明:


       EPERM  The calling process has insufficient privilege  to  call  settimeofday();  
                     under  Linux  the  CAP_SYS_TIME  capability  is   required.

好吧 , 打開一下後門 ,  google 了一下,發現可以使用 capget() & capset來設定.
大致說明一下 CAP 的設定, 首先要先了解三個CAP 的等級.
A. effective => 有效的, 這個有設定就可以允許 操作相對應的東西 , 
                        例如 CAP_SYS_TIME 有設定就可以設定系統時間.
B. permitted => 這是允許的內容 , 如果這個資料內的 CAP_SYS_TIME 沒有設定 , 
                         那設定 effective 也沒用.
C. inheritable => 是否可被繼承 ,如果有設定 , fork() + exec() 的 子 process 也可以繼續
                          被允許對應的設定.

           typedef struct __user_cap_data_struct {
              __u32 effective;
              __u32 permitted;
              __u32 inheritable;
           } *cap_user_data_t;
 

來個實際例子吧, 目前子程序的 P (permitted) 是全部被允許的 , 
不過E(effective)沒有打開 ,利用下列的範例將其打開 , 就可以使用 settimeofday() 設定系統時間了.

        if (geteuid() > 0 )
        {
        //---- Not Root ,need turn on CAP_SYS_TIME.
                header.version = _LINUX_CAPABILITY_VERSION_1;
                header.pid = getpid();
                retval = capget(&header, &cap);
                if(retval)
                        goto capget_fail;

//                      printf("\t[%s] PID:%d E:%x P:%x I:%x\n", __FUNCTION__,
//                                      header.pid, cap.effective, cap.permitted,cap.inheritable);

                header.version = _LINUX_CAPABILITY_VERSION_1;
                header.pid = 0;

                if ( enable )
                        cap.effective   |=  (1 << CAP_SYS_TIME);
                else
                        cap.effective   &=  ~(1 << CAP_SYS_TIME);

                retval = capset(&header, &cap);
                if(retval)
                        goto capset_fail;
        }





PS. 這段 function call 我想要打開 CAP_SYS_TIME 設定系統時間後就關閉(我提供一個特別的 SO API , 所以使用者還是不能 設定 系統時間,除非透過這個 API .
       所以先判別是否為 root (利用 EUID) , 如果不是就可以enable / disable . 如果是 root , 就不理會(root 本來就可以)