168 lines
3.9 KiB
Markdown
168 lines
3.9 KiB
Markdown
|
<h1><center>shell并发控制</center></h1>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
------
|
|||
|
|
|||
|
## 一:FD文件描述符
|
|||
|
|
|||
|
#### 1.FD简述
|
|||
|
|
|||
|
FD:文件描述符/文件句柄
|
|||
|
|
|||
|
进程使用文件描述符用来管理进程打开的文件
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# ll /proc/$$/fd
|
|||
|
总用量 0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0
|
|||
|
[root@xingdiancloud ~]# touch file1
|
|||
|
启用自定义文件描述符打开文件
|
|||
|
[root@xingdiancloud ~]# exec 6<> file1
|
|||
|
[root@xingdiancloud ~]# ll /proc/$$/fd
|
|||
|
总用量 0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 6 -> /root/file1
|
|||
|
[root@xingdiancloud ~]# echo "xingdian" > /proc/$$/fd/6
|
|||
|
[root@xingdiancloud ~]# cat file1
|
|||
|
xingdian
|
|||
|
[root@xingdiancloud ~]# rm -rf file1
|
|||
|
[root@xingdiancloud ~]# ll /proc/$$/fd
|
|||
|
总用量 0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 6 -> '/root/file1 (deleted)'
|
|||
|
[root@xingdiancloud ~]# cat /proc/$$/fd/6
|
|||
|
xingdian
|
|||
|
释放文件描述符
|
|||
|
[root@xingdiancloud ~]# exec 6<&-
|
|||
|
[root@xingdiancloud ~]# ll /proc/$$/fd
|
|||
|
总用量 0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 0 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 1 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 2 -> /dev/pts/0
|
|||
|
lrwx------. 1 root root 64 4月 16 23:51 255 -> /dev/pts/0
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
exec打开一个文件
|
|||
|
|
|||
|
exec关闭一个文件(释放文件句柄)
|
|||
|
|
|||
|
当一个文件FD未被释放,删除源文件也不会影响FD
|
|||
|
|
|||
|
#### 2.管道简述
|
|||
|
|
|||
|
匿名管道
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# rpm -qa | grep rpm
|
|||
|
```
|
|||
|
|
|||
|
命令管道
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# mkfifo /tmp/xingdian
|
|||
|
[root@xingdiancloud ~]# file /tmp/xingdian
|
|||
|
/tmp/xingdian: fifo (named pipe)
|
|||
|
[root@xingdiancloud ~]# tty
|
|||
|
/dev/pts/0
|
|||
|
[root@xingdiancloud ~]# rpm -qa > /tmp/xingdian
|
|||
|
|
|||
|
另一个终端
|
|||
|
[root@xingdiancloud ~]# grep bash /tmp/xingdian
|
|||
|
bash-5.1.8-6.el9.x86_64
|
|||
|
bash-completion-2.11-4.el9.noarch
|
|||
|
[root@xingdiancloud ~]# tty
|
|||
|
/dev/pts/1
|
|||
|
```
|
|||
|
|
|||
|
#### 3.FD案例
|
|||
|
|
|||
|
```shell
|
|||
|
[root@xingdiancloud ~]# cat a.sh
|
|||
|
#!/bin/bash
|
|||
|
exec 7<> /etc/hosts
|
|||
|
exec 8<> /etc/hostname
|
|||
|
|
|||
|
while read -u 7 line
|
|||
|
do
|
|||
|
echo $line
|
|||
|
done
|
|||
|
while read -u 8 line2
|
|||
|
do
|
|||
|
echo $line2
|
|||
|
done
|
|||
|
exec 7<&-
|
|||
|
exec 8<&-
|
|||
|
|
|||
|
[root@xingdiancloud ~]# bash a.sh
|
|||
|
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
|
|||
|
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
|
|||
|
xingdiancloud
|
|||
|
```
|
|||
|
|
|||
|
4.并发案例
|
|||
|
|
|||
|
```shell
|
|||
|
#!/usr/bin/bash
|
|||
|
#固定线程
|
|||
|
read -p "请输入线程数量:" diange
|
|||
|
diangefile=/root/diangefile
|
|||
|
mkfifo $diangefile //虚拟管道
|
|||
|
exec 8<>$diangefile //文件描述
|
|||
|
rm -rf $diangefile
|
|||
|
|
|||
|
for i in `seq $diange`
|
|||
|
do
|
|||
|
echo >&8 //将内容写入到描述文件中,写如了空行
|
|||
|
done
|
|||
|
|
|||
|
for i in {1..100}
|
|||
|
do
|
|||
|
read -u 8 //read 读取描述文件中的内容
|
|||
|
{
|
|||
|
ip=192.168.101.$i
|
|||
|
ping -c1 $ip &>/dev/null
|
|||
|
if [ $? -eq 0 ];then
|
|||
|
echo "$ip is up"
|
|||
|
else
|
|||
|
echo "$ip is down"
|
|||
|
|
|||
|
fi
|
|||
|
echo >&8 //等到进程执行完之后再往管道里丢内容以支持后面的循环
|
|||
|
}&
|
|||
|
done
|
|||
|
wait
|
|||
|
echo "完成"
|
|||
|
exec 8<&- //释放描述文件
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|