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

/ 6,469评论 / 48328阅读 / 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. Eunice Fowler说道:

    영상유포 피해로부터 안전하게 보호받을 수 있는 방법을 알려드립니다 정보 게시물

  2. Jonathan Beck说道:

    Appreciate the comprehensive advice. For more, visit cctv batam

  3. IWIN说道:

    I blog frequently and I seriously appreciate your content. Your article has really peaked my interest. I am going to bookmark your website and keep checking for new details about once a week. I subscribed to your RSS feed as well.

  4. Walter Jensen说道:

    Planning a domestic trip in Perth? Renting a spacious and cushty motor vehicle from Car Hire Perth will make certain a pleasant tour for all people involved

  5. If you’re craving an adrenaline rush like no other, then exclusive jet ski car ride is where you need to be in Dubai

  6. Birdie Nash说道:

    Estoy entusiasmado en los mercados emergentes en África sitio web

  7. Sean Armstrong说道:

    Investigación descriptiva y útil Mira este sitio web

  8. game bài iwin68说道:

    After looking into a few of the articles on your blog, I honestly appreciate your way of blogging. I book-marked it to my bookmark site list and will be checking back in the near future. Please check out my web site too and let me know your opinion.

  9. Bessie Davidson说道:

    As a pickleball fanatic, discovering the proper overgrip for my paddle was once basic. Luckily, I stumbled upon overgrip , and their broad preference of overgrips made it handy to locate the fitting in shape

  10. Dollie Andrews说道:

    Hot air ballooning in Dubai is an ultimate adrenaline rush combined with breathtaking views of the city’s skyline. Book your ride with balloon adventures dubai and prepare for an adventure of a lifetime

  11. Lucas Sharp说道:

    Looking for a memorable experience in Dubai? Rent a quad bike from quad bike rental dubai and create lasting memories as you explore the desert landscapes

  12. This website truly has all of the information I wanted concerning this subject and didn’t know who to ask.

  13. Beulah Olson说道:

    Las cabañas en Pucón ofrecen un ambiente acogedor y hermosas vistas panorámicas. No puedo esperar para visitar https://www.divephotoguide.com/user/cabanasrpet/

  14. Jay Carroll说道:

    Buen punto sobre las tendencias globales y tecnologías emergentes Revisa el post aquí

  15. jacket producer说道:

    Excellent post. I will be facing a few of these issues as well..

  16. Dale Webb说道:

    Dubai’s man-made islands, such as the Palm Jumeirah, are architectural wonders that can be best appreciated from a bird’s-eye view during a helicopter tour The original source

  17. Leo Moreno说道:

    This was very insightful. Check out random video chat for more

  18. Adrian Gordon说道:

    Very informative article. For similar content, visit https://happynailsnm.com/

  19. Augusta Ford说道:

    Ready to feel the wind in your hair and the rush of speed? Don’t miss out on the opportunity to ride in a powerful ##jet car## in Dubai best jet ski hires in the vicinity

  20. 78win说道:

    Greetings! Very helpful advice in this particular article! It’s the little changes that produce the largest changes. Thanks a lot for sharing!

  21. Jared Pittman说道:

    The photographs in this blog post showcasing hot air balloons floating gracefully over Dubai are absolutely stunning! balloon adventures dubai

  22. Patrick Frank说道:

    Las cabañas en Pucón son el refugio ideal para escapar del estrés y relajarse rodeado de naturaleza https://www.indiegogo.com/individuals/37966259

  23. Daniell Foulkes说道:

    bookmarked!!, I love your site.

  24. Frederick Beck说道:

    Me gustaría ver ejemplos de casos de éxito en futuros artículos. https://allmyfaves.com/sandusxbtr

  25. Ruth Craig说道:

    Coincido con la necesidad de mejorar recursos. Productividad

  26. Aw, this was a very nice post. Spending some time and actual effort to create a really good article… but what can I say… I procrastinate a lot and never manage to get nearly anything done.

  27. Virgie Boone说道:

    Great job! Discover more at iluma play

  28. Chad Ballard说道:

    This was quite enlightening. Check out encorenailsminneola.com for more

  29. Devin Burgess说道:

    Thanks for the great explanation. More info at nail salon Columbia Pike

  30. Pretty! This has been a really wonderful post. Thank you for supplying this information.

  31. Louise Burgess说道:

    Discover Dubai’s hidden gems from a different angle with a thrilling helicopter tour that unveils the city’s secrets and lesser-known treasures helicopter in dubai

  32. Anne Campbell说道:

    Pucón es un lugar mágico que te invita a conectarte con la naturaleza. Hospedarse en una de las cabañas de cabañas con tinajas pucon es la mejor manera de disfrutarlo

  33. There is certainly a great deal to know about this subject.
    I really like all of the points you have made.

  34. Sue Underwood说道:

    Las colaboraciones locales semejan esenciales consultar aquí

  35. Shane Cross说道:

    This was quite enlightening. Check out J Nails & Spa for more

  36. Leah Roberts说道:

    I’ve always wanted to experience the feeling of weightlessness in a hot air balloon luxury hot air balloon dubai

  37. Saved as a favorite, I like your website.

  38. Hannah Williams说道:

    Increíble punto sobre la relevancia de la gestión financiera Mira más información

  39. Bessie Curtis说道:

    Plataformas recomendadas para invertir Refinanciación de portfolio

  40. Estella Walters说道:

    Very informative article. For similar content, visit free video chat

  41. Dean Chapman说道:

    This is quite enlightening. Check out ilumaplay for more

  42. Myra Evans说道:

    I enjoyed this post. For additional info, visit https://www.theverge.com/users/eachervmny

  43. Helen Griffin说道:

    Pucón es un destino que combina aventura y relajación. Hospedarse en una de las cabañas disponibles en https://www.divephotoguide.com/user/cabanaspuconrhxd/ es la mejor manera de disfrutarlo al máximo

发表回复

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