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

/ 7,486评论 / 53315阅读 / 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. карниз для штор с электроприводом карниз для штор с электроприводом .

  2. porn说道:

    This is a topic that’s close to my heart… Cheers! Where can I find the contact details for questions?

  3. I blog quite often and I truly appreciate your content. Your article has really peaked my interest. I will take a note of your website and keep checking for new information about once per week. I opted in for your RSS feed too.

  4. porn说道:

    I really like it when individuals come together and share ideas. Great blog, keep it up!

  5. Тут можно преобрести сейф для охотничьего ружья купить оружейный сейф

  6. mostbdcasinotvpt说道:

    Mostbet app ensures secure and smooth betting | Mostbet Bangladesh is your trusted betting platform | Play Aviator and win rewards on Mostbet | Enjoy safe and secure payments on Mostbet | Mostbet app provides a user-friendly experience | Unlock exclusive rewards with Mostbet in Bangladesh | Mostbet casino is trusted by players worldwide Mostbet online gaming.

  7. mostbetbgapkgapt说道:

    Play the Aviator game on Mostbet and win big | Bet on cricket, football, and more on Mostbet | Mostbet ensures an unmatched gaming experience | Mostbet live casino brings excitement to your fingertips | Mostbet Bangladesh offers fast and secure payments | Mostbet app provides everything you need for online gaming Mostbet official site.

  8. Узнай все о двустороннее варикоцеле двустороннее варикоцеле

  9. I blog quite often and I truly thank you for your content. This article has really peaked my interest. I am going to bookmark your blog and keep checking for new information about once per week. I opted in for your RSS feed as well.

  10. Тут можно преобрести купить сейф для ружья сейф для оружия

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

发表回复

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