開源鴻蒙測試fork/waitpid

開源鴻蒙Fork和Waitpid測試

前言

最近做項目需要用到多進程部分河劝,并在不同平臺進行測試渔欢,現(xiàn)將結(jié)果記錄如下:

接口介紹

  • fork

    #include <unistd.h>
    //creates a new process by duplicating the calling process.
    //The new process is referred to as the child process.  The calling
    //process is referred to as the parent process.
    //The child process and the parent process run in separate memory
    //spaces.  At the time of fork() both memory spaces have the same
    //content.  Memory writes, file mappings (mmap(2)), and unmappings
    //(munmap(2)) performed by one of the processes do not affect the
    //other.
    //Return
    //On success, the PID of the child process is returned in the
    //parent, and 0 is returned in the child.  On failure, -1 is
    //returned in the parent, no child process is created, and errno is
    //set to indicate the error.
    
    pid_t fork(void);
    
  • wait

    #include <sys/types.h>
    #include <sys/wait.h>
    //The wait() system call suspends execution of the calling thread
    //until one of its children terminates.  The call wait(&wstatus) is
    //equivalent to:waitpid(-1, &wstatus, 0);
    //Return
    //on success, returns the process ID of the terminated
    //child; on failure, -1 is returned.
    
    pid_t wait(int *status);
    
  • waitpid

    #include <sys/types.h>
    #include <sys/wait.h>
    //The waitpid() system call suspends execution of the calling
    //thread until a child specified by pid argument has changed state.
    //By default, waitpid() waits only for terminated children, but
    //this behavior is modifiable via the options argument, as
    //described below.
    //The value of pid can be:
    //  < -1   meaning wait for any child process whose process group ID
    //         is equal to the absolute value of pid.
    //  -1     meaning wait for any child process.
    //  0      meaning wait for any child process whose process group ID
    //         is equal to that of the calling process at the time of the
    //         call to waitpid().
    //  > 0    meaning wait for the child whose process ID is equal to
    //         the value of pid.
    //The value of options is an OR of zero or more of the following
    //constants:
    //  WNOHANG
    //    return immediately if no child has exited.
    //  WUNTRACED
    //    also return if a child has stopped (but not traced via
    //    ptrace(2)).  Status for traced children which have stopped
    //    is provided even if this option is not specified.
    //  WCONTINUED (since Linux 2.6.10)
    //    also return if a stopped child has been resumed by
    //    delivery of SIGCONT.
    //Return
    //on success, returns the process ID of the child whose
    //state has changed; if WNOHANG was specified and one or more
    //child(ren) specified by pid exist, but have not yet changed
    //state, then 0 is returned.  On failure, -1 is returned.
    
    pid_t waitpid(pid_t pid, int *status, int options);
    

    測試代碼1

    以下代碼設(shè)計為啟動兩個進程雹姊,然后在主進程等待執(zhí)行完成初橘,查看返回值后執(zhí)行主進程

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main()
    {
      pid_t child1, child2;
      pid_t rc1, rc2;
      printf("parent(pid = %d, ppid = %d\n", getpid(), getppid());
      if ((child1 = fork()) == 0)
      {
        for (int i = 1; i <= 10; i++)
        {
          printf("child1(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid());
          sleep(1);
        }
        printf("child1 had completed!\n");
      }
      else if (child1 > 0)
      {
        if ((child2 = fork()) == 0)
        {
          for (int i = 1; i <= 10; i++)
          {
            printf("child2(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid());
            sleep(1);
          }
          printf("child2 had completed!\n");
        }
        else if (child2 > 0)
        {
          //option = 0意味著阻塞父進程等子進程結(jié)束
          rc1 = waitpid(child1, NULL, 0);
          rc2 = waitpid(child2, NULL, 0);
          printf("rc1=%d, rc2=%d\n", rc1, rc2);
          printf("child1=%d, child2=%d\n", child1, child2);
    
          for (int i = 1; i <= 3; i++)
          {
            printf("parent(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid());
            sleep(1);
          }
          printf("parent had completed!\n");
        }
        else if (child2 == -1)
        {
          printf("Error!\n");
        }
      }
      else
      {
        printf("Error!\n");
      }
      return 0;
    }
    

    測試結(jié)果1

    • Linux


      Linux測試
    • OHOS(開源鴻蒙開發(fā)板,RK3568)


      OHOS測試

    總結(jié)1

    Linux下可按照設(shè)計目標執(zhí)行幢炸,但是OHOS下waitpid返回值為-1泄隔,意味沒有找到子進程;但是實際上waitpid是找到子進程并等待子進程執(zhí)行完畢后才執(zhí)行的主進程宛徊。

    測試代碼2

    以下代碼設(shè)計為啟動兩個進程佛嬉,然后在主進程等待執(zhí)行完成,查看返回值后執(zhí)行主進程

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main()
    {
      pid_t child1, child2;
      pid_t rc1, rc2;
      printf("parent(pid = %d, ppid = %d\n", getpid(), getppid());
      if ((child1 = fork()) == 0)
      {
        for (int i = 1; i <= 10; i++)
        {
          printf("child1(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid());
          sleep(1);
        }
        printf("child1 had completed!\n");
      }
      else if (child1 > 0)
      {
        if ((child2 = fork()) == 0)
        {
          for (int i = 1; i <= 10; i++)
          {
            printf("child2(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid());
            sleep(1);
          }
          printf("child2 had completed!\n");
        }
        else if (child2 > 0)
        {
          //option = WNOHANG 意味著不阻塞父進程闸天,子進程和父進程交替運行
          rc1 = waitpid(child1, NULL, WNOHANG);
          rc2 = waitpid(child2, NULL, WNOHANG);
          printf("rc1=%d, rc2=%d\n", rc1, rc2);
          printf("child1=%d, child2=%d\n", child1, child2);
    
          for (int i = 1; i <= 3; i++)
          {
            printf("parent(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid());
            sleep(1);
          }
          printf("parent had completed!\n");
        }
        else if (child2 == -1)
        {
          printf("Error!\n");
        }
      }
      else
      {
        printf("Error!\n");
      }
      return 0;
    }
    

    測試結(jié)果2

    • Linux


      Linux測試
    • OHOS


      OHOS測試

總結(jié)2

WNOHANG:意味不阻塞進程立即返回暖呕,如果子進程存在但是沒有狀態(tài)變化則返回0;Linux執(zhí)行可看出進程是交替執(zhí)行的苞氮,但是OHOS子進程好像沒有搶到執(zhí)行權(quán)限湾揽,所以只有等父進程執(zhí)行完畢后才依次執(zhí)行子進程。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末笼吟,一起剝皮案震驚了整個濱河市库物,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贷帮,老刑警劉巖戚揭,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異撵枢,居然都是意外死亡民晒,警方通過查閱死者的電腦和手機精居,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來镀虐,“玉大人,你說我怎么就攤上這事沟绪」伪悖” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵绽慈,是天一觀的道長恨旱。 經(jīng)常有香客問我,道長坝疼,這世上最難降的妖魔是什么搜贤? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮钝凶,結(jié)果婚禮上仪芒,老公的妹妹穿的比我還像新娘。我一直安慰自己耕陷,他們只是感情好掂名,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著哟沫,像睡著了一般饺蔑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嗜诀,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天猾警,我揣著相機與錄音,去河邊找鬼隆敢。 笑死发皿,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的拂蝎。 我是一名探鬼主播雳窟,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼匣屡!你這毒婦竟也來了封救?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤捣作,失蹤者是張志新(化名)和其女友劉穎誉结,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體券躁,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡惩坑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年掉盅,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片以舒。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡趾痘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蔓钟,到底是詐尸還是另有隱情永票,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布滥沫,位于F島的核電站侣集,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏兰绣。R本人自食惡果不足惜世分,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望缀辩。 院中可真熱鬧臭埋,春花似錦、人聲如沸臀玄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽镐牺。三九已至炫掐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間睬涧,已是汗流浹背募胃。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留畦浓,地道東北人痹束。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像讶请,于是被迫代替她去往敵國和親祷嘶。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容