计算机系统平台实验——使用fork写一个多进程的程序打印进程号


计算机系统平台实验——使用fork写一个多进程的程序打印进程号

一、实验目的

掌握进城概念,熟悉Linux创建进程的方法以及调用另一个程序的方法。了解进程创建,了解fork函数语义。

二、实验内容

写一个并发程序,它一共启动5个进程(包括自己)。每个进程在屏幕上输出自己的进程号,以及父进程的进程号,然后结束。

实验效果

父进程使用fork实现多进程

1.引入头文件,使用命名空间

i#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include<iostream>
#include<sys/wait.h>
using namespace std;

2.在父进程之中调用fork

int main(int argc,char *argv[]){
int pid1, pid2, pid3, pid4, pid5;   
cout<<"I'm original process a. I'll have many children soon. my pid is:"<<getpid()<<endl;
pid1 = fork();   

if (pid1 < 0)
{ 
printf("Warning! First fork failed!");  
 }  
if (0 == pid1) 
{     
cout<<"I'm new process b. My pid is:"<<getpid() <<endl;//成功创建了新线程b,fork在这个子进程之中返回0
cout<<"I'm b.My father process is: a. His pid is:"<<getppid() <<endl;//找到父进程的线程id 
//printf("I'm new process b. My pid is:", getpid() );     
exit(0);   
}   
if (pid1 >0)//pid1的返回值大于1说明fork的父进程返回了子进程标志。第二次创建子进程
{  
wait(NULL);     
pid2 = fork();//第二次调用fork  
if (pid2 < 0) {       
printf("Warning! Fork 2 failed!");    
 }     
if (0 == pid2) {   
cout<<"I'm new process c. My pid is:"<<getpid() <<endl;
//printf("I'm new process c. My pid is:", getpid() ); 
exit(0);    
 }     
if (pid2 >0) {      
 wait(NULL);   
cout<<"I'm c.My father process is: a. His pid is:"<<getppid() <<endl;//找到父进程的线程id 
//printf("My father process is: a. His pid is:", getppid() );
//wait(NULL);     
pid3 = fork();//第三次调用fork  
if (pid3 < 0) {       
printf("Warning! Fork 3 failed!");    
 }     
if (0 == pid3) {   
cout<<"I'm new process d. My pid is:"<<getpid() <<endl;
//printf("I'm new process d. My pid is:", getpid() ); 
exit(0);    
 }     
if (pid3 >0) {      
 wait(NULL);   
cout<<"I'm d.My father process is: a. His pid is:"<<getppid() <<endl;//找到父进程的线程id 
//printf("My father process is: a. His pid is:", getppid() );
//wait(NULL);     
pid4 = fork();//第四次调用fork  
if (pid2 < 0) {       
printf("Warning! Fork 4 failed!");    
 }     
if (0 == pid4) {   
cout<<"I'm new process e. My pid is:"<<getpid() <<endl;
//printf("I'm new process e. My pid is:", getpid() ); 
exit(0);    
 }     
if (pid4 >0) {      
 wait(NULL);   
cout<<"I'm e.My father process is: a. His pid is:"<<getppid() <<endl;//找到父进程的线程id 
//printf("My father process is: a. His pid is:", getppid() );      
 exit(0);     
}      
 exit(0);     
}       
 exit(0);     
} 

实验体会

这是我第一次在Linux内核上编译C++程序,在排除万难成功配置好虚拟机以及相关应用程序以及g++编译器,并且仔细地学习了Linux命令的基本操作之后,我学习了fork()函数以及它的特性:返回两个值,一个等于0代表成功创建子进程,另一个返回子进程标志。可以在这两次返回值之中写if操作来分别执行不同操作。我选择在大于0时再次调用fork创建新的子进程。当fork返回负值时,代表fork失效。这次实验使我对、Linux内核的系统的进程有了初步了解,学会了编译C++语言代码,学会了执行fork函数创建子进程,学会了获得并执行可执行文件。

本课程资源包

我整理了本课程一学期的四次实验和作业,带有详解,内含期末复习资料和课件。无需付费,链接:

链接:https://pan.baidu.com/s/1cvvO_ntbqad5Xa-hbvHZQg 
提取码:f2t0 
复制这段内容后打开百度网盘手机App,操作更方便哦--来自百度网盘超级会员V1的分享
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值