[linux操作系统实验]进程的创建(有时程序输出在cmd提示之后?)

/ 7,487评论 / 53349阅读 / 0点赞

问题


实验具体内容

实验内容

编写一段程序,使用系统调用fork( )创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示'a',子进程分别显示字符'b'和字符'c'。试观察记录屏幕上的显示结果,并分析原因。

涉及的系统调用

…..
pid=fork( );
if  (! pid) printf("I'm the child process!\n");
else if (pid>0) printf("I'm the parent process! \n");
else printf("Fork fail!\n");
……
fork( )调用前
…..
pid=fork( );
if (! pid) printf("I'm the child process!\n");
else if (pid>0) printf("I'm the parent process!\n ");    
else printf("Fork fail!\n");
……
 …..
pid=fork( );
if (! pid) printf("I'm the child process!\n");
else if (pid>0) printf("I'm the parent process!\n ");
else printf("Fork fail!\n");
……
fork( )调用后

源代码

#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        cout << "<< coolight | [软件202,2006300052,郑泳坤]" << endl;
        int fork_num = fork();
        if(fork_num == -1){
                cout << "<< fork Error" << endl;
        }else if(fork_num == 0){ //子进程
                fork_num = fork();
                if(fork_num == -1){
                        cout << "<< fork Error" << endl;
                }else if(fork_num == 0){ //子进程
                        sleep(0.5);
                        cout << "c" << endl;
                }else if(fork_num > 0){
                        sleep(0.5);
                        cout << "b" << endl;
                }
        }else if(fork_num > 0){
                sleep(0.5);
                cout << "a" << endl;
        }
        return 0;g
}

运行结果截图

思考

  1. If you are crafty, a carpenter, or know somebody who’s, you possibly can disassemble a mattress and reuse the supplies on your own.

  2. We’re forging courage, will and information.

  3. The crew of the bottom are there on an expedition to drill to the center of the planet.

  4. Glyn Rosser. For providers to the Deaf.

  5. acb 株価说道:

    November 16, 1998. The show-inside-a-present match completely with the season’s “Discovery and Exploration” theme, as Elmo investigated the world in a manner younger preschoolers understood: by way of simple subject-matter and plenty — and plenty — of repetition.

  6. For political and public companies in Aberdeen.

  7. 右後ろ 英語说道:

    In trendy phrases, it’s the King’s finances submitted to the English Parliament, itemizing how the tax revenues allotted to the King could be spent.

  8. Toddlers are learning to feed themselves and to eat new foods throughout this essential time of development and development.

  9. “He was very thorough and defined every thing very clearly.

  10. So left facet can be the send away, it would be my retrieve, it could be my long chunk attacks.

  11. On February 1, a westbound specific prepare delayed over three hours got here at excessive pace by means of Passaic round 6:30 pm.

  12. We come against demonic goals, visions and revelations.

  13. This unconventional pair of ladies arrange residence at Brympton d’Evercy.

  14. The bins and collection trucks are not suited to slim roads or houses with steep driveways or steps.

  15. The Passaic Herald-Information. June 24, 1933.

  16. All private belongings to these inside had been burned up, and those despatched to the hospital were given new clothes.

  17. Alderman Reginald Spencer Howard.

  18. インド 日本说道:

    The DWP will take off £4.35 a month for each £250 (or a part of £250) of capital above £6,000.

  19. You may be glad you probably did.

  20. They feel that a tram depot of their neighbourhood can be too much.

  21. Celebrating three years of fantastic friendship,’ the mom-of-one wrote.

  22. Martini, regardless of his optimism, acknowledged that he felt it could be a “tough job”, but felt there was an opportunity this new strategy would work out.

  23. Узнай все о варикоцеле слева у мужчин заболевание варикоцеле

  24. Mina Powell说道:

    Well explained. Discover more at szybkie wiadomości

  25. Austin Daniel说道:

    I found this very interesting. For more, visit krótkie wiadomości

  26. Alejandro Waters说道:

    This was a fantastic resource. Check out skrót wiadomości for more

  27. Здесь можно преобрести купить сейф оптом купить сейф оптом

  28. Juegos de casino Jugabet [url=http://www.jugabet777.com]http://www.jugabet777.com[/url] .

  29. вывод из запоя дешево ростов на дону [url=https://family2.quadrobb.me/viewtopic.php?id=1836#p6861/]https://family2.quadrobb.me/viewtopic.php?id=1836#p6861/[/url] .

  30. Della Coleman说道:

    Thanks for the great tips. Discover more at skrót wiadomości

  31. Stairlift hull说道:

    I could not resist commenting. Exceptionally well written!

  32. Тут можно преобрести цена сейфа для оружия сейф для оружия купить в москве

  33. Тут можно преобрести сейф несгораемый купить несгораемый сейф купить

  34. Тут можно преобрести огнеупорный сейф купить сейф огнестойкий цена

  35. Здесь можно преобрести заказать сейф сейф купить цена

  36. 동대구오피说道:

    Your style is very unique in comparison to other folks I have read stuff from. Many thanks for posting when you’ve got the opportunity, Guess I will just bookmark this web site.

  37. Davidslofs说道:

    [url=https://hurtowniastali2.xyz]stal warszawa [/url]
    kД…townik stalowy 30x30x2

  38. Davidslofs说道:

    [url=https://hurtowniastali2.xyz]stal prefabrykowana [/url]
    profile stalowe zamkniД™te cena

  39. Davidslofs说道:

    [url=https://hurtowniastali2.xyz]pЕ‚askownik 40×2 [/url]
    profile waga

  40. I used to be able to find good advice from your content.

  41. вывод из запоя дешево ростов [url=http://internetmoney.bestbb.ru/viewtopic.php?id=31697/]http://internetmoney.bestbb.ru/viewtopic.php?id=31697/[/url] .

  42. Тут можно преобрести купить сейф с доставкой в москве сейфы для ружья

  43. Davidslofs说道:

    [url=https://hurtowniastali2.xyz]po ile jest stal [/url]
    hurtownia stali

  44. Davidslofs说道:

    [url=https://hurtowniastali2.xyz]profil stalowy h [/url]
    profil bramowy 50x50x3

  45. вывод из запоя дешево ростов [url=http://www.motik13.0pk.me/viewtopic.php?id=1990]вывод из запоя дешево ростов[/url] .

  46. вывод из запоя ростовская область [url=https://to.iboard.ws/viewtopic.php?id=8058]https://to.iboard.ws/viewtopic.php?id=8058[/url] .

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注