将相同应用的不同进程分配到不同处理器上的脚本
时间:2005-12-29 来源:wjqhd
#!/bin/sh
typeset -i id
id=`id -u`
if [ $id -gt 0 ]; then
echo "error: only root user can run this script"
echo "usage: $0 <process_name_who_need_affinityname>"
exit 1
fi
if [ x$1 = "x" ]; then
echo "error: need give process name as first argument"
echo "usage: $0 <process_name_who_need_affinityname>"
exit 1
fi
typeset -i CPUNUM
CPUNUM=`cat /proc/cpuinfo | grep "^processor" | grep -v grep | wc -l`
typeset -i PROCESSNUM
PROCESSNUM=`ps ax | grep $1 | grep -v grep | wc -l`
if [ $PROCESSNUM -eq 0 ]; then
echo "error: process could not be found"
echo "usage: $0 <process_name_who_need_affinityname>"
exit 1
fi
# affinity different process on 0-($CPUNUM-1) CPUs
ps ax | grep $1 | grep -v grep | awk '{print $1}' | while read i;
do
taskset -pc $(($i%$CPUNUM)) $i
done