• <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 天下 閱讀(554) 評論(0)  編輯 收藏 引用 所屬分類: Linux編程

            <2012年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            99久久免费国产精品| 久久久久久国产精品免费无码| 久久成人18免费网站| 亚洲精品无码久久毛片| 亚洲AV无码久久精品蜜桃| 青青青伊人色综合久久| 女人高潮久久久叫人喷水| 久久久一本精品99久久精品88 | 亚洲国产成人精品91久久久 | 亚洲欧洲精品成人久久奇米网| 伊人久久精品无码二区麻豆| 91久久福利国产成人精品| 热久久国产欧美一区二区精品| 人妻精品久久无码区| 久久精品无码一区二区app| 久久w5ww成w人免费| 一级A毛片免费观看久久精品| 久久美女人爽女人爽| 久久精品无码一区二区无码 | 久久99久久无码毛片一区二区| 97精品依人久久久大香线蕉97| 久久久久一本毛久久久| 狠狠久久亚洲欧美专区| 97久久国产综合精品女不卡| 久久久午夜精品福利内容| 色婷婷噜噜久久国产精品12p| 色综合久久88色综合天天| 国产成人无码久久久精品一| 久久精品国产亚洲AV忘忧草18| 理论片午午伦夜理片久久| 国产精品美女久久久久av爽| 国产精品久久永久免费| 精品无码久久久久久尤物| 久久综合狠狠综合久久| 亚洲人成精品久久久久| 婷婷综合久久中文字幕蜜桃三电影| 亚洲午夜精品久久久久久浪潮 | 狠狠色婷婷久久综合频道日韩| 日本精品久久久久影院日本| 久久无码一区二区三区少妇| 色偷偷91久久综合噜噜噜噜|