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

/ 1,465评论 / 4828阅读 / 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. Charles Mullins说道:

    Thanks for the useful post. More like this at GROSFILLEX OMEGA CHAISE LOUNGE

  2. Great post. I’m facing many of these issues as well..

  3. Don Phillips说道:

    Nicely detailed. Discover more at multicom batam

  4. stickers说道:

    Howdy! I simply wish to offer you a big thumbs up for your excellent information you have got right here on this post. I am coming back to your website for more soon.

  5. Kathryn Jackson说道:

    Thanks for the great information. More at random video chat

  6. Calvin Palmer说道:

    A respectable overgrip is relevant for retaining a constant grip for your pickleball paddle. I’ve been driving https://www.divephotoguide.com/user/benjinmuzj/ ‘s overgrips for months now, and they not ever disappoint in phrases of durability and overall performance

  7. Roger Powers说道:

    Karel Servis, müşteri memnuniyetine gerçekten önem veren bir firma https://www.ted.com/profiles/47343609

  8. Pretty section of content. I just stumbled upon your web site and in accession capital to assert that I get in fact enjoyed account your blog
    posts. Anyway I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

  9. kontol说道:

    Spot on with this write-up, I seriously think this web site needs a great deal more attention. I’ll probably be returning to see more, thanks for the advice!

  10. Eula Brewer说道:

    Facing a car breakdown can be frustrating, but with the help of Sports car recovery dubai ‘s expert team, you’ll receive prompt and reliable car recovery services in Dubai, minimizing stress and inconvenience

  11. Ellen Ball说道:

    Thanks for the detailed post. Find more at Big Boom Agency

  12. check my site说道:

    I was able to find good info from your articles.

  13. ngentot memek说道:

    Great post. I will be facing many of these issues as well..

  14. Adele Wade说道:

    Appreciate the useful tips. For more, visit GROSFILLEX DINING CHAIRS AND TABLES

发表回复

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