15AH, San Francisco

California, United States.

Send Your Mail At:

tianyingkejishe@sina.cn

Working Hours

Mon-Sat: 9.30am To 7.00pm

分类标题

Autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et dolore feugait.

分类归档 Linux

【Linux】调度策略和优先级

一、背景

在Linux中,线程是一种轻量级的执行单元,可以在进程内独立运行。线程可以分为普通线程和实时线程,它们之间的区别在于其调度和优先级设置。

  SCHED_OTHER,普通的调度(非实时线程),应用层设置优先级0,调度器总会给此类线程分配一定的CPU资源,只不过是被分配到的频次和时间片长度较少。每1s中实时线程和普通线程的时间比例是95 :5。

  普通线程没有固定的响应时间要求,它们的优先级由系统动态调整。Linux使用CFS调度器来管理普通线程。CFS调度器采用一种称为红黑树的数据结构来维护线程的优先级。每个线程都有一个vruntime值,它表示线程在运行队列中消耗的虚拟时间。CFS调度器会根据线程的vruntime值来确定运行的顺序。优先级较高的线程vruntime值较小,因此能够更早地获得CPU的时间片。

  适用场景:实时性要求不高,但要求必须能被执行的线程。

  SCHED_FIFO,抢占式调度(实时线程),实时先行,应用层设置优先级1-99,同一优先级的多个线程中,一旦某个抢占式线程获取CPU,除非被更高优先级线程抢占(比如在非实时线程中创建一个更高优先级的实时线程),或该线程主动让出CPU资源,否则该线程将会一直占用CPU(但总会分配一点资源给SCHED_OTHER非实时线程)。

  适用场景:实时性要求高,不希望被频繁打断的任务。

  SCHED_RR,轮询式调度(实时线程),实时循环,设置优先级1-99,以循环方式运行,每个线程都有一个时间片来执行任务,时间片耗尽后,该线程将被放入队列的末尾,而较低优先级的线程有机会执行。

  适用场景:实时性要求高,允许被频繁打断的任务。

  在Linux中,可以使用sched_setscheduler函数。这个函数允许我们选择普通线程或实时线程。对于普通线程,可以使用nice函数来动态调整优先级。对于实时线程,可以使用sched_setscheduler函数来设置其类型和优先级。

  关于优先级高低和数值大小的关系,在应用层和内核中二者是相反的。

  设置线程的优先级需要谨慎,因为过高的优先级可能会导致系统资源的过度占用,从而影响其他线程和进程的正常运行。另外,需要注意的是,只有具有足够权限的用户才能设置较高的实时线程优先级。

  总结起来,Linux中的线程分为普通线程和实时线程。普通线程的优先级由系统动态调整,而实时线程的优先级由用户显式设置。通过合理地设置线程的优先级,可以提高系统的性能和响应时间。然而,设置线程的优先级需要慎重考虑,以避免影响其他线程和进程的正常运行。

二、方法

    pthread_t pis_task;
    pthread_attr_t attr;
    struct sched_param sched_param;
// 初始化线程属性
    pthread_attr_init(&attr);

    // 设置线程为实时线程
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // 设置线程优先级
    sched_param.sched_priority = THREAD_PRIORITY;
    pthread_attr_setschedparam(&attr, &sched_param);

    pthread_create(&pis_task, &attr, PrtMgr_Decode_Task, (void *)&PrintMgr_Init_colorId);
    pthread_setname_np(pis_task, "name");

【Linux】打印机环境安装二

前言

传统USB打印机如何实现网络打印机转变,发挥打印机余热。目前好多不用的机顶盒,我们刷成Linux系统。把打印机的USB线插到机顶盒上,机顶盒通过网线或者wifi连接到路由器上,实现USB到网络转换。那路由器Linux到底需要做哪些操作呢,接下来咱们一起聊聊。

一、安装打印机驱动环境

CUPS是开源的打印机服务开源框架,已集成成千上万的各式打印机驱动,我们不用重复造轮子。

