分类 生活随笔 下的文章

spinlock(自旋锁)和mutex(互斥锁)的区别(转)

首先spinlock是只有在内核态才有的,当然你也可以在用户态自己实现,但是如果想要调用spinlock_t类型,那只有内核态才有。但是semaphore是内核态和用户态都有的,mutex是一种特殊的semaphore。

spinlock是一种忙等待,也就是说,进程是不会睡眠的,只是一直在那里死循环。而mutex是睡等,也就是说,如果拿不到临界资源,那它会选择进程睡眠。那什么时候用spinlock,什么时候用mutex呢?首先,如果是在不允许睡眠的情况下,只能只用spinlock,比如中断的时候。然后如果临界区中执行代码的时间小于进程上下文切换的时间,那应该使用spinlock。反之应该使用mutex。

那mutex和semaphore有什么区别呢?mutex是用作互斥的,而semaphore是用作同步的。也就是说,mutex的初始化一定是为1,而semaphore可以是任意的数,所以如果使用mutex,那第一个进入临界区的进程一定可以执行,而其他的进程必须等待。而semaphore则不一定,如果一开始初始化为0,则所有进程都必须等待。同时mutex和semaphore还有一个区别是,获得mutex的进程必须亲自释放它,而semaphore则可以一个进程获得,另一个进程释放。

nginx+php页面空白

今天在Ubuntu上遇到一个诡异的问题,机器用apt-get安装了nginx和php5,在sites-enabled下添加了一个本地网站的配置:监听80端口,设置server_name,location / {}也做了入口指向,location ~ .php$ {}里面也配置了 fastcgi_pass unix:/var/run/php5-fpm-event.sock;,由于配置了很多次nginx了,所以这块自认为也没有什么问题。然后就是php-fpm的配置了:单独建立了一个php-fpm的pool,通过unix socket和nginx进行通信,然后重启了nginx和php5-fpm,然后浏览器打开绑定了本地hosts的一个域名,发现一片空白,如下:
2015-04-16 17:19:35屏幕截图.png

其实index.php页面就写了一行phpinfo(),应该不会是一片空白啊,肯定是某个地方出错了。
首先我想到的是ubuntu apt-get安装的php禁用了phpinfo(),于是在命令行里面用php index.php执行这个文件,没有问题,看php.ini的配置也是正确的,所以排除;然后又怀疑nginx root的路径指错了,再回去检查nginx的配置文件,结果路径没有错,路径错了页面应该是404啊,我设置了404跳转,所以也不是404,再看location的正则!-e $request_filename rewrite ^/(.*)$ /index.php last;,大概这两句,检查没有问题啊,正则是正确的啊;然后我又怀疑是php-fpm没有启动起来,php5-fpm-event.sock,没有建立起来,所以我去查看了这个文件,发现存在啊,所以问题也不在这里;所以我再去看nginx的error_log,发现什么都没有,把错误日志的级别改到info级别,还是没有任何错误日志,这可奇怪了。。。nginx和php-fpm的通信貌似不存在任何问题;所以我接着去看了php-fpm.log,发现也没有任何问题,没有报错,来来去去的折腾了好一会儿,没有找到问题。

没办法,还是看请求吧,上面的图,状态码:200 ,size:0Kb,所以我想,页面空白不是我上面想到的那些问题,我再打开请求的详情:
2015-04-16 17:38:39屏幕截图.png

Server: "nginx/1.6.2 (Ubuntu)" 说明请求是经由nginx成功处理的,再看 X-Powered-By:"PHP/5.5.12-2ubuntu4.3" 说明请求也经过了php的处理,那么现在就可以讲上面那些猜测的错误推翻了,实际上没看到错误日志也应正了不是上面想到的那些错误,因为整个流程是正确的。

那么问题到底处在哪里呢?为什么是为空白?

首先要相信,index.php的phpinfo()绝对不会输出空白,到底为什么会空白呢?那么我们可以逆推,首先是可能php-fpm并没有执行到index.php,再往前推,有可能是nginx没有告诉php-fpm要执行哪个php脚本文件,再往前推,我们浏览器没有告诉nginx要执行哪个php脚本。问题肯定出现在这3个步骤里面。

