给公司做网站的公司常州做网站代理商
1 POSIX pthread_create原理
 1)fork()、pthread_create()、vfork()对应的系统调用分别是sys_fork()、sys_clone()、sys_vfork(),它们在内核中都是通过do_fork()实现的。
 2)系统中所有的进程都组织在init_task.tasks链表下面,每个进程的线程组织在每个进程task_sturct->signal的链表下。参考函数for_each_process()和for_each_process_thread()。
 3)根据上述分析,Linux用户态和内核线程都由task_struct结构体描述,是一个轻量级的进程,因此内核中current变量可以表示进程、用户态线程和内核线程。
 2 RTLinux
 2.1 prio和rt_priority的区别
 - 实时进程的优先级(rt_priority)数字越大则优先级越高,99最高,0最低;而普通进程正好相反,优先级(static_prio)数字越大则优先级越低,100最高,139最低(对应nice值-20 ~ 19)
 - 内核调度是按照task_struct中的prio来调度的,prio的值越小,优先级就越高。实时线程的rt_priority转换成prio:prio = MAX_RT_PRIO - 1 - p->rt_priority;普通线程的static_prio转换成prio:prio = static_prio = MAX_RT_PRIO + nice + 20
 - 进程创建时的默认优先级是120,对应的nice值为0
 2.2 priority inheritance
 in kernel/locking/rtmutex.c
 rt_mutex_adjust_prio() - 适用于优先级值小于100的实时任务
 in kernel/sched/core.c
 rt_mutex_setprio()
 2.3 中断线程化
 in kernel/irq/manage.c
 setup_irq()
 2.4 URLs
 https://wiki.linuxfoundation.org/realtime/start
 https://rt.wiki.kernel.org/index.php/Frequently_Asked_Questions
 3 cgroup
 3.1 子系统
 cat /proc/cgroups
 cat /proc/[pid]/cgroup
 3.2 cgroup文件系统
 Linux挂载路径:/sys/fs/cgroup/
 Android挂载路径:/dev/
 4 namespace
 4.1 命名空间
 clone()
 CLONE_NEWNS:Linux第一个引入的namespace,没有想到后面会引入其它的namespace,所以没有叫CLONE_NEWMOUNT
 CLONE_NEWPID:进程
 CLONE_NEWNET:网络,call copy_net_ns()
 CLONE_NEWIPC:IPC通信
 CLONE_MEWUTS:UNIX Time-sharing System,主机名和域名
 CLONE_NEWUSER:用户和用户组
 4.2 每个进程的mount信息
 /proc/{pid}/mounts
 /proc/{pid}/mountinfo
 /proc/{pid}/mountstats
 /proc/{pid}/ns/mnt
 4.3 netns showcase
 external/iproute2/
 change /var/run/netns to /mnt/netns
 ip netns add ns1
 # move eth0 to ns1 namespace
 ip link set eth0 netns ns1
 # ip netns exec ns1 <command>
 ip netns exec ns1 ip link set lo up
 ip netns exec ns1 ifconfig eth0 192.168.0.10/24 up
 ip netns exec ns1 ip route add default gw 192.168.0.1
 # bind process to ns1
 ip netns exec ns1 </path/to/cmd>
 # back to root namespace, 1 means root namespace
 ip netns exec ns1 ip link set eth0 netns 1
 ip netns delete ns1
 ifconfig eth0 192.168.0.10/24 up
 5 LXC使用
 5.1 source
 https://github.com/lxc
 5.2 systemd配置文件
 /lib/systemd/system/
 systemctl list-unit-files | grep enable
 xxx.service path
 systemctl list-unit-files | grep xxx
 systemctl cat xxx.service
 5.3 常用命令
 lxc-ls
 lxc-info -n <container_name>
 lxc-checkconfig
 lxc-attach -n <container_name>
 lxc-start -n <container_name>
 显示container init的PID。
 lxc-info -H -p -n <container_name> -P <PATH>
 lxc-start启动失败时,分析输出log文件:--logfile <file_name>。
 退出container shell命令:exit
 5.4 容器网络
 容器的vethN虚拟网卡被桥接到lxcbr0上。
 5.5 passthrough
 lxc.cgroup.devices.allow = type major:minor access
 lxc.cgroup.devices.allow = b 254:2 rwm  # 其中m表示mknod。
 查看可以access到的设备:
 /sys/fs/cgroup/devices/devices.list
 5.6 Linux container aarch64 adb
 1) adb-arm
 https://github.com/bonnyfone/adb-arm
 2) /lib64/libcrypto.so.1.1
 wget <URL>
 rpm2cpio xxx.rpm | cpio -idmv
 libcrypto.so.1.1 is OpenSSL library.
 6 LXC网络
 6.1 基本概念
 一个物理网络设备最多存在在一个network namespace中。
 net/core/net_namespace.c
 网络命名空间定义了2个链表:pernet_list和net_namespace_list。
 net/core/dev.c
 alloc_netdev_mqs()
 {
     [...]
     // point to the default net namespace
     dev_net_set(dev, &init_net);
     [...]
 }
 6.2 静态物理网络配置
 LXC的host要将eth0分配给container OS,必须在host中先将以太网驱动模块insmod,之后再调用lxc-start启动Container OS,否则启动失败。
 systemctl status systemd-modules-load
 depmod
 /opt/<container_name>/config
 # LXC 2.1后使用如下的网络配置语句。
 lxc.net.1.type = phys
 lxc.net.1.link = eth0
 6.3 动态物理网络配置
 1) src/lxc/tools/lxc_device.c
 call netlink IFLA_NET_NS_PID.
 2) Moves eth0 from the host as eth1 in <conatiner_name>.
 lxc-device -n <conatiner_name> add eth0 eth1
 host和container端ifname不一样:
 ifconfig eth1 192.168.0.5/24 up
 host和container端ifname一样:
 ip addr add 192.168.0.5/24 dev eth0
 3) Moves eth1 from <conatiner_name> as eth0 in the host.
 lxc-device -n <conatiner_name> del eth1 eth0
 6.4 udev自动passthrough
 1) /usr/bin/eth_passthrough.sh
 #!/bin/sh
 /usr/bin/ip link set eth0 netns $(/usr/bin/lxc-info -H -p -n <container_name> -P <PATH>)
 2) /etc/udev/rules.d/100-eth_passthrough.rules
 # udevadm monitor
 # udevadm info -a /sys/class/net/eth0
 # udevadm test /sys/class/net/eth0
 SUBSYSTEM!="net", GOTO="eth_pt_end"
 ACTION!="add", GOTO="eth_pt_end"
 KERNEL!="eth0", GOTO="eth_pt_end"
 #RUN+="/bin/sh -c '/usr/bin/lxc-device -n <container_name> -P <PATH> add eth0 eth0'"
 RUN+="/usr/bin/eth_passthrough.sh"
 LABEL="eth_pt_end"
 3) start udevd with debug mode
 udevd --debug --daemon
 7 Abbreviations
 cgroup:Control Group
 LXC:Linux Container
 MT8666:车载后装,由MT6735改进而来;前装一般使用MT2712
 pi_waiters:priority inheritance,优先级继承