1、安装cups

登陆机顶盒调试,会Linux开发的大家都明白。

apt install cups

2、修改 CUPS 配置文件 /etc/cups/cupsd.conf

将 Listen 修改为 0.0.0.0:631

将 Browsing 修改为 Yes

对应位置加上 Allow all

# Only listen for connections from the local machine.
Listen localhost:631 // [!code --]
Listen 0.0.0.0:631 // [!code ++]
Listen /run/cups/cups.sock 
​
# Show shared printers on the local network.
Browsing Off // [!code --]
Browsing Yes // [!code ++]
BrowseLocalProtocols dnssd
​
# Restrict access to the server...

  Order allow,deny
  Allow all // [!code ++]

​
# Restrict access to the admin pages...

  Order allow,deny
  Allow all // [!code ++]

​
# Restrict access to configuration files...

  AuthType Default
  Require user @SYSTEM
  Order allow,deny
  Allow all // [!code ++]

​
# Restrict access to log files...

  AuthType Default
  Require user @SYSTEM
  Order allow,deny
  Allow all // [!code ++]

3、重启cups服务

systemctl restart cups

4、登陆服务器web,进行打印机添加

访问 https://ip:631 进入 CUPS 的 Web UI,点击添加 Add Printer

5、windows添加网络打印机

【Linux】打印机环境安装一

一、环境配置

 sudo apt-get update
 sudo apt-get upgrade
 sudo apt install cups  #公共UNIX打印系统

二、驱动安装

sudo apt-get install hplip

三、打印机安装

hp-setup -i #第一个选0,第二个按p
xiaobao@armbian:~/software/shareprint$ hp-setup -i

HP Linux Imaging and Printing System (ver. 3.23.12)
Printer/Fax Setup Utility ver. 9.0

Copyright (c) 2001-18 HP Development Company, LP
This software comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to distribute it
under certain conditions. See COPYING file for more details.

(Note: Defaults for each question are maked with a '*'. Press <enter> to accept the default.)


--------------------------------
| SELECT CONNECTION (I/O) TYPE |
--------------------------------

  Num       Connection  Description
            Type
  --------  ----------  ----------------------------------------------------------
  0*        usb         Universal Serial Bus (USB)
  1         net         Network/Ethernet/Wireless (direct connection or JetDirect)

Enter number 0...1 for connection type (q=quit, enter=usb*) ? 0

Using connection type: usb

Setting up device: hp:/usb/DeskJet_1110_series?serial=CN75J28090069Z



---------------------
| PRINT QUEUE SETUP |
---------------------


Please enter a name for this print queue (m=use model name:'DeskJet_1110'*, q=quit) ?
Using queue name: DeskJet_1110
Locating PPD file... Please wait.

Found PPD file: hplip:0/ppd/hplip/HP/hp-deskjet_1110_series.ppd
Description:

Note: The model number may vary slightly from the actual model number on the device.

Does this PPD file appear to be the correct one (y=yes*, n=no, q=quit) ?
Enter a location description for this printer (q=quit) ?
Enter additonal information or notes for this printer (q=quit) ?

Adding print queue to CUPS:
Device URI: hp:/usb/DeskJet_1110_series?serial=CN75J28090069Z
Queue name: DeskJet_1110
PPD file: hplip:0/ppd/hplip/HP/hp-deskjet_1110_series.ppd
Location:
Information:


You do not have permission to add a printer. You need authentication.
Username: root
Password:


---------------------
| PRINTER TEST PAGE |
---------------------


Would you like to print a test page (y=yes*, n=no, q=quit) ? y

HP Linux Imaging and Printing System (ver. 3.23.12)
Testpage Print Utility ver. 6.0

Copyright (c) 2001-18 HP Development Company, LP
This software comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to distribute it
under certain conditions. See COPYING file for more details.


HP Linux Imaging and Printing System (ver. 3.23.12)
System Tray Status Service ver. 2.0

