|Python| Open a new console by sub-process

How can we open a new console to do another thing at the same time?
(multi-thread programming)
Ans: use “subprocess

def openNewConsole():
    '''open new file in Host consol'''
    log = open("myLogFile.log", "w")
    '''Create a subprocess to do "adb logcat" and let stdout send to "log"'''
    ret = subprocess.Popen("adb logcat -v threadtime -b all", creationflags =subprocess.CREATE_NEW_CONSOLE, stdout=log)
    return ret

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

– run faster than writing as function

– occupy memories

– 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){
    int c;
    c = *a + *b;
    printf("do_something %d \n", c);

int minus(int *a, int *b){
    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);
        printf("in IF!!\n");
        do_something(&m, &n); --------------------------->???
    return 0;


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

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


|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