|Code| LLVM initial

One day, if you want to implement a module on the platform x86_64 for arm64, you will need to know the tool chain, “cross compiler”.
The famous modern compiler is LLVM.

Why should we understand the compiler?
– build for multi-platform
– improve the efficiency of your code
– …..

I have a conclusion for the developer whose library is under the user space.
Use suitable optimization provided by LLVM, and make your code readable.
Dazzle coding is meaningless.

你所不知道的 C 語言:編譯器和最佳化原理篇
[共筆] 不深不淺,帶你認識 LLVM
“編譯器 LLVM 淺淺玩” by Pokai Chang

|Design| Communicate with Kernel driver

User space can not exchange data with kernel space directly.
They need to use system call.
e.g. fopen, ioctl, write, read …… etc.

In Kernel Space ……
1. Register your device with “register_chrdev”, defined in linux/fs.h .
 https://ithelp.ithome.com.tw/articles/10159749
2. Implement driver functions, ioctl, open,…
 struct file_operations fops = {
  .owner = THIS_MODULE,
  .read = device1_read,
  .write = device1_write,
  .ioctl = device1_ioctl,
  .open = device1_open,
  .release = device1_release,
 };

 int ioctl(struct inode *, struct file *, unsigned int, unsigned long);
 is in linux/ioctl.h
 http://ccckmit.wikidot.com/lk:io
 開發 driver 需要的基礎知識
 user space/kernel space 的IO觀念及實作

Copy data ……
 Kernel Space to User Space: copy_to_user()
 User Space to Kernel Space: copy_from_user()

In User Space ……
Use system call to control kernel driver.
 fopen (open)
 write
 read
 close
 seek
 poll / select
 ioctl
 mmap
 fcntl


e.g.

int main(int argc, char *argv[]){
  int devfd;
  int num = 0;

  if (argc > 1) num = atoi(argv[1]);
  if (num < 0) num = 0xff;

  devfd = open("/dev/debug", O_RDONLY);
  if (devfd == -1) {
    printf("Can't open /dev/debug\n");
    return -1;
  }

  printf("Write 0x%02x...\n", num);
  ioctl(devfd, IOCTL_WRITE, num);
  printf("Done. Wait 5 seconds...\n");
  sleep(5);
  close(devfd);

  return 0;
}

|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