Copyright (c) 2001-18 HP Development Company, LP
This software comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to distribute it
under certain conditions. See COPYING file for more details.

warning: No display found.
error: hp-systray requires Qt4 GUI and DBus support. Exiting.
warning: Unable to connect to dbus. Is hp-systray running?
Printing test page to printer DeskJet_1110...
请求 ID 为 DeskJet_1110-1 (1 个文件)
Test page has been sent to printer.

note: If an error occured, or the test page failed to print, refer to the HPLIP website
note: at: http://hplip.sourceforge.net for troubleshooting and support.


Done.

Done.

linux 获取OS盘符的方法

在服务器测试中,对硬盘的测试有很多,有些测试用例需要我们写一下自动化的脚本,比如给服务器所有的盘施加压力,这个就需要我们把除了OS盘的所有盘遍历出来,下面介绍几种方法来筛选OS盘
我们知道,判断os 盘的方法一般是看其有没有boot分区,通常用两个指令来获取:lsblk和df -h 下面一次介绍一下这两种方法的写法

1、 lsblk 如图所示 带有boot 分区的就是OS盘

系统盘是sda

系统盘是nvme0n1

shell命令行:

lsblk -l  |grep -i boot -B 1 |grep -i disk |awk '{print $1}'

筛选除了OS外所有的盘符

lsblk -l  |grep -vw sda |grep -i disk |awk '{print $1}'
lsblk -l  |grep -vw nvme0n1 |grep -i disk |awk '{print $1}'

2、 df -h

系统盘是sda

shell命令行

df -h | awk '{print $1}' | grep -iE "/dev/sd" | sed 's/[0-9]//g' |sort -u |awk -F "/" {'print $NF'}

系统盘是nvme0n1

shell 命令行

 df -h | awk '{print $1}' | grep -iE "/dev/nvme" | sed 's/p[0-9]//g' |sort -u |awk -F "/" {'print $NF'}

对此可以写成一个小脚本来判断OS,代码如下:

#!/bin/bash
bootdisk=`df -h |grep -i boot | awk '{print $1}' | grep -iE "/dev/sd" | sed 's/[0-9]//g' |sort -u|awk -F "/" '{print $NF}'`
if  test -z "$bootdisk"
then
        bootdisk=`df -h |grep -i boot| awk '{print $1}' | grep -iE "/dev/nvme" | sed 's/p[0-9]*//g' |sort -u|awk -F "/" '{print $NF}'`
        echo "os disk os $bootdisk"
else
        echo "os disk is $bootdisk"
fi

运行结果如下图:

【phy】yt8521 phy芯片驱动移植

1、准备驱动文件

2、添加到相应目录

把motorcomm.c、yt8614-phy.h放到Linux的linux-5.19.1/drivers/net/phy/

把motorcomm_phy.h放到Linux的linux-5.19.1/include/linux/

3、修改Makefile

增加:obj-$(CONFIG_MOTORCOMM_PHY) += motorcomm.o

4、修改Kconfig

增加:

config MOTORCOMM_PHY
        tristate "Motorcomm PHYs"
        help
          Enables support for Motorcomm network PHYs.
          Currently supports the YT8511 gigabit PHY.

【安凯】环境搭建

一、配置gcc工具

export PATH="${PATH}:/opt/arm-anykav500-linux-uclibcgnueabi/bin"
临时生效,必须使用绝对路径。

二、libmpfr.so.4: cannot open shared object file: No such file or directory

sudo ln -s /usr/lib/x86_64-linux-gnu/libmpfr.so.6 /usr/lib/x86_64-linux-gnu/libmpfr.so.4

三、yylloc符号错误

……
186
187 extern YYSTYPE yylval;
188 //extern YYLTYPE yylloc;
189 int yyparse (void);
……
1193 /* The lookahead symbol. / 1194 int yychar; 1195 1196 / The semantic value of the lookahead symbol. / 1197 YYSTYPE yylval; 1198 / Location data for the lookahead symbol. / 1199 static YYLTYPE yylloc 1200 # if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL 1201 = { 1, 1, 1, 1 } 1202 # endif 1203 ; 1204 / Number of syntax errors so far. */
1205 int yynerrs;
1206
……

