|Design| Benefits of using Macro

What is the Macro?
If you write as “#define” in your code, that is “Macro” .

Macro will handle by the preprocessor.
You can think Macro as a method to replace the String which appears in you code..

pros:
– run faster than writing as function

cons:
– occupy memories

concepts:
– there is no the concept of pointer in macro.
just “replacing the string”

An question:
What is the output of the following code?

#define macro_to_func(pp) ({printf("in Macro ...\n"); minus ;})

void do_something(int *a, int *b){
    printf("do_something\n");
    int c;
    c = *a + *b;
    printf("do_something %d \n", c);
}

int minus(int *a, int *b){
    printf("minus\n");
    int c;
    c = *a - *b +1;
    printf("minus a = %d \n", *a);
    printf("minus b = %d \n", *b);
    printf("minus %d \n", c);
    return c;
}

int main()
{
    int (*do_something)(int* c, int* d);
    //printf("fptr 1 do_something %p\n", &do_something);

    int m = -1;
    int n = 7;

    printf("do_something = macro_to_func\n");
    do_something = macro_to_func(pp);
    //printf("fptr minus %p\n", &minus);
    //printf("fptr 2 do_something %p\n", &do_something);
    if(do_something){
        printf("in IF!!\n");
        do_something(&m, &n); --------------------------->???
    }
    return 0;
}

Answer:

do_something = macro_to_func
in Macro …
in IF!!
minus
minus a = -1
minus b = 7
minus -7

Explanation:
“int (*do_something)” is a local function pointer.
In the macro, the macro_to_func will be replaced as “minus”, but do_something !!!

|Code| Processes communicate by IPC methods

Threads can shares the same resources such as address space, file descriptors, stack and other process related attributes in the same process.
BUT Processes can not share resources directly.

Processes will communicate to each other by IPC method.
There are two ways:
 1. Shared Memory
 2. Message passing: Socket, FIFO, POSIX message, dbus

The following methods, which we often see, belong to message passing.
Pipe
Socket
Remote Procedural calls (RPCs)


Originally, I though it is a basic interview question and everyone has at least a common answer .
Until I found someone did not know , I know I was wrong.



https://www.geeksforgeeks.org/inter-process-communication-ipc/
https://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-thread

|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| 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