• <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)論

            久久精品人人做人人爽电影| 精品国际久久久久999波多野| 久久精品国产网红主播| 欧美成人免费观看久久| 久久婷婷五月综合97色直播| 久久精品无码一区二区三区| 久久99国产精品久久99| 国产精品一久久香蕉产线看| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久亚洲中文字幕精品有坂深雪 | 久久精品国产精品亚洲人人| 9999国产精品欧美久久久久久| 国产AV影片久久久久久| 国产精品va久久久久久久| 久久久久18| 日本人妻丰满熟妇久久久久久| 国产精品久久久久jk制服| 国产精品熟女福利久久AV| 亚洲国产成人乱码精品女人久久久不卡| 久久99精品国产麻豆蜜芽| 尹人香蕉久久99天天拍| 日产精品久久久久久久| 大香网伊人久久综合网2020| 欧美与黑人午夜性猛交久久久 | 色88久久久久高潮综合影院| 精品久久久久久亚洲精品| 国产成人99久久亚洲综合精品| 久久久久亚洲AV无码专区首JN| 99久久中文字幕| 久久91精品国产91| 久久久青草青青亚洲国产免观| 日本亚洲色大成网站WWW久久 | 久久91亚洲人成电影网站| 日韩亚洲国产综合久久久| 久久九九久精品国产| 亚洲午夜久久久久久久久电影网 | 久久久午夜精品| 91精品国产综合久久婷婷| 久久久免费观成人影院| 国产99精品久久| 久久综合给合久久狠狠狠97色69|