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

/ 6,505评论 / 48806阅读 / 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. Hello! I know this is kinda off topic however , I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog article or vice-versa? My site covers a lot of the same topics as yours and I think we could greatly benefit from each other. If you’re interested feel free to shoot me an e-mail. I look forward to hearing from you! Terrific blog by the way!

  2. Hey there! I randomly stumbled upon your blog from Yahoo. Your content is filled with interesting information, and I will probably use it at some point in my career. Keep up the excellent work!

  3. gas safe fitters说道:

    Really nice pattern and excellent content, very little else we want .

  4. Hello! I know this is kinda off topic but I’d figured I’d ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa? My website goes over a lot of the same topics as yours and I feel we could greatly benefit from each other. If you are interested feel free to send me an email. I look forward to hearing from you! Terrific blog by the way!

  5. I like this web blog so much, saved to my bookmarks .

  6. not use a beginning stage. For me, the starting stage is understanding which market I’m aiming for.

  7. gas safe direct说道:

    I beloved up to you’ll obtain performed proper here. The comic strip is attractive, your authored subject matter stylish. however, you command get bought an edginess over that you want be turning in the following. in poor health certainly come more beforehand once more since precisely the similar just about very continuously inside case you protect this hike.

  8. i do a lot of article submissions in a week or two and i hire some writers from india and other countries to do my stuf,.

  9. gassafe说道:

    Some really nice and utilitarian information on this web site , also I believe the style and design holds good features.

  10. I’m sorry for the huge evaluation, however I’m actually loving the brand new Zune, as well as hope this, along with the excellent evaluations another people wrote, can help you determine if it is the proper choice for you.

  11. Without difficulty, the post is really the greatest on this precious topic. I harmonise with your conclusions and also will certainly thirstily look forward to your incoming updates. Just saying thanks will certainly not simply just be sufficient, for the extraordinary lucidity in your writing. I will certainly promptly grab your rss feed to stay abreast of any kind of updates. Genuine work and much success in your business endeavors!

  12. I usually dont post in Blogs but your blog forced me to, amazing work. beautiful.

  13. Iím not that much of a internet reader to be honest but your sites really nice, keep it up! I’ll go ahead and bookmark your website to come back later. All the best

  14. Hey there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding one? Thanks a lot!

  15. best suvs… […]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[…]…

  16. I enjoy your composing design, do carry on creating! I will be back!

  17. I really wanted to make a brief comment in order to appreciate you for these nice strategies you are giving out on this site. My particularly long internet look up has finally been compensated with reliable content to go over with my neighbours. I ‘d say that many of us readers are undoubtedly endowed to be in a great network with so many marvellous individuals with helpful techniques. I feel truly privileged to have used your entire site and look forward to so many more awesome minutes reading here. Thank you once more for a lot of things.

  18. Prior to you decide to create your own checklist to include an idea associated with what camping checklist ought to. Really … A listing can be greater than what you need.

  19. Youre so cool! I dont suppose Ive read anything in this way before. So nice to uncover somebody with some original ideas on this subject. realy appreciate starting this up. this excellent website is something that is needed over the internet, a person if we do originality. valuable work for bringing something new towards the web!

  20. gassaferegister说道:

    you have got a great weblog here! do you wish to make some invite posts on my small weblog?

  21. gassafe说道:

    I hate my boss. So during the spare times, I am quite down and dumbed from the repetitive tasks. Your posts give my brain some needed excercise. Thanks!

  22. Oh my goodness! a fantastic post dude. Thanks Nevertheless My business is experiencing issue with ur rss . Don’t know why Not able to sign up for it. Possibly there is any person obtaining identical rss difficulty? Anyone who knows kindly respond. Thnkx

  23. Some truly terrific work on behalf of the owner of this web site , utterly great content material .

  24. call gas safe说道:

    Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and return to read more of your useful info. Thanks for the post. I’ll definitely return.

  25. You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you.

  26. It’s truly a great and helpful piece of info. I’m happy that you just shared this useful info with us. Please stay us informed like this. Thank you for sharing.

  27. An impressive share, I recently with all this onto a colleague who was simply doing little analysis for this. And hubby actually bought me breakfast since I found it for him.. smile. So let me reword that: Thnx for your treat! But yeah Thnkx for spending the time to talk about this, I find myself strongly about this and adore reading much more about this topic. Whenever possible, as you become expertise, might you mind updating your blog site with additional details? It is highly great for me. Massive thumb up just for this article!

  28. I precisely wished to appreciate you once again. I am not sure the things I might have taken care of without those ways contributed by you over such field. It was before a horrifying case in my circumstances, however , seeing this skilled avenue you processed the issue took me to weep over gladness. I am just happier for this work and as well , have high hopes you recognize what a great job that you’re getting into training people using your websites. I know that you have never encountered all of us.

  29. Simply wanna input that you have a very nice website , I enjoy the pattern it really stands out.

  30. Dzięki za jasne i zwięzłe informacje na temat bezpieczeństwa SEO.

  31. Dzięki za kompleksowy przewodnik po SEO. Bardzo pouczający!

  32. Doceniam szczegółowe wyjaśnienia na temat SEO i bezpieczeństwa.

  33. Cieszę się, że znalazłem ten blog przed rozpoczęciem mojego projektu SEO.

  34. Dzięki za świetne porady na temat SEO. Bezpieczeństwo jest kluczowe!

  35. Bardzo pouczający blog na temat SEO! Dzięki za podzielenie się nim.

  36. Doceniam skupienie się na bezpieczeństwie, gdy mowa o SEO.

  37. Ten blog to cenne źródło informacji dla każdego, kto myśli o SEO.

  38. seo lokalne说道:

    Świetne wskazówki dotyczące znalezienia wiarygodnych usług SEO. Dzięki!

  39. Ten post był bardzo pouczający na temat procesu SEO. Dzięki!

  40. Ten blog to cenne źródło informacji dla każdego, kto martwi się o SEO.

  41. Ten post to świetne źródło informacji dla każdego, kto potrzebuje SEO.

  42. Dziękuję za podkreślenie znaczenia profesjonalnego podejścia do SEO.

  43. Doceniam skupienie się na bezpieczeństwie, gdy mowa o SEO.

  44. Ten post był bardzo pouczający na temat procesu SEO. Dzięki!

  45. Dzięki za praktyczne porady dotyczące radzenia sobie z SEO.

  46. audyt seo shoper说道:

    Właśnie dowiedziałem się, że moja strona potrzebuje SEO. Te informacje są ratunkiem.

  47. Wow, nie zdawałem sobie sprawy, jak skomplikowane może być SEO. Dzięki za te informacje!

  48. Dzięki za praktyczne wskazówki dotyczące znalezienia wykwalifikowanego specjalisty SEO.

  49. Świetny artykuł na temat znaczenia SEO dla bezpieczeństwa.

  50. Czuję się znacznie pewniej w temacie SEO po przeczytaniu tego bloga.

发表回复

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