• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天下

            記錄修行的印記

            linux pexec函數

            #include <unistd.h>
            #include 
            <sys/mman.h>
            #include 
            <sys/types.h>
            #include 
            <fcntl.h>
            #include 
            <stdio.h>
            #include 
            <string.h>
            #include 
            <stdlib.h>
            #include 
            <sys/wait.h>

            int pexec(const char* command,int* result)
            {
                
            int pid;
                
            int fds[2= {-1,-1};
                
            int status = -1;

                
            if(pipe(fds) < 0
                {
                    
            return -1;
                }

                
            if( (pid=vfork()) < 0)
                {
                    close(fds[
            0]);
                    close(fds[
            1]);
                    
            return -1;
                }

                
            if (pid == 0 )
                {
                    dup2(fds[
            1],1);
                    close(fds[
            0]);
                    close(fds[
            1]);
                    execl(
            "/bin/sh""sh""-c", command,NULL);
                    _exit(
            -1);
                }
                
            else
                {
                    waitpid(pid,
            &status,0);
                    close(fds[
            1]);
                    
            if (WIFEXITED(status)==0
                    {
                        close(fds[
            0]);
                        
            return -1;
                    }
                    status  
            = WEXITSTATUS(status);
                    
            if (result!=NULL) 
                    {
                        
            *result = status;
                    }
                    
            //printf("pid(%d) exited,status:%d \r\n",pid,status);
                    return fds[0];
                } 
            }

            int main(int argc,char* argv[]) 
            {
                
            if (argc<2)
                {
                    printf(
            "Usage:%s <cmd> \r\n",argv[0]);
                    
            return -1;
                }
                
            int status = -1;
                
            int fd = pexec(argv[1],&status);
                
            if (fd==-1)
                {
                    printf(
            "pexec(%s) error \r\n",argv[1]);
                    
            return -1;
                }
                
            char buf[4097];
                
            int readed = 0;
                
            while( (readed = read(fd,buf,4096))>0)
                {
                    buf[readed] 
            = 0;
                    printf(
            "readed:%d\r\n%s",readed,buf);
                }
                close(fd);
                
            return 0;
            }

            posted on 2012-08-10 09:23 天下 閱讀(560) 評論(0)  編輯 收藏 引用 所屬分類: Linux編程

            <2011年3月>
            272812345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            久久久这里有精品| 日本道色综合久久影院| 国产欧美久久久精品影院| 色诱久久av| 精品久久久噜噜噜久久久| 一本色道久久88加勒比—综合| 国产精自产拍久久久久久蜜| 国产精品永久久久久久久久久| 久久久久久久久66精品片| 精品免费久久久久久久| 午夜视频久久久久一区| 99久久国产综合精品麻豆| 青青草原综合久久大伊人| 国产精品久久久久影视不卡| 一本久道久久综合狠狠躁AV| 久久亚洲高清观看| 色婷婷综合久久久久中文| 久久男人中文字幕资源站| 国产精品久久成人影院| 狠狠综合久久AV一区二区三区| yellow中文字幕久久网| 久久精品国产网红主播| 久久婷婷五月综合成人D啪| 久久99精品久久久久久野外| 久久91精品国产91久久麻豆| 久久精品日日躁夜夜躁欧美| 办公室久久精品| 国产精品狼人久久久久影院 | 久久精品成人欧美大片| 狠狠久久综合伊人不卡| 久久夜色精品国产亚洲| 久久综合狠狠综合久久综合88| 伊人久久无码精品中文字幕| 国产ww久久久久久久久久| 久久香蕉国产线看观看乱码| 久久精品一区二区三区不卡| 97超级碰碰碰久久久久| 国产精品久久成人影院| 日本三级久久网| 情人伊人久久综合亚洲| 久久精品18|