Implementation tool: PC
Implementation environment: Linux
Internship content (function, goal):
Implemented ideas, methods and technologies (including data structures and algorithms):
Main code:
#include<stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
pid_t pid;
pid=fork();
if(pid<0) /* 如果出错 */
printf("error occurred!\n");
else if(pid==0) /* 如果是子进程 */
{
printf("我是子进程1,进程号是%d\n",getpid());
pid_t pid1;
pid1 = fork();
if(pid1==0){
printf("我是子进程1的子进程,进程号是%d\n",getpid());
}
}
else{
printf("我是父进程,进程号是%d\n",getpid());
pid=fork();
if(pid==0)
printf("我是子进程2,进程号是%d\n",getpid());
else{
pid=fork();
if(pid==0)
printf("我是子进程3,进程号是%d\n",getpid());
}
}
return 0;
}
Notes:
1. The process using fork() is the parent process, and the new process created by fork() is the child process.
2. Fork() is created successfully, the return value is 0 for the child process, and the pid (a positive integer) of the child process for the parent process.
3. After calling fork(), both the parent process and the child process start executing from the next statement.
4. getpid() returns the process identifier of the calling process, then the k of the output child process is equal to the i, j of the parent process.
Result analysis:
Screenshot of the experiment:
Experiment experience:
(1) Deepen the understanding of the concept of process, and clarify the difference between process and procedure.
(2) Further understand the essence of concurrent execution.
(3) Analyze the phenomenon of processes competing for resources, and learn how to solve the mutual exclusion of processes.
(4) Review the content of the textbook on process control and process synchronization to deepen the understanding of the concept of process management.
(5) Carefully read the process management part of the experimental materials, and analyze the operation of multiple processes.
If you find it useful, please like, comment, share and favorite, thank you very much, good people have a safe life! ! !
I wish you all a happy day, sprinkle flowers❀❀❀❀❀❀*★,°*:.☆( ̄▽ ̄)/$:*.°★*.