|Code| How to use control flow by pthread_cond_timedwait()

There are three parameters related with pthread_cond_timedwait().

Definition:
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);

Parameters:
pthread_cond_t *restrict cond : the condition you want to trigger this flow
pthread_mutex_t *restrict mutex : Mutual exclusion
const struct timespec *restrict abstime : absolutely waiting time ( = system time + waiting



【Step by Step】

Initial the mutex:
1. pthread_mutex_init(pthread_mutex_t *restrict  __mutex ,
              const pthread_mutexattr_t *restrict __mutex_attr);
   __mutex_attr can be NULL.
   __mutex_attr is the parameter about mutex.
  If it is not NULL, go to “2.”

2. pthread_mutexattr_init(pthread_mutexattr_t * __mutex_attr );
 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
   type: PTHREAD_MUTEX_RECURSIVE,
      PTHREAD_MUTEX_NORMAL ……
3. pthread_cond_init(pthread_cond_t *restrict __cond ,
const pthread_condattr_t *restrict __cond_attr);

Start to wait for signal ” __cond “:
1. lock mutex: pthread_mutex_lock(&__mutex)
2. pthread_cond_timedwait(&__cond, &__mutex, &__abstime)
3. unlock mutex: pthread_mutex_unlock(&__mutex)

Start to send the signal ” __cond “:
1. lock mutex: pthread_mutex_lock(&__mutex)
2. pthread_cond_signal (&__cond)
3. unlock mutex: pthread_mutex_unlock(&__mutex)

Destroy the mutex:
1. pthread_mutex_destroy( pthread_mutex_t *restrict  __mutex );
2. pthread_mutexattr_destroy( pthread_mutexattr_t * __mutex_attr );
3. pthread_cond_destroy( pthread_cond_t *restrict __cond );

E.g.

// This thread will wait the signal.
void thread1(void *arg){
 int inArg = (int)arg;
 int ret = 0;
 struct timeval now;
 struct timespec outtime;

 pthread_mutex_lock(&g_mutex);

 gettimeofday(&now, NULL);
 outtime.tv_sec = now.tv_sec + 5;
 outtime.tv_nsec = now.tv_usec * 1000;
 ret = pthread_cond_timedwait(&g_cond, &g_mutex, &outtime);

 pthread_mutex_unlock(&g_mutex);
}


int main(void)
{
 pthread_t id1;
 int ret;

 // Initial mutex
 pthread_cond_init(&g_cond, NULL);
 pthread_mutex_init(&g_mutex, NULL);
 ret = pthread_create(&id1, NULL, (void *)thread1, (void *)1);

 if (0 != ret){
  printf(“thread 1 create failed!\n”);
  return 1;
 }

 printf(“Waiting %ds send the signal!\n”, SENDSIGTIME);
 sleep(SENDSIGTIME);

 // send the signal
 pthread_mutex_lock(&g_mutex);
 pthread_cond_signal(&g_cond);
 pthread_mutex_unlock(&g_mutex);
 pthread_join(id1, NULL);

 //Destroy the mutex
 pthread_cond_destroy(&g_cond);
 pthread_mutex_destroy(&g_mutex);
 return 0;
}

References:
https://blog.csdn.net/dead_g/article/details/73338960
https://linux.die.net/man/3/pthread_cond_timedwait
https://linux.die.net/man/3/pthread_cond_init
https://blog.csdn.net/yasi_xi/article/details/19112077

|Design| bit map

如果你的某個宣告有多重意義,
但你一次只想知道某的欄位的狀態,可以善加利用 bit map

舉例:

#define status1 (uint_8) 1<<0
#define status2 (uint_8) 1<<1
#define status3 (uint_8) 1<<2

uint_8 test_case = 2;
int test_hex_case = 0xFA;

If( test_case & status1 ){ // false; 00000000 <- 00000010 & 00000001
  //do nothing
}
If( test_case & status2 ){ // true, not zero; 00000010 <- 00000010 & 00000010
  //do something
}
If( test_hex_case & status2 ){ // true, not zero;
// 0xFA = 1111 1010
// 00000010 <- 1111 1010 & 00000010
  // do something
}

|Jobs| 利用 Samba 建立虛擬機與本機共享資料夾

環境
虛擬機:Virtual Box
主系統:Windows 10
子系統:Ubuntu 18.04

假設你網卡已經設定好且能在虛擬機利用主系統網路上網!
以下開始設定 Samba 共享資料夾!

Part 1. 在子系統的 Ununtu 裡面
Step 1. 固定子系統的 IP
    ifconfig
    找到 host-only 網路的那張網卡,設定固定 IP!
    因為 host-only 預設網段是192.168.56.0,
    因此 IP 設定上也要在同一個網段之下。
    sudo gedit /etc/network/interfaces
     auto 網卡名稱
     iface 網卡名稱 inet static
     address host-only網卡的IP
     netmask 255.255.255.0
     network 192.168.56.0

    reboot

Step 2. 安裝與設定 Samba
    sudo apt-get install samba smbclien    
    sudo gedit /etc/samba/smb.conf
     加入以下設定
      [Share]
      path = /
      public = yes
      writable = yes
      read only = no
      valid users = YOURNAME
      force create mode = 777
      create mask = 0777
      directory mask = 0777
      delete readonly = yes
      guest ok = yes
      available = yes
      browseable = yes

Step 3. 設定從 Windows 登入的密碼
     sudo smbpasswd -a YOURNAME

Step 4. 測試/etc/samba/smb.conf是否有問題
     sudo testparm

Step 5. 重啟Samba,使以上設定生效
    sudo /etc/init.d/smbd restart


Part 2. 在主系統的 Windows
Step 1. Ping 一下 Ubuntu
     ping host-only網卡的IP

Step 2. 到本機點右鍵
    連線到網路磁碟機,輸入 host-only網卡的IP
    記得兩個勾勾都要勾哦!
    讓它能用其他方式連線(程式自己會去找)

以上,感謝同事支援,之前我都直接用 Native Linux。

|Design| C++ getInstance

如果你要獲取特定的物件

就可以使用 getInstance()

這樣比全域變數看起來舒心一點

e.g. ————————————————————————

// test.h
class SomeService
{
public:
static SomeService &GetInstance();

}

// test.cpp
SomeService& SomeService::GetInstance()
{
static SomeService instance;
return instance;
}

————————————————————————


Ref. https://blog.csdn.net/fanyunda1988/article/details/51516930

|Job| Application No Response Analysis

ANR 的發生情形,通常是時間到了還沒有回應!所以掌握這個想法,就比較好找 Code。

比較常見有三種情形:

  1. Service Timeout
  2. BroadcastQueue Timeout
  3. ContentProvider Timeout

 

Case 1. Service Timeout

 a. Service 啟動後,會發送 SERVICE_TIMEOUT_MSG 消息
 b. Service 刪除後,就會刪除 SERVICE_TIMEOUT_MSG 消息
 c. 如果 SERVICE_TIMEOUT_MSG 未删除,時間到後就會 ANR

 呼叫 ActiveServices 的 serviceTimeout 方法進行處理,
 serviceTimeout方法邏輯:

 if (anrMessage != null) {
  mAm.appNotResponding(proc, null, null, false, anrMessage);
 }

 

Case 2. BroadcastQueue Timeout

 BroadcastQueue 中的 mHandler 收到 BROADCAST_TIMEOUT_MSG 消息時會觸發

 // 正常要發送消息前
 broadcastTimeoutLocked(false);

 // 發送消息,鎖住
 case BROADCAST_TIMEOUT_MSG: {
  synchronized (mService) {
   broadcastTimeoutLocked(true);

 // ANR
 在 broadcastTimeoutLocked 方法中,首先取得 anrMessage 字串

 anrMessage = “Broadcast of ” + r.intent.toString();

 mHandler.post(new AppNotResponding(app, anrMessage));
 mService.appNotResponding(mApp, null, null, false, mAnnotation);

 

Case 3. ContentProvider Timeout

 MainHandler 的 handleMessage CONTENT_PROVIDER_PUBLISH_TIMEOUT_MSG

 private final void processContentProviderPublishTimedOutLocked(ProcessRecord app) {
cleanupAppInLaunchingProvidersLocked(app, true);
 removeProcessLocked(app, false, true, “timeout publishing content providers”);
 }

 app.kill(reason, true);

 

ref.

https://blog.csdn.net/u012439416/article/category/7261623

 

|Jobs| How to write your own daemon?

要怎樣寫一個服務:他能夠

在一開機啟動、

不易死掉(你可以寫 script 一直啟動他)、

fork() 後終止父行程並和呼叫的終端機(TTY)脫離關係?

 

首先,我們可以寫個服務(執行檔或是一個 script),處理我們要做的事。

接著,我們再準備一個script,放到正確的位置。而這份草稿他的目的最主要就是: 告訴系統,在一開機啟動我們的服務!

以下最主要就是介紹這個 script 該如何寫:



.Method 1: traditional

1. WRITE: /etc/init.d/triggerd
    to run script or daemon: $TRIGGERD_HOME/bin/triggerMyService.sh

 #!/bin/bash
 start_triggerd=$TRIGGERD_HOME/bin/triggerMyService.sh
 stop_triggerd=$TRIGGERD_HOME/bin/triggerMyService.sh

 start() {
  echo -n “Starting MyService”
 }

 stop() {
  echo -n “Shutdown MyService”
 }

 #how we were called
 case “$1” in
  start)
     start
   ;;
  stop)
   stop
   ;;
  restart)
   stop
   sleep 10
   start
   ;;
  *)
   echo “Usage: $0 {start|stop|restart}”
 esac
 exit 0


