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

            //轉載必須注明
            //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:執行成功 ,false:執行失敗
            //參數說明:
            //pfnExec    回調函數
            //pArg        回調函數的參數
            //pnStatus    把pfnExec執行的返回值,返回給pnStatus指向的變量
            //dwTimeOut    執行的超時時間,單位毫秒(1秒=1000毫秒)
            //bKill        超時退出時,是否殺死子進程.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) 評論(0)  編輯 收藏 引用 所屬分類: Linux編程

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            久久久久亚洲精品日久生情| 久久久一本精品99久久精品66| 大蕉久久伊人中文字幕| 国产综合成人久久大片91| 亚洲国产精品无码久久九九| 久久久久亚洲AV无码网站| 久久精品国产亚洲Aⅴ蜜臀色欲| 久久久国产精华液| 久久99精品国产自在现线小黄鸭| 国产精品9999久久久久| 久久这里只精品99re66| 精品久久久久久国产潘金莲| 久久久亚洲精品蜜桃臀| 久久精品www人人爽人人| 亚洲国产精品一区二区三区久久 | 国内精品久久久久久久涩爱 | 久久久不卡国产精品一区二区| 囯产极品美女高潮无套久久久| 久久亚洲欧美日本精品| 欧美成人免费观看久久| 久久精品无码一区二区三区日韩| 97精品国产97久久久久久免费 | 伊人久久大香线蕉av不变影院| 久久er国产精品免费观看2| 亚洲AV无码久久精品成人| 色播久久人人爽人人爽人人片aV| 狠狠色丁香婷综合久久| 久久精品人人做人人爽电影蜜月 | 久久久九九有精品国产| 久久久久久人妻无码| 久久精品国产亚洲AV香蕉| 日韩va亚洲va欧美va久久| 国产ww久久久久久久久久| 欧美伊香蕉久久综合类网站| 久久Av无码精品人妻系列| 免费精品久久天干天干| 欧美黑人激情性久久| 中文字幕无码久久久| 久久久国产亚洲精品| 成人综合久久精品色婷婷| 99久久精品国产一区二区|