Linux 进程获取并实现多个进程排队执行

5 篇文章 0 订阅

在实际应用中,发现如果多用户多核编译,会造成内存和CPU占用过多,导致系统卡死或者编译失败。那么如何实现编译排队呢?

即,一个时间内只允许一个用户进程执行编译,检测到多余1个进程在编译时,当前用户的编译进程先等待,直到其它进程退出或者完成后再执行。

先来了解一下原理:

比如我们看bash进程,ps -u,并且,如果bash 进程超过5时,退出软件执行;小于5个时,等待检查;当只有1 个bash进程时,就跳出循环。

可以将此函数嵌入到自己的程序中,用于检查特定进程是否存在,不满足条件就等待,满足条件就执行,实现进程执行的排队。

ProcessSequenceCheck()
{

	## Creat log 
	echo "### Start now !!! ###"
	SingleProcessLog=SingleProcessLog_build.log

	ProceeDiscribe="bash"  ##### change the name you need

	pid=$(ps -au|grep $ProceeDiscribe |grep -v grep| awk '{print $2}')

	echo "Search Process Name:${ProceeDiscribe}">>$SingleProcessLog
	echo "All the PID list is :${pid}">>$SingleProcessLog

	arr_pid=($pid)
	sequence=${#arr_pid[*]}
	echo "${sequence} build missions are running.">>$SingleProcessLog
	echo "The running build missions' PID: ${arr_pid[*]}">>$SingleProcessLog


	if [ ${sequence} -gt 5 ];then
	
		echo "***************There are more than 5 missions in waitting sequence.Please try later.***************"
	else 
		echo "                                                                                "
		echo "***************Waiting: Another ${sequence} build missions are in waitting sequence.***************"
        echo "***************Waiting: Another ${sequence} build missions are in waitting sequence.***************">>$SingleProcessLog
		echo "                                                                                "
		#index=`expr ${sequence} + 1`
        index=`expr ${sequence}`
		echo "                                                                        "
		echo "***************Your build is ${index}th in the waiting sequence***************"
		echo "                                                                        "
		while ((1));do

			if [ ${index} = 1 ];then
				echo "                                                        "
				echo "***************Start your mission:index[${index}]*****************"
				echo "                                                        "			
				sleep 20
				break
			else 
				echo "                                                                                   "
				echo "*****************************Monitor build waiting sequence:index[${index}]*****************************"
				for((i=0;i<$sequence;++i))
				do	
					if [ ${arr_pid[$i]} = 0 ];then
						continue			
					else			
						ps -au|grep -v grep|grep ${arr_pid[$i]} 
						if [ $? -eq 0 ];then
							echo "PID: ***** ${arr_pid[$i]}*****  build mission is waiting.Please wait."

						else
							echo "                                                                                   "				
							echo "********************PID:${arr_pid[$i]} build mission has finished.********************"			
							arr_pid[$i]=0
							index_bak=$[$index-2]
							index=$[$index-1]
							echo "                                                                        "
							echo "***************Your process is ${index}th in the waiting sequence***************"
							echo "                                                                        "
							echo "***************Waiting: Another ${index_bak} build missions are in the waiting sequence.***************"
							echo "                                                                        "
							cur_pid=$(ps -au|grep $ProceeDiscribe |grep -v grep| awk '{print $2}')
							cur_arr_pid=($cur_pid)
							echo "${index_bak} build missions are in waiting sequence."
							if [ ${index} = 1 ];then
								echo "No build in waiting sequence now."
							else
								echo "The running build missions' PID: ${cur_arr_pid[*]}"
                                echo "The running  missions' PID: ${cur_arr_pid[*]}">>$SingleProcessLog
							fi
						fi
					fi		
				done
			 fi
		sleep 3
		done
		echo "***************Your build mission is done*****************"
	fi


}

ProcessSequenceCheck
echo "### Check passed , start now !!! ###"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值