那么我先检查了nginx的访问日志access-log,发现请求里有记录 /index.php,说明我们对nginx的请求是正确的,依次检查第二个步骤:我们知道nginx是通过 fastcgi协议和php通信的,所有的请求参数传递都是通过fastcgi_param指令传个php-fpm的,这些参数在php的超全局变量$_SERVER里面都可以看见,那么SCRIPT_FILENAME这个值有没有有没有传给php-fpm呢?由于nginx的fastcgi参数管理是放在一个叫做fastcgi_params的文件里单独管理,用主配置文件nginx.conf包含进来的,所以打开fastcgi_params检查,发现没有传递'SCRIPT_FILENAME'这一个参数,我尝试在最后一行加入了
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ,然后重启nginx,刷新浏览器,我靠,爱情来的太快就像龙卷风,phpinfo()的内容出来了。问题居然能出现在这里??为啥php没有收到执行哪个脚本的参数,为毛不记错误日志不报错?这不是坑爹么?

吐槽归吐槽,问题好歹解决了,以后遇到类似的问题也能知道怎么解决了。所以在blog上mark一记!

CentOS下shadowsocks-libev一键安装脚本(转)

https://github.com/teddysun/shadowsocks_install

#! /bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#===============================================================================================
#   System Required:  CentOS6.x (32bit/64bit)
#   Description:  Install Shadowsocks(libev) for CentOS
#   Author: Teddysun <[email protected]>
#   Intro:  http://teddysun.com/357.html
#===============================================================================================

clear
echo "#############################################################"
echo "# Install Shadowsocks(libev) for CentOS 6 or 7 (32bit/64bit)"
echo "# Intro: http://teddysun.com/357.html"
echo "#"
echo "# Author: Teddysun <[email protected]>"
echo "#"
echo "#############################################################"
echo ""

# Make sure only root can run our script
function rootness(){
if [[ $EUID -ne 0 ]]; then
   echo "Error:This script must be run as root!" 1>&2
   exit 1
fi
}

# Get version
function getversion(){
    if [[ -s /etc/redhat-release ]];then
        grep -oE  "[0-9.]+" /etc/redhat-release
    else    
        grep -oE  "[0-9.]+" /etc/issue
    fi    
}

# CentOS version
function centosversion(){
    local code=$1
    local version="`getversion`"
    local main_ver=${version%%.*}
    if [ $main_ver == $code ];then
        return 0
    else
        return 1
    fi        
}

# Disable selinux
function disable_selinux(){
if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
fi
}

# Pre-installation settings
function pre_install(){
    # Not support CentOS 5
    if centosversion 5; then
        echo "Not support CentOS 5, please change to CentOS 6 or 7 and try again."
        exit 1
    fi
    #Set shadowsocks-libev config password
    echo "Please input password for shadowsocks-libev:"
    read -p "(Default password: teddysun.com):" shadowsockspwd
    if [ "$shadowsockspwd" = "" ]; then
        shadowsockspwd="teddysun.com"
    fi
    echo "password:$shadowsockspwd"
    echo "####################################"
    get_char(){
        SAVEDSTTY=`stty -g`
        stty -echo
        stty cbreak
        dd if=/dev/tty bs=1 count=1 2> /dev/null
        stty -raw
        stty echo
        stty $SAVEDSTTY
    }
    echo ""
    echo "Press any key to start...or Press Ctrl+C to cancel"
    char=`get_char`
    #Install necessary dependencies
    yum install -y wget unzip openssl-devel gcc swig python python-devel python-setuptools autoconf libtool libevent
    yum install -y automake make curl curl-devel zlib-devel openssl-devel perl perl-devel cpio expat-devel gettext-devel
    # Get IP address
    echo "Getting Public IP address, Please wait a moment..."
    IP=$(curl -4 icanhazip.com)
    if [[ "$IP" = "" ]]; then
        IP=`curl -s -4 ipinfo.io | grep "ip" | awk -F\" '{print $4}'`
    fi
    echo -e "Your main public IP is\t\033[32m$IP\033[0m"
    echo ""
    #Current folder
    cur_dir=`pwd`
    cd $cur_dir
}

# Download latest shadowsocks-libev
function download_files(){
    if [ -f shadowsocks-libev.zip ];then
        echo "shadowsocks-libev.zip [found]"
    else
        if ! wget --no-check-certificate https://github.com/shadowsocks/shadowsocks-libev/archive/master.zip -O shadowsocks-libev.zip;then
            echo "Failed to download shadowsocks-libev.zip"
            exit 1
        fi
    fi
    unzip shadowsocks-libev.zip
    if [ $? -eq 0 ];then
        cd $cur_dir/shadowsocks-libev-master/
    else
        echo ""
        echo "Unzip shadowsocks-libev failed! Please visit http://teddysun.com/357.html and contact."
        exit 1
    fi
    # Download start script
    if ! wget --no-check-certificate https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks-libev; then
        echo "Failed to download shadowsocks-libev start script!"
        exit 1
    fi
}

