• <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 ExecFun

            //轉(zhuǎn)載必須注明
            //Aaron.xu
            #include <unistd.h>
            #include <stdlib.h>
            #include <stdio.h>
            #include <time.h>
            #include <sys/time.h>
            #include <errno.h>
            #include <assert.h>
            #include <sys/types.h>
            #include <sys/wait.h>
            #include <string.h>

            unsigned int GetTickCount(void)
            {
                struct timeval tv;
                gettimeofday(&tv, NULL);
                return (tv.tv_sec*1000 + tv.tv_usec/1000);
            }

            typedef int (*ExecFunProc)(void* pArg);

            int mysleep(void* pArg)
            {
                sleep(4);
                return 2;
            }



            //返回值: true:執(zhí)行成功 ,false:執(zhí)行失敗
            //參數(shù)說(shuō)明:
            //pfnExec    回調(diào)函數(shù)
            //pArg        回調(diào)函數(shù)的參數(shù)
            //pnStatus    把pfnExec執(zhí)行的返回值,返回給pnStatus指向的變量
            //dwTimeOut    執(zhí)行的超時(shí)時(shí)間,單位毫秒(1秒=1000毫秒)
            //bKill        超時(shí)退出時(shí),是否殺死子進(jìn)程.true:殺死,false:不殺死
            bool ExecFun(ExecFunProc pfnExec,void* pArg,int* pnStatus,unsigned int dwTimeOut,bool bKill = false)
            {
                pid_t childPid;
                pid_t tempPid;
                int nStatus = 0;    
                int bRet = 0;
                unsigned int dwEndTime = GetTickCount() + dwTimeOut;

                assert(pfnExec!=NULL);
                assert(pnStatus!=NULL);

                *pnStatus = -1;
                if( (childPid = fork()) < 0 )
                {
                    printf("fork error:%d \r\n",errno);
                    return false;
                }
                if(childPid == 0)
                {
                    printf("at %u,child pid:%d Entry\r\n",GetTickCount(),getpid());
                    nStatus = pfnExec(pArg);
                    printf("at %u,child pid:%d Exit(%d)\r\n",GetTickCount(),getpid(),nStatus);
                    exit(nStatus);
                }
                else
                {
                    while( 1)
                    {
                        usleep(50*1000);
                        tempPid = waitpid(childPid,&nStatus,WNOHANG);
                        if (tempPid<0)
                        {
                            printf("waitpid(%d) execption,errno:%d \r\n",childPid,errno);
                            return false;
                        }
                        if (tempPid>0)
                        {
                            nStatus  = WEXITSTATUS(nStatus);
                            printf("waitpid(%d) normal exit(%d)\r\n",childPid,nStatus);
                            *pnStatus = nStatus;
                            return true;
                        }

                        if (GetTickCount()>dwEndTime)
                        {
                            printf("at %d,waitpid(%d) time out exit\r\n",GetTickCount(),childPid);
                            if (bKill && (kill(childPid,SIGKILL)==0))
                            {
                                wait(&nStatus);
                            }
                            return false;
                        }
                    }
                    return false;
                }
            }

            int main() {
                int nRet = 0;
                bool bRet = ExecFun(mysleep,NULL,&nRet,2000,true);

                printf("line:%d , bRet:%d,nRet:%d \r\n",__LINE__,bRet,nRet);

                //puts(strerror(EINTR));
                return 0;
            }

            posted on 2012-07-05 09:30 天下 閱讀(381) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Linux編程

            <2012年9月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(4)

            隨筆分類(lèi)(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評(píng)論

            久久久久高潮毛片免费全部播放| 国产无套内射久久久国产| 久久99热这里只频精品6| 精品伊人久久大线蕉色首页| 久久精品人人做人人爽电影蜜月| 亚洲国产精品久久久久婷婷老年| 性做久久久久久久久老女人| 狠狠色丁香久久婷婷综合五月| 成人精品一区二区久久久| 精品久久久无码21p发布| 91精品久久久久久无码| 麻豆av久久av盛宴av| 久久91精品综合国产首页| 亚洲精品乱码久久久久久蜜桃图片 | 久久棈精品久久久久久噜噜| 91精品国产91久久久久久| 99久久精品免费看国产一区二区三区| 久久亚洲精品成人AV| 色综合久久中文字幕综合网| 久久精品国产亚洲麻豆| 久久国产精品成人影院| 久久婷婷五月综合国产尤物app| 91久久九九无码成人网站| 国产精品免费看久久久| 99精品久久久久久久婷婷| 亚洲七七久久精品中文国产| 99久久免费国产精品| 9999国产精品欧美久久久久久| 久久99精品久久久久久久久久| 久久AV无码精品人妻糸列| 久久亚洲AV无码精品色午夜麻豆| 成人国内精品久久久久影院VR| 国产精品久久网| 久久国产精品久久久| 99久久婷婷国产综合亚洲| 久久99热狠狠色精品一区| 久久精品一区二区| 91精品国产高清久久久久久91| 99久久国产热无码精品免费久久久久| 国产精品一久久香蕉产线看| 久久亚洲国产午夜精品理论片|