目录
Tast1:
Write a CPU bound C program and a I/O bound C program (e.g. use a number of printf statements within a while(1) loop). Compile and execute both of them. Observe the effect of their CPU share using the top display and comment.
相关知识了解:
CPU-bound(计算密集型) 和I/O bound(I/O密集型)_cpu bound_剑西楼的博客-CSDN博客
linux命令---top_一只特立独行的SB猴子的博客-CSDN博客
如何在Linux上搭建C++开发环境_linux安装c++环境_WongKyunban的博客-CSDN博客
在Linux系统下编译并执行C++程序_linux如何编译c++文件_Joyce_Ng的博客-CSDN博客
linux操作之:设置fedora的yum国内源_就呆在云上的博客-CSDN博客
圆周率π的几种计算方法与分析_π怎么计算_学而思,思而在!讷于言,敏于行!的博客-CSDN博客
Linux top命令详解_xiaoxiao_chen945的博客-CSDN博客
实践过程:
I/O密集型程序:主要循环输出hello world
#include<iostream> using namespace std; int main() { while(1) cout<<"Hello World!"<<endl; return 0; }
CPU密集型程序:循环计算圆周率
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<iomanip> using namespace std int main() { int n=0; double sum=0; double pi; while(1) { sum=0; for(int i=1;i<=n;i++) { sum+=1.0/(4*i-3)-1.0/(4*i-1); pi=sum*4; cout<<setiosflags(ios::fixed)<<setprecision(5)<<pi<<" "; } } return 0; }
终端调用
结果分析
Test2:
Write a program in C that creates a child process, waits for the termination of the child and lists its PID.
相关知识了解:
Linux进程基础——创建子进程_linux创建子进程_嵌入式小鸟的博客-CSDN博客
Linux中fork函数详解(附图解与代码实现)_linux fork__才疏学浅_的博客-CSDN博客
wait()和waitpid()的一些理解_wait(null)_rouse2617的博客-CSDN博客
#include <sys/types.h>的作用_liuzhanchen1987的博客-CSDN博客
linux标准库unistd.h__yuan_的博客-CSDN博客
实践过程:
代码:
#include<sys/types.h> #include<unistd.h> #include<stdio.h> int main() { pid_t child_pid; printf("the main program PID is %d\n",(int)getpid()); child_pid = fork(); if(child_pid < 0) { printf("error:Fork failed"); exit(-1); } else if (child_pid ==0) { printf("this is the child process, PID is %d\n",(int)getpid()); } else { wait(NULL); printf("Parent and child will finish together, PID is %d\n",(int)child_pid); } return 0; }
运行结果:
Test3:
Compile and run the program code for asgn1.c and record your observations. Perform the modification mentioned and answer the questions that follow.
(a) Comment the inner loop in both the if and the else blocks, compile and run program code for asgn1.c again. Record your observations.
(b) Do you find any difference in the output. If not, then what do you think is the role of the inner loop in both if and the else blocks ?
(c) Modify code for asgn1.c in order to make the child process finish before the parent process starts
相关知识了解:
linux中fork()函数详解(原创!!实例讲解)_fork函数_jason314的博客-CSDN博客
实践过程:
代码:
#include <stdio.h> #include <unistd.h> #define ITR 100000 int main() { int i,j,pid; pid = fork(); if( pid == 0 ){ for ( i=0;i<20;i++){ for (j=0;j<ITR;j++); printf("Child:%d\n",i); } } else{ for ( i=0;i<20;i++){ for (j=0;j<ITR;j++); printf("Parent:%d\n",i); } } }
初步运行结果:
注释后的代码:
#include <stdio.h> #include <unistd.h> #define ITR 100000 int main() { int i,j,pid; pid = fork(); if( pid == 0 ){ for ( i=0;i<20;i++){ //for (j=0;j<ITR;j++); printf("Child:%d\n",i); } } else{ for ( i=0;i<20;i++){ //for (j=0;j<ITR;j++); printf("Parent:%d\n",i); } } }
运行结果:
结果对比:
注释掉内部循环后,child依次完成,之后parent才依次完成
设置ITR=1000:
设置ITR=10000000:
分析:
ITR越大,parent进程越先完成.
由链接可知,创建新进程成功后,系统中出现两个基本完全相同的进程,这两个进程执行没有固定的先后顺序,哪个进程先执行要看系统的进程调度策略。
本系统的调度策略是先调用子进程,猜测内环的目的是让“打印”等待一段时间,类似wait()函数,所以等待时间越长,parent越有可能先完成。
修改代码为:
#include <stdio.h> #include <unistd.h> #define ITR 100000 int main() { int i,j; pid_t pid; pid = fork(); if( pid == 0 ){ for ( i=0;i<20;i++){ for (j=0;j<ITR;j++); printf("Child:%d\n",i); } } else{ for ( i=0;i<20;i++){ //for (j=0;j<ITR;j++); printf("Parent:%d\n",i); } } }
运行结果:
发现已经产生一定的效果
将ITR增大为10000000,运行结果为:
Test4:
Create a file named my file.txt that contains the following four lines :
Child 1 reads this line
Child 2 reads this line
Child 3 reads this line
Child 4 reads this line
Write a C program that forks four other processes. After forking the parent process goes into wait state and waits for the children to finish their execution. Each child process reads a line from the file my file.txt (Child 1 reads line 1, child 2 reads line 2, child 3 reads line 3 and child 4 reads line 4 ) and each prints the respective line. The lines can be printed in any order.
相关知识了解:
Linux学习笔记-Linux下读写文件_linux读写文件_IT1995的博客-CSDN博客
文件读写(Linux)_linux读取文件_[T]的博客-CSDN博客
实践过程:
代码:
#include<iostream> #include<fstream> #include<string> #include<vector> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> using namespace std; vector<string>line; void readTextFile(int s) { ifstream ifs; ifs.open("myFile.txt",ios::in); if(!ifs.is_open()) { cout<<"Fail to open!"<<endl; return; } string str; while(getline(ifs,str)) { line.push_back(str); } ifs.close(); cout<<line[s-1]<<endl; return; } int main() { int i; pid_t pid; pid=fork(); if(pid==0) { readTextFile(1); return 0; } else { pid=fork(); if(pid==0) { readTextFile(2); return 0; } else { pid=fork(); if(pid==0) { readTextFile(3); return 0; } else { pid=fork(); if(pid==0) { readTextFile(4); return 0; } } } } cout<<"Now parent pid is"<<pid<<endl; return 0; }
运行结果:
Test5:
Write two programs file1.c and file2.c. Program file1.c uses these :
(a) fork() to launch another process
(b) exec() to replace the program driving this process, while supplying arguments to file2.c to complete its execution
(c) wait() to complete the execution of the child process
(d) file1.c takes two arguments x (a number less than 1) and n (number of terms to be added, 1 or more). For example: file1 0.5 5
(e) When the child process finishes, the parent prints:
Parent(PID=yyy) : Done
Program file2.c requires two arguments to obtain the approximate value of ex by adding the first n terms in the relation : ex = 1+x+x2/2!+x3/3!+....... and prints the result in the format:
Child(PID=yyy) : For x = 0.5 the first 5 terms yields 1.6484375
Hint : Child-specific processing immediately following the fork() command should load file2.c into the newly created process using the exec() command. This exec() command should also pass 2 arguments to the child. Refer to the man page of exec() command to know how to pass on arguments to the child process. Parent-specific processing should ensure that the parent will wait() for the child- specific processing to complete.
相关知识了解:
Linux函数exec_linux exec_lilboom的博客-CSDN博客
atof函数详解_XiaoZheng2003的博客-CSDN博客
实现过程:
file2.c:
#include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<stdlib.h> #include<math.h> double factorial(int n) { double fac = 1; for (int i = 1; i <= n; i++) { fac = fac * i; } return fac; } int main(int argc,char*argv[])//从file1传入的参数 { printf("\nfile2: \n"); printf("%i\n",argc); int i; for(i=0;i<argc;i++) printf("%d:%s\n",i,argv[i]); double x=atof(argv[1]); int n = (int)atof(argv[2]); double e = 1; for (int i = 1; i <= n; i++) { e += pow(x, i) / factorial(i); } int a = getpid(); printf("Child((PID=%i):For x = %f,the first %i terms yields %f\n",a,x,n,e); exit(0); return 0; }
file1.c:
#include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<stdlib.h> int main(int argc,char*argv[]) { int i; for(i=0;i<argc;i++) printf("%d:%s\n",i,argv[i]); char* argument_list[] = {"./file2", argv[1], argv[2],NULL}; pid_t pid_1; pid_1 = fork(); if(pid_1<0) { printf("error"); } else if(pid_1==0) { execvp("./file2",argument_list); //execvp("./file2",NULL); } else { printf("parent proccess\n"); } wait(pid_1); int m = getpid(); printf("Parent(PID=%i)\n",m); return 0; }
运行结果: