fork()函数-创建子进程

本文介绍fork()系统调用如何创建一个与父进程几乎完全相同的子进程,并探讨了进程资源复制的细节,包括环境变量、文件描述符等。通过示例代码展示了如何区分父进程与子进程的执行路径,并讨论了潜在的问题,如竞态条件和文件访问同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fork():

The fork() system call will spawn a new child process which is an identical process to the parent except that has a new system process ID. The process is copied in memory from the parent and a new process structure is assigned by the kernel. The return value of the function is which discriminates the two threads of execution. A zero is returned by the fork function in the child's process.

The environment, resource limits, umask, controlling terminal, current working directory, root directory, signal masks and other process resources are also duplicated from the parent in the forked child process.

Example:

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<stdlib.h>
  4 int globalVariable = 2;
  5 int main(){
  6     char *sIdentifier;
  7     int iStackVariable = 20;
  8
  9     pid_t pID = fork();
 10
 11     if( pID == 0 ) {
 12         //Code only executed by child process
 13         sIdentifier = "Child Process: ";
 14         globalVariable++;
 15         iStackVariable++;
 16         } else if ( pID < 0 ) {
 17             printf( "Failed to fork!/n" );
 18             exit(1);
 19             //Throw exception
 20         } else {
 21             //Code only executed by parent process
 22             sIdentifier = "Parent Process: ";
 23         }  
 24
 25         //Code executed by both parent and child.
 26         printf( " %s/n", sIdentifier );
 27         printf( " Global variable: %d/n", globalVariable );
 28         printf( " Stack variable: %d/n", iStackVariable );
 29 }

 

explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ gcc fork.c
explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ ./a.out
 Parent Process:
 Global variable: 2
 Stack variable: 20
 Child Process:
 Global variable: 3
 Stack variable: 21

 

[Potential Pitfall] : Some memory duplicated by a forked process such as file pointers, will cause intermixed output from both processes. Use the wait() function so that the processes do not access the file at the same time or open unique file descriptors. Some like stdout or stderr will be shared unless synchronized using wait() or some other mechanism. The file close on exit is another gotcha. A terminating process will close files before exiting. File locks set by the parent process are not inherited by the child process.

[Potential Pitfall] : Race conditions can be created due to the unpredictability of when the kernel scheduler runs portions or time slices of the process. One can use wait() . the use of sleep() does not guarentee reliability of execution on a heavily loaded system as the scheduler behavior is not predictable by the application.

Note on exit() vs _exit() : The C library function exit() calls the kernel system call _exit() internally. The kernel system call _exit() will cause the kernel to close descriptors, free memory, and perform the kernel terminating process clean-up. The C library function exit() call will flush I/O buffers and perform aditional clean-up before calling _exit() internally. The function exit(status ) causes the executable to return "status" as the return code for main() . When exit(status ) is called by a child process, it allows the parent process to examine the terminating status of the child (if it terminates first). Without this call (or a call from main() to return() ) and specifying the status argument, the process will not return a value.

 

#include <stdlib.h>

void exit(int status
);
                
#include <unistd.h>

void _exit(int status
);
                

Man Pages:

  • fork - create a child process
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值