# Config shadowsocks
function config_shadowsocks(){
    if [ ! -d /etc/shadowsocks-libev ];then
        mkdir /etc/shadowsocks-libev
    fi
    cat > /etc/shadowsocks-libev/config.json<<-EOF
{
    "server":"0.0.0.0",
    "server_port":8989,
    "local_address":"127.0.0.1",
    "local_port":1080,
    "password":"${shadowsockspwd}",
    "timeout":600,
    "method":"aes-256-cfb"
}
EOF
}

# iptables set
function iptables_set(){
    echo "iptables start setting..."
    /sbin/service iptables status 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
        /etc/init.d/iptables status | grep '8989' | grep 'ACCEPT' >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            /sbin/iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 8989 -j ACCEPT
            /etc/init.d/iptables save
            /etc/init.d/iptables restart
        else
            echo "port 8989 has been set up."
        fi
    else
        echo "iptables looks like shutdown, please manually set it if necessary."
    fi
}

# Install 
function install(){
    # Build and Install shadowsocks-libev
    if [ -s /usr/local/bin/ss-server ];then
        echo "shadowsocks-libev has been installed!"
        exit 0
    else
        ./configure
        make && make install
        if [ $? -eq 0 ]; then
            mv $cur_dir/shadowsocks-libev-master/shadowsocks-libev /etc/init.d/shadowsocks
            chmod +x /etc/init.d/shadowsocks
            # Add run on system start up
            chkconfig --add shadowsocks
            chkconfig shadowsocks on
            # Start shadowsocks
            /etc/init.d/shadowsocks start
            if [ $? -eq 0 ]; then
                echo "Shadowsocks-libev start success!"
            else
                echo "Shadowsocks-libev start failure!"
            fi
        else
            echo ""
            echo "Shadowsocks-libev install failed! Please visit http://teddysun.com/357.html and contact."
            exit 1
        fi
    fi
    cd $cur_dir
    # Delete shadowsocks-libev floder
    rm -rf $cur_dir/shadowsocks-libev-master/
    # Delete shadowsocks-libev zip file
    rm -f shadowsocks-libev.zip
    clear
    echo ""
    echo "Congratulations, shadowsocks-libev install completed!"
    echo -e "Your Server IP: \033[41;37m ${IP} \033[0m"
    echo -e "Your Server Port: \033[41;37m 8989 \033[0m"
    echo -e "Your Password: \033[41;37m ${shadowsockspwd} \033[0m"
    echo -e "Your Local IP: \033[41;37m 127.0.0.1 \033[0m"
    echo -e "Your Local Port: \033[41;37m 1080 \033[0m"
    echo -e "Your Encryption Method: \033[41;37m aes-256-cfb \033[0m"
    echo ""
    echo "Welcome to visit:http://teddysun.com/357.html"
    echo "Enjoy it!"
    echo ""
    exit 0
}

# Uninstall Shadowsocks-libev
function uninstall_shadowsocks_libev(){
    printf "Are you sure uninstall shadowsocks_libev? (y/n) "
    printf "\n"
    read -p "(Default: n):" answer
    if [ -z $answer ]; then
        answer="n"
    fi
    if [ "$answer" = "y" ]; then
        ps -ef | grep -v grep | grep -v ps | grep -i "ss-server" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            /etc/init.d/shadowsocks stop
        fi
        chkconfig --del shadowsocks
        # delete config file
        rm -rf /etc/shadowsocks-libev
        # delete shadowsocks
        rm -f /usr/local/bin/ss-local
        rm -f /usr/local/bin/ss-tunnel
        rm -f /usr/local/bin/ss-server
        rm -f /usr/local/bin/ss-redir
        rm -f /usr/local/lib/libshadowsocks.a
        rm -f /usr/local/lib/libshadowsocks.la
        rm -f /usr/local/include/shadowsocks.h
        rm -rf /usr/local/lib/pkgconfig
        rm -f /usr/local/share/man/man8/shadowsocks.8
        rm -f /etc/init.d/shadowsocks
        echo "Shadowsocks-libev uninstall success!"
    else
        echo "uninstall cancelled, Nothing to do"
    fi
}

# Install Shadowsocks-libev
function install_shadowsocks_libev(){
    rootness
    disable_selinux
    pre_install
    download_files
    config_shadowsocks
    if ! centosversion 7; then
        iptables_set
    fi
    install
}

# Initialization step
action=$1
[  -z $1 ] && action=install
case "$action" in
install)
    install_shadowsocks_libev
    ;;
uninstall)
    uninstall_shadowsocks_libev
    ;;
*)
    echo "Arguments error! [${action} ]"
    echo "Usage: `basename $0` {install|uninstall}"
    ;;
esac