关于fork 可以命令,查看详细说明及用法:man 2 fork
NAME
fork - create a child process
SYNOPSIS
#include <unistd.h>
pid_t fork(void);
DESCRIPTION
fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.
The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content. Memory writes, file mappings (mmap(2)), and unmappings
(munmap(2)) performed by one of the processes do not affect the other.
The child process is an exact duplicate of the parent process except for the following points:
* The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)).
* The child's parent process ID is the same as the parent's process ID.
* The child does not inherit its parent's memory locks (mlock(2), mlockall(2)).
* Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to zero in the child.
* The child's set of pending signals is initially empty (sigpending(2)).
* The child does not inherit semaphore adjustments from its parent (semop(2)).
* The child does not inherit process-associated record locks from its parent (fcntl(2)). (On the other hand, it does inherit fcntl(2) open file description locks and flock(2) locks from its par‐
ent.)
* The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)).
* The child does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its parent (see io_set‐
up(2)).
The process attributes in the preceding list are all specified in POSIX.1. The parent and child also differ with respect to the following Linux-specific process attributes:
* The child does not inherit directory change notifications (dnotify) from its parent (see the description of F_NOTIFY in fcntl(2)).
* The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal when its parent terminates.
* The default timer slack value is set to the parent's current timer slack value. See the description of PR_SET_TIMERSLACK in prctl(2).
* Memory mappings that have been marked with the madvise(2) MADV_DONTFORK flag are not inherited across a fork().
* The termination signal of the child is always SIGCHLD (see clone(2)).
* The port access permission bits set by ioperm(2) are not inherited by the child; the child must turn on any bits that it requires using ioperm(2).
printf("The father pid=%d The child pid=%d num=%d\n", getppid(),getpid(), num);
sleep(3);
}
}
else
{
while (1)
{
printf("The father pid=%d num=%d\n", getpid(), num);
sleep(5);
}
}
return 0;
}
复制代码
保存为fork_test.c
gcc -o fork_test.a fork_test.c
复制代码
运行:
./fork_test.a
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
The father pid=15131 The child pid=15132 num=100
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
The father pid=15131 The child pid=15132 num=100
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
编译时遇到了错误:
gcc -o fork_test.a fork_test.c
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
分析要链接动态库
用命令:
gcc -shared -g -o fork_test.a fork_test.c
/usr/bin/ld: /tmp/cc1wgtVD.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/cc1wgtVD.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
但又出现问题, 按提示要加参数 fPIC的, 但我没用
重新用原始命令编译:
gcc -o fork_test.a fork_test.c
又OK了