fork and exec
来源:互联网 发布:上海市行知小学照片 编辑:程序博客网 时间:2024/06/07 18:48
程序分歧fork()
fork()会产生一个与父程序相同的子程序,唯一不同之处在於其process
id(pid)。
如果我们要撰写守护神程序,或是例如网路伺服器,需要多个行程来同时提供
多个连线,可以利用fork()来产生多个相同的行程。
函数宣告
pid_t fork(void);
pid_t vfork(void);
返回值:
-1 : 失败。
0 : 子程序。
>0 : 将子程序的process id传回给父程序。
在Linux下fork()及vfork()是相同的东西。
范例一: fork.c
在这个范例中,我们示范fork()的标准用法。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void main(void)
{
pid_t pid;
printf("hello/n");
pid = fork();
switch (pid) {
case -1: printf("failure!/n"); break;
case 0: printf("I am child!/n"); break;
default: printf("my child is %d/n",pid); break;
}
for (;;) { /* do something here */ }
}
编译:
gcc -o ex1 fork.c
执行结果:
./ex1 &
hello
my child is 8650
I am child!
我们可以见到,使用fork(),可将一个程序分岐成两个。在分歧之前的程序码
只执行一次。
检验行程:
ps | grep ex1
8649 p0 R 0:40 ./ex1
8650 p0 R 0:40 ./ex1
8649是父程序的pid,8650则为子程序的pid。
您会需要用到"killall ex1"来杀掉两个行程。
范例二: daemon.c
在UNIX中,我们一般都利用fork(),来实作所谓的"守护神程序",也就是DOS中
所谓的"常驻程序"。一般的技巧是将父程序结束,而子程序便成为"守护神"。
这个范例中,示范一般标准的daemon写法。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void main(void)
{
pid_t pid;
pid = fork();
if (pid>0) {
printf("daemon on duty!/n");
exit(0);
} else
if (pid<0) {
printf("Can't fork!/n");
exit(-1);
}
for (;;) {
printf("I am the daemon!/n");
sleep(3);
/* do something your own here */
}
}
编译:
gcc -o ex2 daemon.c
执行结果:
./ex2
daemon on duty!
I am the daemon!
接下来每三秒钟,都会出现一个"I am the daemon!"的讯息,这表示您的程序
已经"长驻"在系统中了。
检验行程:
ps | grep ex2
8753 p0 S 0:00 ./ex2
注意到在范例一中,我们下的指令为"./ex1 &",而在范例二中为"./ex2",没
有"&"符号。
范例三:Perl Script
my @subpid;
my $rtpid = $$;
foreach my $file (@file){
chomp $file;
my($file, $dir, $ext) = fileparse($file);
if (1 == 1) {
my $exec_cmd = "$cmp_cmd ${rsyncTgtDir}${file} ${rsyncTmpDir}${file}";
my $pid = fork();
if ($pid == 0){
setpgrp(0, $rtpid);
exec($exec_cmd);
print "fail to execute $exec_cmd/n";
exit(1);
}
else{
push(@subpid, $pid);
}
}
}
if($$ == $rtpid ){
foreach my $pid (@subpid){
if(getpgrp($pid) == $rtpid ){
waitpid($pid, 0);
}
}
}
- fork, exec and system
- fork and exec
- Fork and Exec
- linux fork and exec function
- 进程控制:system, exec* and fork
- fork、exec
- fork exec
- fork exec
- ubuntu linux shell fork, source and exec 调用执行
- fork + exec, system, popen
- fork和exec
- fork source exec区别
- UNIX gcc fork exec
- Liunx Fork Exec Processes
- fork和exec
- vfork,fork,exec区别
- fork和exec
- fork,wait,exec使用
- 哪有一天不等你啊!
- PKU 3639 Exchange Rates
- 关于CListBox和CComboBox修改窗口Style的问题
- 从服务器上拿数据下来.
- matlab并行计算中worker与cpu core的关系
- fork and exec
- asp.net日期格式化
- 跟老老紫竹学Java - 短消息系统演示和下载地址
- string class的实现
- 哇塞,CSDN有回来啦,可喜可贺啊
- PKU 3070 Fibonacci
- 君子不器
- 顺子对百录收录的看法
- 国足欢迎你 歌词