2. chmod 777 /etc/init.d/triggerd



.Method 2: systemctl

1. 建立一個名為 triggerd.service 的系統服務指令碼
 vim /usr/lib/systemd/system/triggerd.service

 [Unit]
 Description=triggerMyService.sh

 [Service]
 Type=forking
 PIDFile=…
 Environment=CATALINA_HOME=/usr/local/triggerMyService.sh

 ExecStart=/usr/local/triggerMyService.sh start
 ExecStop=/usr/local/triggerMyService.sh stop
 User=…
 Group=…
 PrivateTmp=true

 [Install]
 WantedBy=multi-user.target

2. 重新載入一下配置
 systemctl daemon-reload

3. 設定允許開機自啟動
 systemctl enable triggerd

 #啟動服務
 systemctl start triggerd

 

ref.

創建 Daemon 程式

start-stop-daemon 啟動停止系統守護進程

[開機啟動]Linux開機自啟和運行級別

centos7下配置Tomcat開機啟動(service方式和daemon方式)

Linux systemd 寫 可自動啟動的 Daemon Service

 

|Tools| Useful Linux Commands

.在特定類型的文件裡找字串
grep -ir “ooxx“ –include=”*.java”

.掛載外接硬碟
sudo fdisk -l
mkdir /media/usb
sudo mount -v -t auto /dev/sdb1 /media/usb

.找特定類型的檔案

find -iname *.git

.確認編譯出來檔案的 ELF

readelf

.列出所有執行的東西

ps -Al

|Jobs| 系統廠工程師需要具備的 Linux 相關知識

  • IPC 溝通有許多方式(兩個 process 互相交換資料的方法)
  • 何時要開 proccess,何時用 multi-thread 就好
  • In C language, system()
  • Makefile 如何寫
  • 系統默認路徑
    • Include 系統默認搜尋範圍: /usr/include/usr/local/include
    • Lib 預設目錄: /lib、/usr/lib、/usr/local/lib
      • 環境變數 LD_LIBRARY_PATH 中指向搜索路徑
      • /etc/ld.so.conf 文件中增加搜索目錄
    • 執行檔預設目錄: 如果不加 ./ ,預設是執行 /bin、/usr/bin

WordPress.com.

Up ↑