进程的创建(c语言)
linux系统下验证程序的并发性,使用c语言fork()函数创建子进程,根据fork()函数特性,操作父子进程,观察父子进程在操作系统管理下执行情况,理解进程的独立性
实验步骤
(1)利用fork()函数创建进程
打开终端并输入以下代码创建实验文件
Charlie@Charlie-PC:~$ cd Desktop
Charlie@Charlie-PC:~/Desktop$ mkdir test
Charlie@Charlie-PC:~/Desktop$ cd test
Charlie@Charlie-PC:~/Desktop/test$ touch hello.c
使用vscode打开hello.c文件并输入以下代码
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
//Practice:How to create a child process?
int main()
{
pid_t pid;
pid_t cid;
//利用getpid函数返回当前进程的id;
printf("Before fork Process id is:%d\n ",getpid());
//利用fork()函数创建子进程;
fork();
printf("After fork,Process id is:%d\n",getpid());
pause(); //暂停程序运行,用于观察该程序在系统中产生的进程
return 0;
}
保存并退出,使用gcc对源文件进行编译。
利用**./命令**运行编译后的文件(ctrl+c停止运行),运行结果如下:
Charlie@Charlie-PC:~/Desktop/test$ ./hello
Before fork Process id is:9833
After fork,Process id is:9833
After fork,Process id is:9834
重新打开一个终端输入ps -al可以查看当前运行的进程状态
Charlie@Charlie-PC:~/Desktop/test$ ps -al
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 9833 9457 0 80 0 - 571 do_sys pts/0 00:00:00 hello
1 S 1000 9834 9833 0 80 0 - 571 do_sys pts/0 00:00:00 hello
4 R 1000 9897 9841 0 80 0 - 4343 - pts/1 00:00:00 ps
执行fork()后创建了一个pid为9834的子进程,子进程的ppid为父进程的pid
(2)fork()函数返回值研究
fork()函数成功执行后在父进程中返回子进程的pid,在子进程中返回值为0
修改代码如下,重新编译、执行,观察终端输出情况
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid,cid;
printf("Before fork Process id is:%d\n ",getpid());
cid = fork();
/*
fork()函数如果成功创建子进程,对于父子进程返回值不一样,
父进程中返回子进程的id,
子进程返回0;
如果创建失败则返回-1
*/
printf("After fork,Process id is:%d,cid=%d\n",getpid(),cid);
pause();
return 0;
}
终端输出如下
Charlie@Charlie-PC:~/Desktop/test$ ./hello
Before fork Process id is:8726
After fork,Process id is:8726,cid=8727
After fork,Process id is:8727,cid=0
可以看到子进程的pid为8727,父进程的pid为8726,fork()函数执行后在父子程序返回了不同的值,在父进程中cid的值是子进程的pid,在子进程中cid的值是0
(3)通过判断fork()函数的返回值让父子进程执行不同语句
fork()函数成功执行后,在父子进程中的返回值不一样,保存返回值再执行判断语句,可以让不同进程执行不同语句
修改代码如下,重新编译、执行,观察终端输出情况
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
//Practice:How to create a child process?
int main(int argc)
{
pid_t pid,cid;
printf("Before fork Process id is:%d\n ",getpid());
cid = fork();
/*
fork()函数如果成功创建子进程,对于父子进程返回值不一样,
父进程中返回子进程的id,
子进程返回0;
如果创建失败则返回-1
*/
if(cid == 0)
{
printf("Child process id (my parent pid is %d):%d\n",getppid(),getpid());
printf("HELLO\n");
}else{
printf("Parent process id:%d\n",getpid());
printf("WORLD\n");
}
pause();
return 0;
}
终端输出如下
Charlie@Charlie-PC:~/Desktop/test$ ./hello
Before fork Process id is:12621
Parent process id:12621
WORLD
Child process id (my parent pid is 12621):12622
HELLO
父进程中fork()返回值不等于0输出WORLD,子进程中fork()函数返回值为0输出HELLO
(4)验证进程的并发执行
进程执行具有并发性,进程交替执行,互不干扰
修改代码如下,重新编译、执行,观察终端输出情况
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc)
{
pid_t pid,cid;
//利用getpid函数返回当前进程的id;
printf("Before fork Process id is:%d\n ",getpid());
//利用fork()函数创建子进程;
cid = fork();
/*
fork()函数如果成功创建子进程,对于父子进程返回值不一样,
父进程中返回子进程的id,
子进程返回0;
如果创建失败则返回-1
*/
if(cid == 0)
{
printf("Child process id (my parent pid is %d):%d\n",getppid(),getpid());
for(int i=0; i<5000 ; i++)
{
printf("HELLO\n");
}
}else{
printf("Parent process id:%d\n",getpid());
for(int i=0 ;i<5000 ;i++)
{
printf("WORLD\n");
}
}
pause();
return 0;
}
终端输出出现HELLO和WORLD交替的情况
现代计算机处理器效率非常高,少数的几个循环可能一次执行完(进程并发执行效果不明显)这里选择循环打印5000次让实验效果更加明显
HELLO
HELLO
WORLD
HELLO
父子进程交替执行所打印的内容不同
(5)验证父子进程内存空间相互独立
fork函数创建的子进程是将父进程在内存中的所有数据都复制一遍,两个进程的运行互不干扰
修改代码如下,重新编译、执行,观察终端输出情况
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
//Practice:How to create a child process?
int main(int argc)
{
pid_t pid,cid;
int x = 100;
printf("Before fork Process id is:%d\n ",getpid());
cid = fork();
/*
fork()函数如果成功创建子进程,对于父子进程返回值不一样,
父进程中返回子进程的id,
子进程返回0;
如果创建失败则返回-1
*/
if(cid == 0)
{
printf("Child process id (my parent pid is %d):%d\n",getppid(),getpid());
for(int i=0; i<3 ; i++)
{
printf("x=%d\n",x);
x++;
}
}else{
printf("Parent process id:%d\n",getpid());
for(int i=0 ;i<3 ;i++)
{
printf("x=%d\n",x);
x--;
}
}
pause();
return 0;
}
终端输出如下
\Charlie@Charlie-PC:~/Desktop/test$ ./hello
Before fork Process id is:13246
Parent process id:13246
x=100
x=99
x=98
Child process id (my parent pid is 13246):13247
x=100
x=101
x=102
父进程中x在循环中逐次减一,子进程中x在循环中逐次加一
(6)孤儿进程托管
由于进程在计算机内是并发执行的,当父进程比子进程先执行完后,子进程被系统进程(pid为1)托管
修改代码如下,重新编译、执行,观察终端输出情况
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc)
{
pid_t pid,cid;
int x=100;
printf("Before fork Process id is:%d\n ",getpid());
cid = fork();
/*
fork()函数如果成功创建子进程,对于父子进程返回值不一样,
父进程中返回子进程的id,
子进程返回0;
如果创建失败则返回-1
*/
if(cid == 0)
{
printf("Child process id (my parent pid is %d):%d\n",getppid(),getpid());
}else{
printf("Parent process id:%d\n",getpid());
}
return 0;
}
终端输出如下
Charlie@Charlie-PC:~/Desktop/test$ ./hello
Before fork Process id is:5093
Parent process id:5093
Child process id (my parent pid is 1):5094
*** 可以看到子进程输出的父进程pid为1***
输入ps -el可以查看所有进程状况
Charlie@Charlie-PC:~/Desktop/test$ ps -el
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 1 0 0 80 0 - 41820 - ? 00:00:02 systemd
1 S 0 2 0 0 80 0 - 0 - ? 00:00:00 kthreadd
1 I 0 3 2 0 60 -20 - 0 - ? 00:00:00 rcu_gp
1 I 0 4 2 0 60 -20 - 0 - ? 00:00:00 rcu_par_gp
pid为1的进程为systemd,是系统进程,根据程序终端输出情况可以知道父进程在子进程前先运行,当父进程运行结束后子进程再运行,所以后面子进程中输出父进程的pid值是1,说明子进程在父进程结束后变为孤儿进程,被系统进程所托管
修改代码如下,重新编译、执行,观察终端输出情况
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc)
{
pid_t pid,cid;
int x=100;
printf("Before fork Process id is:%d\n ",getpid());
cid = fork();
/*
fork()函数如果成功创建子进程,对于父子进程返回值不一样,
父进程中返回子进程的id,
子进程返回0;
如果创建失败则返回-1
*/
if(cid == 0)
{
printf("Child process id (my parent pid is %d):%d\n",getppid(),getpid());
}else{
printf("Parent process id:%d\n",getpid());
wait(NULL); //wait函数可以让进程等待子进程结束后再结束该进程
}
return 0;
}
终端输出为
Before fork Process id is:6117
Parent process id:6117
Child process id (my parent pid is 6117):6118
可以看到子进程显示父进程pid还是6117不是1,父进程等待子进程结束后再结束(释放内存空间)