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

/ 7,486评论 / 53336阅读 / 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. Joshua Banks说道:

    This was a fantastic resource. Check out wiadomości polska for more

  2. нарколог на дом краснодар [url=www.zal.rolevaya.info/viewtopic.php?id=5359/]www.zal.rolevaya.info/viewtopic.php?id=5359/[/url] .

  3. вызов нарколога на дом [url=https://www.advance2.ukrbb.net/viewtopic.php?f=2&t=777]https://www.advance2.ukrbb.net/viewtopic.php?f=2&t=777[/url] .

  4. нарколог на дом краснодар [url=https://superogorod.ucoz.org/forum/2-2084-1/]нарколог на дом краснодар[/url] .

  5. вывод. из. запоя. на. дому. ростов. [url=superogorod.ucoz.org/forum/2-2082-1]вывод. из. запоя. на. дому. ростов.[/url] .

  6. I was able to find good information from your articles.

  7. mostbetbgapktrpt说道:

    Play the Aviator game on Mostbet and win big | Mostbet registration is fast and simple | Play your favorite games on Mostbet Bangladesh | Mostbet makes online betting simple and enjoyable | Play responsibly with Mostbet Bangladesh | Play the latest casino games on Mostbet Bangladesh Mostbet bonuses.

  8. Bess Woods说道:

    Thanks for the valuable insights. More at wiadomości polska

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

  10. Dylan Rice说道:

    Thanks for the great content. More at wiadomości polska

  11. вывод из запоя ростов-на-дону [url=https://www.advance2.ukrbb.net/viewtopic.php?f=2&t=775]https://www.advance2.ukrbb.net/viewtopic.php?f=2&t=775[/url] .

  12. Andre Allen说道:

    This was highly useful. For more, visit wiadomości polska

  13. porn说道:

    This is a topic which is near to my heart… Best wishes! Where can I find the contact details for questions?

  14. вывод из запоя цены ростов на дону [url=https://gaslo.ukrbb.net/viewtopic.php?f=13&t=3407]https://gaslo.ukrbb.net/viewtopic.php?f=13&t=3407[/url] .

  15. web site说道:

    web site best social media services.

  16. Robert May说道:

    Well explained. Discover more at wiadomości polska

  17. porn说道:

    This web site truly has all the info I needed about this subject and didn’t know who to ask.

  18. Предлагаем услуги профессиональных инженеров офицальной мастерской.
    Еслли вы искали сервисный центр xiaomi, можете посмотреть на сайте: официальный сервисный центр xiaomi
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  19. Diplomi_rnSr说道:

    сколько стоят дипломы [url=https://1oriks-diplom199.ru/]сколько стоят дипломы[/url] .

  20. Узнай все о на что влияет варикоцеле варикоцеле яичка

  21. Rescue efforts were hindered as a result of rain from Tropical Depression Grace on sixteen August.

  22. Probably the most tough aspect of this challenge was making small financial sacrifices along the best way.

  23. On the whole, rooms washed with an abundance of natural light are best painted both white or a pale coloration; darker rooms are more attractive in a wealthy, warm colour.

  24. Steve Stone说道:

    Appreciate the thorough insights. For more, visit podsumowanie wiadomości

  25. Coolie shades have an outdated title, but their traditional, huge, hatlike form continues to be a favourite atop a large base.

  26. So it may be actually challenging that means.

  27. They have 2 youngsters and pay £800 a month for childcare.

  28. In September 1939, World Conflict II broke out and the stations of Croix-Rouge and Cluny-La Sorbonne, considered too close to different stations, had been closed.

  29. 竹の子冷凍说道:

    The terminus station was Waldsee, named after the waters in the recreation space of Möslepark.

  30. Kelvinliz说道:

    Здесь можно заказать кроссовки New Balance с доставкой. Выбирайте свою идеальную пару прямо сейчас.
    https://userbookmark.com/story18429763/new-balance-550

  31. 浸漬 読み方说道:

    The smaller and lighter mud-like particles will drift much farther downwind, typically for a whole bunch of miles.

  32. Heading in direction of Lehener Straße, trams were still signposted for the Hauptbahnhof.

  33. Begin off by taking her on transient errands, notably ones that do not require you to get out of the automobile: the curbside drop-off field at the put up workplace, drive-up bank teller, or the drive-by means of window at a quick-meals restaurant.

  34. William Albert Coggan, Divisional Officer, Hampshire Fire Brigade.

  35. Elstein, Aaron (September 11, 2024).

  36. It was then when she realized she may make a business out of her beautiful bridal designs.

  37. Lonely canines howl just to see if anybody else is on the market.

  38. This pun-stuffed cookbook is named after the legendary and forbidden e-book of the useless that is “alien to all sane and balanced readers.” Like that mysterious tome, this cookbook accommodates many darkish (but still scrumptious) secrets and techniques within its pages.

  39. Rosenstein, still representing the Paterson and Hudson River Railroad, filed an official objection to the Martini alternate, discovering issues with the requirement that 2.5 miles (4.0 km) of observe would have to be relocated.

  40. A basic pure treasure, marble provides sophistication and richness to any dining area.

  41. They may ask one other player if they have a particular card, with the purpose of amassing 4-of-a-variety.

  42. Roger Hardy, Evaluation: End of roadmap?

  43. The rubbish Primer. Journal, Smithsonian; Thompson, Helen.

发表回复

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