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

/ 6,511评论 / 48894阅读 / 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. My spouse and i got delighted Raymond could deal with his studies out of the precious recommendations he got out of the blog. It is now and again perplexing to just possibly be offering tactics that many some other people may have been making money from. And we see we have got the blog owner to be grateful to for this. All of the explanations you made, the simple site navigation, the friendships you can aid to instill – it’s all impressive, and it is aiding our son and the family reason why that issue is satisfying, which is certainly pretty indispensable. Many thanks for the whole lot!

  2. Is it my on-line browser or the website, but I can only see a fraction of the post. How should I correct this?

  3. heating repair说道:

    It is really a great and helpful piece of info. I’m glad that you shared this useful info with us. Please keep us up to date like this. Thank you for sharing.

  4. Greetings! Very helpful advice on this article! It is the little changes that make the biggest changes. Thanks a lot for sharing!

  5. Can I just say what a reduction to seek out someone who actually is aware of what theyre talking about on the internet. You undoubtedly know how you can bring an issue to gentle and make it important. Extra folks have to read this and perceive this aspect of the story. I cant consider youre not more in style since you undoubtedly have the gift.

  6. I’m extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one nowadays..

  7. I was on Twitter looking for Neiko Tools when I found a link to this blog, happy I stopped by – Cheers

  8. I’m curious to find out what blog platform you have been utilizing? I’m experiencing some small security problems with my latest blog and I’d like to find something more risk-free. Do you have any solutions?

  9. Usually there are some fascinating time limits here however I don’t know if they all center to heart. You can find some validity however I’m going to take hold opinion until I investigate it further. Good article , thanks and that we want extra! Included with FeedBurner as properly

  10. This will be a terrific blog, would you be interested in doing an interview about just how you developed it? If so e-mail me!

  11. steam cleaners can clean lots of dirty clothes in a very short period of time that is why i always prefer to use them,

  12. ac installation说道:

    There is noticeably a lot of money to understand this. I assume you made certain nice points in functions also.

  13. I never usually post on blogs but I have found this is very useful work.

  14. I’d also like to state that most of those that find themselves without the need of health insurance are normally students, self-employed and those that are jobless. More than half from the uninsured are under the age of Thirty five. They do not sense they are in need of health insurance since they are young along with healthy. Their income is normally spent on real estate, food, and also entertainment. Some people that do represent the working class either whole or not professional are not given insurance by means of their jobs so they go without because of the rising valuation on health insurance in america. Thanks for the tips you discuss through your blog.

  15. I am very happy to read this. This is the type of info that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.

  16. I do not even know the way I stopped up right here, but I thought this publish used to be good. I do not recognize who you might be however certainly you’re going to a well-known blogger when you are not already Cheers!

  17. Wow! This could be one particular of the most beneficial blogs We’ve ever arrive across on this subject. Basically Excellent. I’m also a specialist in this topic therefore I can understand your hard work.

  18. Hello! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing
    very good results. If you know of any please share. Thank you!

    I saw similar blog here: Choose your escape room

  19. heating repair说道:

    Thanks for another informative blog. The place else may I get that type of information written in such an ideal approach? I’ve a project that I’m simply now running on, and I’ve been on the look out for such info.

  20. I am honored to be one of the readers of the blog post. Thanks to your own generous support I’m the first in my household to have some knowledge concerning this topic. Growing up within a less privileged neighborhood has not only made available challenges of access to the internet, but it has moreover made me realize the quality of being blessed with all the wealth of information on your web blog.

  21. I’d always want to be update on new articles on this site, bookmarked ! .

  22. You have brought up a very excellent details , thankyou for the post. “Beginnings are apt to be shadowy and so it is the beginnings of the great mother life, the sea.” by Rachel Carson.

  23. very good post, i actually love this web site, carry on it}

  24. hey there and thanks to your info – I have definitely picked up anything new from right here. I did then again experience several technical issues the usage of this web site, since I skilled to reload the site lots of instances previous to I may just get it to load correctly. I had been pondering if your web host is OK? Not that I am complaining, but sluggish loading instances occasions will sometimes impact your placement in google and could damage your high-quality score if advertising and with Adwords. Well I’m including this RSS to my email and can glance out for much extra of your respective exciting content. Make sure you update this once more soon..

  25. duct cleaning说道:

    After study many of the web sites for your website now, we genuinely much like your way of blogging. I bookmarked it to my bookmark website list and will also be checking back soon. Pls consider my internet site too and inform me what you think.

  26. hvac company说道:

    Thanks for this post, I am a big big fan of this internet site would like to proceed updated.

  27. some hair straighteners that use chemicals are very harsh to the hair, that is why you should be careful with those::

  28. I got what you mean , saved to fav, very decent website .

  29. You seem to be very professional in the way you write.::’~*

  30. Perhaps you’ve had failures of spammers? I likewise use Blog Engine and involve some good anti-spam practices; please Email me if you are attracted to an exchange of practices.

  31. I have enjoyed reading your article, so I felt I would comment to tell you. Cheers for taking a moment to write. Thank you!!

  32. Blaine is not the best magician but i can say that he has great showmanship and i like his show.

  33. It’s difficult to get knowledgeable people with this topic, but the truth is could be seen as do you know what you’re referring to! Thanks

  34. Pretty nice post. I simply stumbled upon your weblog and wanted to mention that I’ve truly loved browsing your blog posts. After all I will be subscribing on your feed and I’m hoping you write once more soon!

  35. Woah! I’m really digging the template/theme of this site. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between usability and appearance. I must say you have done a awesome job with this. Also, the blog loads extremely quick for me on Internet explorer. Outstanding Blog!

  36. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Cheers!

  37. I like what you guys are up too. Such clever work and reporting! Keep up the superb works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my web site .

  38. I think other website owners should take this website as an model, very clean and superb user genial design and style .

  39. When I click on your RSS feed it gives me a page of weird text, is the malfunction on my end?

  40. My partner and i appeared to be aware of this particular currently, however there seemed to be several useful bits which finished the picture to me, thanks a lot!

  41. Oh my goodness! a fantastic post dude. Thanks However We’re experiencing problem with ur rss . Do not know why Not able to enroll in it. Will there be everyone getting identical rss dilemma? Anybody who knows kindly respond. Thnkx

  42. hi, I’ve been ranking high for “free justin bieber stuff”.

  43. It’s in point of fact a great and helpful piece of info. I am happy that you shared this useful information with us. Please stay us up to date like this. Thanks for sharing.

  44. Thank you finding the time to discuss doing this, I believe powerfully concerning it as well as really enjoy reviewing more to do with this process subject matter. Whenever prospective, whilst you attain understanding, exactly what musings posting to your trusty weblog in also material? This is used by i am.

  45. Just have to thank you for this great information. This is what I was looking for (Thanks Bing

  46. Your good competence and kindness in maneuvering everything was essential. I do not know what I would’ve done if I had not encountered such a subject like

  47. Im impressed, I must say. extremely rarely do I discovered a blog thats both educative and entertaining, and let me tell you, youve hit the nail on the

  48. when it comes to free email, i found gmail to be the best and yahoo the worst**

  49. Naturally, when you make your own beats online, there are limitations as to what you can do. Depending on the age and power of your computer system you will have varying results.

  50. An interesting dialogue is price comment. I feel that you should write extra on this topic, it might not be a taboo subject but generally individuals are not sufficient to talk on such topics. To the next. Cheers

发表回复

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