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

/ 9评论 / 1136阅读 / 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. Leonore说道:

    Hey! Do you know if they make any plugins to assist with SEO?
    I’m trying to get my site to rank for some targeted keywords but I’m
    not seeing very good success. If you know of any please share.
    Many thanks! I saw similar text here: Backlink Portfolio

  2. najlepszy sklep说道:

    Hi there! 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 success.

    If you know of any please share. Appreciate it!
    You can read similar art here: Ecommerce

  3. Bernadette说道:

    Wow, fantastic blog layout! How lengthy have you ever been running a
    blog for? you make blogging glance easy. The entire look of your site is wonderful, let alone the content!
    You can see similar here ecommerce

  4. Vous pouvez également personnaliser la surveillance de certaines applications, et il commencera immédiatement à capturer régulièrement des instantanés de l’écran du téléphone.

  5. CFSRVZ说道:

    cialis price Cardioprotective effect of allyl isothiocyanate in a rat model of doxorubicin acute toxicity

  6. escanty说道:

    Background Diabetic foot infections DFIs are major public health problems and knowledge of microbes that cause infections are helpful to determine proper antibiotic therapy finasteride over the counter

  7. VORVOSESK说道:

    Standard of Care Axicabtagene Ciloleucel for Relapsed or Refractory Large B Cell Lymphoma Results From the US Lymphoma CAR T Consortium how to buy priligy im 16 years old 15 Pregnancy is associated with a higher rate of maternal arrhythmias, 18 ranging from 73 93 in some studies

发表回复

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