
[小众限速]Centos7限速和测速
Unix/LinuxC技术 jackxiang 2022-1-11 15:45
背景:有时你想测试一下传说中的微服务如熔断,服务器的接口超时(尤其是上传姑且叫大文件),这个rpm包应该是有用的。
限速
wondershaper是国外人开发的一款在Linux内核下基于TC工具的对整块网卡的限度工具。
第一种安装方法
首先下载wondershaper的rpm安装包:wondershaper-1.1a-7.noarch.rpm
安装wondershaper:
[root@localhost ~]# rpm -ivh wondershaper-1.1a-7.noarch.rpm
第二种安装方法
由于Centos7第三方库内才有这个软件,先安装第三方库
yum install epel-release -y
yum install wondershaper -y
限速命令:
第一个参数为网卡接口名,后面两个数字分别代表上行和下行的速度(单位Kb)
[root@localhost ~]# wondershaper eth0 5000 500
取消限速:
[root@localhost ~]# wondershaper clear eth0
来自:https://www.shuzhiduo.com/A/LPdo61bg53/
限速
wondershaper是国外人开发的一款在Linux内核下基于TC工具的对整块网卡的限度工具。
第一种安装方法
首先下载wondershaper的rpm安装包:wondershaper-1.1a-7.noarch.rpm
安装wondershaper:
[root@localhost ~]# rpm -ivh wondershaper-1.1a-7.noarch.rpm
第二种安装方法
由于Centos7第三方库内才有这个软件,先安装第三方库
yum install epel-release -y
yum install wondershaper -y
限速命令:
第一个参数为网卡接口名,后面两个数字分别代表上行和下行的速度(单位Kb)
[root@localhost ~]# wondershaper eth0 5000 500
取消限速:
[root@localhost ~]# wondershaper clear eth0
来自:https://www.shuzhiduo.com/A/LPdo61bg53/
[实践OK]Alfred 修改内置 Terminal 为 iTerm2
Unix/LinuxC技术 jackxiang 2021-12-10 16:06
Alfred是Mac下一个非常强大的工具,网上有许多如何使用的教程,我目前也只是会使用一些基本功能。其中一个我特别喜欢的功能就是直接可以在Alfred里面输入shell命令(默认需要在命令前家>符号,表示是shell命令),然后就会自动打开终端执行。但这里有个问题就是Alfred默认使用Mac自带的Terminal,而大多数在Mac经常使用终端的一般都会使用iTerm2,因为它配合zsh+oh my zsh,真的是非常强大。对于目前最新的iTerm 3.x版本,可以通过如下方法将Alfred内置的Terminal改为iTerm2:
打开Alfred的"Preferences->features->Terminal/shell",将"Application"选为"custom",然后将下面的编辑框中的脚本内容替换为下面的
需要注意的是不同的iTerm2版本脚本内容不太一样,该脚本适用于最新的iTerm2版本,即iTerm2 3.0.
实践来自:https://www.cxymm.net/article/qq_29855509/120960802
打开Alfred的"Preferences->features->Terminal/shell",将"Application"选为"custom",然后将下面的编辑框中的脚本内容替换为下面的
需要注意的是不同的iTerm2版本脚本内容不太一样,该脚本适用于最新的iTerm2版本,即iTerm2 3.0.
实践来自:https://www.cxymm.net/article/qq_29855509/120960802
sudo codesign --force --deep --sign - /Applications/PhotoScapeX.app
修复失败。
修复失败。
强制关掉一些进程后,方可使用zsh,发现有一个进程开了太多的进程:
ps -ef|grep UltraSearchSyste|wc -l
3594
zsh出现 :fork failed: resource temporarily unavailable,且启动不了程序了。
ps -ef|grep UltraSearchSyste|head -1
0 105 1 0 四05下午 ?? 9:44.65 /var/root/.UltraSearchSystem/UltraSearchSystemDaemon pd
0 98562 105 0 3:41下午 ?? 0:00.00 (UltraSearchSyste)
0 98569 105 0 9:05下午 ?? 0:00.00 (UltraSearchSyste)
开机启动位置,以为它是:
ls ~+/com.UltraSearchSystemDaemon.plist
cat ~+/com.UltraSearchSystemDaemon.plist
其实,它不是,是它,符合位置和pd参数:
/Library/LaunchDaemons/com.UltraSearchSystemP.plist
解决开机启动问题:
sudo rm -rf ~+/com.UltraSearchSystemP.plist
Password:
来自:
如何删除mac上的ultrasearchapp?
https://www.zhihu.com/question/400059869
再次参考:https://www.makeuseof.com/tag/hidden-launchdaemons-launchagents-mac/
来自:https://www.pcrisk.com/removal-guides/18893-ultrasearchsystem-adware-mac
ps -ef|grep UltraSearchSyste|wc -l
3594
zsh出现 :fork failed: resource temporarily unavailable,且启动不了程序了。
ps -ef|grep UltraSearchSyste|head -1
0 105 1 0 四05下午 ?? 9:44.65 /var/root/.UltraSearchSystem/UltraSearchSystemDaemon pd
0 98562 105 0 3:41下午 ?? 0:00.00 (UltraSearchSyste)
0 98569 105 0 9:05下午 ?? 0:00.00 (UltraSearchSyste)
开机启动位置,以为它是:
ls ~+/com.UltraSearchSystemDaemon.plist
cat ~+/com.UltraSearchSystemDaemon.plist
其实,它不是,是它,符合位置和pd参数:
/Library/LaunchDaemons/com.UltraSearchSystemP.plist
解决开机启动问题:
sudo rm -rf ~+/com.UltraSearchSystemP.plist
Password:
来自:
如何删除mac上的ultrasearchapp?
https://www.zhihu.com/question/400059869
再次参考:https://www.makeuseof.com/tag/hidden-launchdaemons-launchagents-mac/
来自:https://www.pcrisk.com/removal-guides/18893-ultrasearchsystem-adware-mac
执行命令:sudo su -
source ~/.zshrc
compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask
出现:compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask
解决办法:
vim /usr/local/share/zsh/site-functions/_brew_cask
来自:https://blog.csdn.net/shentian885/article/details/115902464
source ~/.zshrc
compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask
出现:compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew_cask
解决办法:
vim /usr/local/share/zsh/site-functions/_brew_cask
来自:https://blog.csdn.net/shentian885/article/details/115902464
[实践OK]cat命令查看ps看不到的整个命令行和相关环境变量。
Unix/LinuxC技术 jackxiang 2021-6-25 10:40
ps只能看到部分:
root 3195 2205 0 Jun19 ? 00:00:13 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/0a8be205faf5aa1500e55e83e36a3027c626ae
cat查看:
#cat /proc/3195/cmdline
containerd-shim-namespacemoby-workdir/var/lib/containerd/io.containerd.runtime.v1.linux/moby/0a8be205faf5aa1500e55e83e36a3027c626aea234747a51c949ffb1a9f95c83-address/run/containerd/containerd.sock-containerd-binary/usr/bin/containerd-runtime-root/var/run/docker/runtime-runc-systemd-cgroup
二、执行程序的软链接:
#ls -lart /proc/3195/exe
lrwxrwxrwx 1 root root 0 Jun 25 10:06 /proc/3195/exe -> /usr/bin/containerd-shim
三、环境变量:
#cat /proc/3195/environ
LANG=en_US.UTF-8PATH=/opt/containerd/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binLD_LIBRARY_PATH=:/opt/containerd/libGOMAXPROCS=2
四、对应的fd编号:
#ls /proc/3195/fd
0 1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9
root 3195 2205 0 Jun19 ? 00:00:13 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/0a8be205faf5aa1500e55e83e36a3027c626ae
cat查看:
#cat /proc/3195/cmdline
containerd-shim-namespacemoby-workdir/var/lib/containerd/io.containerd.runtime.v1.linux/moby/0a8be205faf5aa1500e55e83e36a3027c626aea234747a51c949ffb1a9f95c83-address/run/containerd/containerd.sock-containerd-binary/usr/bin/containerd-runtime-root/var/run/docker/runtime-runc-systemd-cgroup
二、执行程序的软链接:
#ls -lart /proc/3195/exe
lrwxrwxrwx 1 root root 0 Jun 25 10:06 /proc/3195/exe -> /usr/bin/containerd-shim
三、环境变量:
#cat /proc/3195/environ
LANG=en_US.UTF-8PATH=/opt/containerd/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binLD_LIBRARY_PATH=:/opt/containerd/libGOMAXPROCS=2
四、对应的fd编号:
#ls /proc/3195/fd
0 1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9
[实践OK]Mac 上 Oh My Zsh 无法更新
Unix/LinuxC技术 jackxiang 2021-6-24 09:45
mac下面sudo su - 后出现:
sudo su
Password:
[Oh My Zsh] Would you like to update? [Y/n]: y
Updating Oh My Zsh
error: 不能变基式拉取:您有未暂存的变更。
error: 请提交或贮藏它们。
There was an error updating. Try again later?
最后我的解决办法是:
cd ~/.oh-my-zsh/
git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git restore <文件>..." 丢弃工作区的改动)
类型变更: plugins/fedora/fedora.plugin.zsh
类型变更: plugins/go/go.plugin.zsh
类型变更: plugins/gradle/_gradle
类型变更: plugins/gradle/_gradlew
类型变更: plugins/per-directory-history/per-directory-history.plugin.zsh
git reset --hard origin/master #强制让本地的代码和远端一致
HEAD 现在位于 08da19e3 emacs: revert cab1ac6e682038b2ceca9d16f8c458c0e79149ca (#7765)
git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
git pull
remote: Enumerating objects: 9644, done.
root@bogon# upgrade_oh_my_zsh
Note: `upgrade_oh_my_zsh` is deprecated. Use `omz update` instead.
Updating Oh My Zsh
From https://github.com/ohmyzsh/ohmyzsh
* branch master -> FETCH_HEAD
Current branch master is up to date.
__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/
Oh My Zsh is already at the latest version.
To keep up with the latest news and updates, follow us on Twitter: https://twitter.com/ohmyzsh
Want to get involved in the community? Join our Discord: https://discord.gg/ohmyzsh
Get your Oh My Zsh swag at: https://shop.planetargon.com/collections/oh-my-zsh
参考:https://www.chendalei.com/articles/2018/10/09/1539052284615.html
提示:
Updating Oh My Zsh
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.
There was an error updating. Try again later?
这种情况很可能发生,因为您已经在.oh-my-zsh目录中编辑了一些内容但尚未提交。
导航到该目录(.oh-my-zsh/目录)。
运行git status。 它应该显示已更改的文件列表。
使用git add暂存这些文件
使用git commit -m提交这些更改
你现在应该可以升级了。
可选:在步骤2之后,您可以运行git commit -a -m,它应该暂存所有未暂存的文件并提交这些更改(组合步骤3和4)。
然后通过upgrade_oh_my_zsh命令升级zsh。
参考来源:https://github.com/robbyrussell/oh-my-zsh/issues/1984
sudo su
Password:
[Oh My Zsh] Would you like to update? [Y/n]: y
Updating Oh My Zsh
error: 不能变基式拉取:您有未暂存的变更。
error: 请提交或贮藏它们。
There was an error updating. Try again later?
最后我的解决办法是:
cd ~/.oh-my-zsh/
git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git restore <文件>..." 丢弃工作区的改动)
类型变更: plugins/fedora/fedora.plugin.zsh
类型变更: plugins/go/go.plugin.zsh
类型变更: plugins/gradle/_gradle
类型变更: plugins/gradle/_gradlew
类型变更: plugins/per-directory-history/per-directory-history.plugin.zsh
git reset --hard origin/master #强制让本地的代码和远端一致
HEAD 现在位于 08da19e3 emacs: revert cab1ac6e682038b2ceca9d16f8c458c0e79149ca (#7765)
git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
git pull
remote: Enumerating objects: 9644, done.
root@bogon# upgrade_oh_my_zsh
Note: `upgrade_oh_my_zsh` is deprecated. Use `omz update` instead.
Updating Oh My Zsh
From https://github.com/ohmyzsh/ohmyzsh
* branch master -> FETCH_HEAD
Current branch master is up to date.
__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/
Oh My Zsh is already at the latest version.
To keep up with the latest news and updates, follow us on Twitter: https://twitter.com/ohmyzsh
Want to get involved in the community? Join our Discord: https://discord.gg/ohmyzsh
Get your Oh My Zsh swag at: https://shop.planetargon.com/collections/oh-my-zsh
参考:https://www.chendalei.com/articles/2018/10/09/1539052284615.html
提示:
Updating Oh My Zsh
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.
There was an error updating. Try again later?
这种情况很可能发生,因为您已经在.oh-my-zsh目录中编辑了一些内容但尚未提交。
导航到该目录(.oh-my-zsh/目录)。
运行git status。 它应该显示已更改的文件列表。
使用git add暂存这些文件
使用git commit -m提交这些更改
你现在应该可以升级了。
可选:在步骤2之后,您可以运行git commit -a -m,它应该暂存所有未暂存的文件并提交这些更改(组合步骤3和4)。
然后通过upgrade_oh_my_zsh命令升级zsh。
参考来源:https://github.com/robbyrussell/oh-my-zsh/issues/1984
解决办法:
下载VCRUNTIME140.dll文件;微软官网:下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=52685
下载真实地址及名称:vc_redist.x64.exe
https://download.microsoft.com/download/0/6/4/064F84EA-D1DB-4EAA-9A5C-CC2F0FF6A638/vc_redist.x64.exe
下载VCRUNTIME140.dll文件;微软官网:下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=52685
下载真实地址及名称:vc_redist.x64.exe
https://download.microsoft.com/download/0/6/4/064F84EA-D1DB-4EAA-9A5C-CC2F0FF6A638/vc_redist.x64.exe
[实践OK]C语言函数指针再学习。
Unix/LinuxC技术 jackxiang 2021-5-10 09:49
此次学习来源:https://mp.weixin.qq.com/s/YqZI4XUlsyPpaGIjy0pG3A
#cat p.c
#./p
value= 520
cat pp.c
#./pp
cat ppp.c
#./ppp
value0 = 1
value1 = 2
value2 = 3
temp[0] = 1
temp[1] = 2
temp[2] = 3
以前的学习:
https://jackxiang.com/post/7485/
#cat p.c
#./p
value= 520
cat pp.c
#./pp
cat ppp.c
#./ppp
value0 = 1
value1 = 2
value2 = 3
temp[0] = 1
temp[1] = 2
temp[2] = 3
以前的学习:
https://jackxiang.com/post/7485/
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
真实下载地址:https://download.oracle.com/otn/java/jdk/8u291-b10/d7fc238d0cbf4b0dac67be84580cfb4b/jdk-8u291-macosx-x64.dmg
源码包下载地址:https://download.oracle.com/otn-pub/java/jdk/16.0.1+9/7147401fd7354114ac51ef3e1328291f/jdk-16.0.1_osx-x64_bin.tar.gz?AuthParam=1620358646_aa79aa4a7788c49057533e4b661b7f35
下载前的用户名密码:
https://jackxiang.com/post/6461/
来自:https://zhuanlan.zhihu.com/p/34915988
start.sh
真实下载地址:https://download.oracle.com/otn/java/jdk/8u291-b10/d7fc238d0cbf4b0dac67be84580cfb4b/jdk-8u291-macosx-x64.dmg
源码包下载地址:https://download.oracle.com/otn-pub/java/jdk/16.0.1+9/7147401fd7354114ac51ef3e1328291f/jdk-16.0.1_osx-x64_bin.tar.gz?AuthParam=1620358646_aa79aa4a7788c49057533e4b661b7f35
下载前的用户名密码:
https://jackxiang.com/post/6461/
来自:https://zhuanlan.zhihu.com/p/34915988
start.sh
[实践OK]Centos8环境中配置PHP出现configure: error: Package requirements (oniguruma) were not met
Php/Js/Shell/Go jackxiang 2021-3-3 23:45
在Centos8环境中配置PHP出现如上报错:
原因是没有安装配置oniguruma导致的
解决方法如下:
1、获取源码,2.并解压
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
备用下载: http://down.24kplus.com/linux/oniguruma/oniguruma-6.9.4.tar.gz
3、编译安装
./autogen.sh && ./configure --prefix=/usr
make && make install
如果是Centos7环境中的话:
解决方法如下:
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-6.7.0-1.el7.x86_64.rpm
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
备用下载:
oniguruma:http://down.24kplus.com/linux/oniguruma/oniguruma-6.7.0-1.el7.x86_64.rpm
oniguruma-devel:http://down.24kplus.com/linux/oniguruma/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
————————————————
版权声明:本文为CSDN博主「薄凉小伙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Kangshuo2471781030/article/details/107241779
原因是没有安装配置oniguruma导致的
解决方法如下:
1、获取源码,2.并解压
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
备用下载: http://down.24kplus.com/linux/oniguruma/oniguruma-6.9.4.tar.gz
3、编译安装
./autogen.sh && ./configure --prefix=/usr
make && make install
如果是Centos7环境中的话:
解决方法如下:
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-6.7.0-1.el7.x86_64.rpm
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
备用下载:
oniguruma:http://down.24kplus.com/linux/oniguruma/oniguruma-6.7.0-1.el7.x86_64.rpm
oniguruma-devel:http://down.24kplus.com/linux/oniguruma/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
————————————————
版权声明:本文为CSDN博主「薄凉小伙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Kangshuo2471781030/article/details/107241779
[实践OK]python获取传入argv参数列表,以及字符为空判断,python退出之退出os._exit(-1)。
Php/Js/Shell/Go jackxiang 2021-2-23 17:43
python excelsearchKeyList.py k.txt
sys.argv[0] = excelsearchKeyList.py
sys.argv[1] = k.txt
二)判断字符为空:
python excelsearchKeyList.py k.txt
原创视频
line is null
line is null
三)判断是否传入新的参数:
来自:https://blog.csdn.net/index20001/article/details/74294945#:~:text=Python%E7%A8%8B%E5%BA%8F%E6%9C%89%E4%B8%A4%E7%A7%8D,%E8%A7%A3%E9%87%8A%E5%99%A8%E5%B0%86%E4%BC%9A%E9%80%80%E5%87%BA%E3%80%82
[实践OK]CentOS8下ssh登陆出现[error] character map file `UTF-8' not found: No such file or directory
Unix/LinuxC技术 jackxiang 2021-2-3 17:55
问题:
rpm -qa *langpack*
cat /etc/locale.conf
LC_ALL="zh_CN.utf8"
LC_CTYPE="zh_CN.utf8"
LANG="zh_CN.utf8"
解决办法:
yum install glibc-langpack-zh -y
rpm -qa *langpack*
cat /etc/locale.conf
LC_ALL="zh_CN.utf8"
LC_CTYPE="zh_CN.utf8"
LANG="zh_CN.utf8"
解决办法:
yum install glibc-langpack-zh -y
一、现象
Linux关机或重启时提示A stop job is running for ..
导致关机慢。
二、解决方法
编辑/etc/systemd/system.conf
修改下面两个变量为:
DefaultTimeoutStartSec=10s
DefaultTimeoutStopSec=10s
执行:systemctl daemon-reload
来自:https://blog.csdn.net/penge199761/article/details/79136826
Linux关机或重启时提示A stop job is running for ..
导致关机慢。
二、解决方法
编辑/etc/systemd/system.conf
修改下面两个变量为:
DefaultTimeoutStartSec=10s
DefaultTimeoutStopSec=10s
执行:systemctl daemon-reload
来自:https://blog.csdn.net/penge199761/article/details/79136826
[实践OK]用c语言的语法逻辑进行判断是不是质数的重构逻辑学习。
Unix/LinuxC技术 jackxiang 2021-1-14 10:53
质数定义:只有1和它本身两个约数,除此之外再没有其它约数。
示例1:
make zs
cc zs.c -o zs
#./zs
质数
示例2:
for循环介入:
引入scanf:
#./zs
5
质数
#./zs
6
合数
打印出约数:
#./zs
5
质数
#./zs
6
2 3 合数
更快一点,引入break:
#./zs
6
合数
#./zs
1000
合数
任一大于2的偶数都可以写成两个质数之和,采用部分穷举法,可以从1-100,1-10000啥的,但无法穷举所有:
#./zs
100
4=2+2
6=3+3
8=3+5
10=3+7
12=5+7
14=3+11
16=3+13
18=5+13
20=3+17
22=3+19
24=5+19
26=3+23
28=5+23
30=7+23
32=3+29
34=3+31
36=5+31
...
示例1:
make zs
cc zs.c -o zs
#./zs
质数
示例2:
for循环介入:
引入scanf:
#./zs
5
质数
#./zs
6
合数
打印出约数:
#./zs
5
质数
#./zs
6
2 3 合数
更快一点,引入break:
#./zs
6
合数
#./zs
1000
合数
任一大于2的偶数都可以写成两个质数之和,采用部分穷举法,可以从1-100,1-10000啥的,但无法穷举所有:
#./zs
100
4=2+2
6=3+3
8=3+5
10=3+7
12=5+7
14=3+11
16=3+13
18=5+13
20=3+17
22=3+19
24=5+19
26=3+23
28=5+23
30=7+23
32=3+29
34=3+31
36=5+31
...
[实践OK]关于go进行多进程通讯的等待子进程问题学习浅显研究。
Php/Js/Shell/Go jackxiang 2021-1-4 18:53
背景:有一篇朋友圈说是一个k8s里面Server的Pod销毁上报Zookeeper出现IP地址连接还是原来的,用到了zk的watcher,理论上不应该这样,于是查来查去,其大体意思就是说用了Go的多进程,有一个进程去读取配置了,因为配置还是老的,而用到了Go的多进程,于是文末那代码,我看了下,简单,但不明白,于是顺藤摸瓜学习一下这个rsync的原理。
——————————
#tree -L 2 .|grep go
├── loop.go
├── pipe.go
├── waitgrouppointer.go
└── wg.go
#cat loop.go
package main
import (
"fmt"
//"time"
)
func main(){
for i := 0; i < 100 ; i++{
go fmt.Println(i)
}
//time.Sleep(time.Second)
}
这个说是运行太快,子进程没输出主进程就退了,啥也不输出。
#cat pipe.go
package main
import (
"fmt"
//"time"
)
func main() {
c := make(chan bool, 100)
for i := 0; i < 100; i++ {
go func(i int) {
fmt.Println(i)
c <- true
}(i)
}
for i := 0; i < 100; i++ {
<-c
}
}
这个说是管道要是上万,会耗尽系统管道资源。
#cat waitgroup.go
package main
import (
"fmt"
"sync"
//"time"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go func(i int) {
fmt.Println(i)
wg.Done()
}(i)
}
wg.Wait()
}
这个说是挺好的,因为它用到了原子加减法,这里好像C语言也有类似的函数,
再就是说它是用来阻塞方进程的,WaitGroup 对象内部有一个计数器,最初从0开始,它有三个方法:Add(), Done(), Wait() 用来控制计数器的数量。Add(n) 把计数器设置为n ,Done() 每次把计数器-1 ,wait() 会阻塞代码的运行,直到计数器地值减为0。
这里首先把wg 计数设置为100, 每个for循环运行完毕都把计数器减一,主函数中使用Wait() 一直阻塞,直到wg为零——也就是所有的100个for循环都运行完毕。相对于使用管道来说,WaitGroup 轻巧了许多。
最后,如果要搞裂成函数,得用引用传入,指针变量:
WaitGroup对象不是一个引用类型
WaitGroup对象不是一个引用类型,在通过函数传值的时候需要使用地址:
#cat waitgrouppointer.go
package main
import (
"fmt"
"sync"
//"time"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go f(i, &wg)
}
wg.Wait()
}
// 一定要通过指针传值,不然进程会进入死锁状态
func f(i int, wg *sync.WaitGroup) {
fmt.Println(i)
wg.Done()
}
以上来自:https://blog.csdn.net/u013474436/article/details/88749749
溯源来自:https://mp.weixin.qq.com/s/s9G6yDqfeFepbKT6DJASng
#cat wg.go
package main
import (
"fmt"
"sync"
)
func main() {
ok := true
for i := 0; i <1000; i ++ {
var arr []int
wg := sync.WaitGroup{}
for j := 0; j <2; j ++ {
wg.Add(1)
go func() {
defer wg.Done()
arr = append(arr, i)
}()
}
wg.Wait()
if len(arr) < 2 {
fmt.Printf("error:%d \n", i)
ok = false
break
}
}
if ok {
fmt.Println("ok")
}
}
阅读全文
——————————
#tree -L 2 .|grep go
├── loop.go
├── pipe.go
├── waitgrouppointer.go
└── wg.go
#cat loop.go
package main
import (
"fmt"
//"time"
)
func main(){
for i := 0; i < 100 ; i++{
go fmt.Println(i)
}
//time.Sleep(time.Second)
}
这个说是运行太快,子进程没输出主进程就退了,啥也不输出。
#cat pipe.go
package main
import (
"fmt"
//"time"
)
func main() {
c := make(chan bool, 100)
for i := 0; i < 100; i++ {
go func(i int) {
fmt.Println(i)
c <- true
}(i)
}
for i := 0; i < 100; i++ {
<-c
}
}
这个说是管道要是上万,会耗尽系统管道资源。
#cat waitgroup.go
package main
import (
"fmt"
"sync"
//"time"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go func(i int) {
fmt.Println(i)
wg.Done()
}(i)
}
wg.Wait()
}
这个说是挺好的,因为它用到了原子加减法,这里好像C语言也有类似的函数,
再就是说它是用来阻塞方进程的,WaitGroup 对象内部有一个计数器,最初从0开始,它有三个方法:Add(), Done(), Wait() 用来控制计数器的数量。Add(n) 把计数器设置为n ,Done() 每次把计数器-1 ,wait() 会阻塞代码的运行,直到计数器地值减为0。
这里首先把wg 计数设置为100, 每个for循环运行完毕都把计数器减一,主函数中使用Wait() 一直阻塞,直到wg为零——也就是所有的100个for循环都运行完毕。相对于使用管道来说,WaitGroup 轻巧了许多。
最后,如果要搞裂成函数,得用引用传入,指针变量:
WaitGroup对象不是一个引用类型
WaitGroup对象不是一个引用类型,在通过函数传值的时候需要使用地址:
#cat waitgrouppointer.go
package main
import (
"fmt"
"sync"
//"time"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go f(i, &wg)
}
wg.Wait()
}
// 一定要通过指针传值,不然进程会进入死锁状态
func f(i int, wg *sync.WaitGroup) {
fmt.Println(i)
wg.Done()
}
以上来自:https://blog.csdn.net/u013474436/article/details/88749749
溯源来自:https://mp.weixin.qq.com/s/s9G6yDqfeFepbKT6DJASng
#cat wg.go
package main
import (
"fmt"
"sync"
)
func main() {
ok := true
for i := 0; i <1000; i ++ {
var arr []int
wg := sync.WaitGroup{}
for j := 0; j <2; j ++ {
wg.Add(1)
go func() {
defer wg.Done()
arr = append(arr, i)
}()
}
wg.Wait()
if len(arr) < 2 {
fmt.Printf("error:%d \n", i)
ok = false
break
}
}
if ok {
fmt.Println("ok")
}
}