调整yylloc为局部符号.

【安凯】SDK编译时./mkfs.jffs2打包失败问题

一、报错

./mkfs.jffs2: error while loading shared libraries: liblzo2.so.2: cannot open shared object file: No such file or directory

二、解决方法

sudo apt-get update              //更新源,可以先执行第二步,不能安装则尝试更新源
sudo apt-get install liblzo2-dev:i386

Shell 中 kill 掉指定名称的进程

在 Linux 开发中我们时常会遇到对于之前进程 kill 掉,然后再运行当前进程或程序的情况,此时我们是不知道需要 kill 的进程号的,那么就需要通过一个 shell 命令组合来实现这个需求。

实现方法

如下命令可以实现:

ps a | grep -w nameprocess | grep -v grep| cut -c 1-6 | xargs kill -9

说明:

  • 管道符 “|” 用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。
  • “ps a” 是查看所有进程的命令。这时检索出的进程将作为下一条命令 “grep“的输入,注意要结束其它程序时,请将上面命令中的 nameprocess 替换成其它程序名,-w ‘nameprocess’ 强制 PATTERN 仅完全匹配字词。
  • “grep -v grep” 是在列出的进程中去除含有关键字 “grep” 的进程。
  • “cut -c 1-6” 是截取输入行的第 1 个字符到第 6 个字符,而这正好是进程号 PID,或者根据自己实际的情况可以调整字符的截取位置。
  • “xargs kill -9” 中的 xargs 命令是用来把前面命令的输出结果(PID)作为 “kill -9” 命令的参数,并执行该命令。
  • “kill -9” 会强行杀掉指定进程,这样就成功清除了同名进程。

其他方法

ps axu|grep nameprocess | awk '{print "kill -9 "}'

【Linux】分区扩容至整个SD卡

一、查看信息

root@somlabs:~# fdisk -l
省略若干


Disk /dev/mmcblk1: 3.7 GiB, 3965714432 bytes, 7745536 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9de2d76a

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk1p0      16384 1628159 1611776  787M 83 Linux

二、操作分区

假如需要扩展分区为mmcblk0p1

root@somlabs:~# fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/mmcblk1: 3.7 GiB, 3965714432 bytes, 7745536 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9de2d76a

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk1p1      16384 1628159 1611776  787M 83 Linux
这里也看到Start扇区也是16384
Command (m for help): d
把这个分区删除
Partition 1 has been deleted.

Command (m for help): p
列出分区信息Disk /dev/mmcblk1: 3.7 GiB, 3965714432 bytes, 7745536 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9de2d76a

Command (m for help): n
新建分区Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
主分区Partition number (1-4, default 1): 1
分区序号,First sector (2048-7745535, default 2048): 16384
这里要使用起始扇区16384这个值Last sector, +sectors or +size{K,M,G,T,P} (16384-7745535, default 7745535):
默认回车取最大,
Created a new partition 1 of type 'Linux' and of size 3.7 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n
不改变文件分区信号??
Command (m for help): w
写入生效
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

三、重启系统

root@somlabs:~# reboot

四、扩容文件系统

resize2fs /dev/mmcblk1p1
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/mmcblk1p1 is m[  122.795430] EXT4-fs (mmcblk1p1): resizing filesystem from 201472 to 966144 blocks
ounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
[  123.861183] EXT4-fs (mmcblk1p1): resized filesystem to 966144
The filesystem on /dev/mmcblk1p1 is now 966144 (4k) blocks long.

五、查看大小

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.7G  499M  3.0G  15% /
devtmpfs         88M     0   88M   0% /dev
tmpfs           248M     0  248M   0% /dev/shm
tmpfs           248M  3.4M  245M   2% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           248M     0  248M   0% /sys/fs/cgroup
tmpfs            50M     0   50M   0% /run/user/0