
本文共 5433 字,大约阅读时间需要 18 分钟。
������������
1���������������
���������������������������������������������������������������������������������Linux������������fork
���������������������������������������
1.1 ������������������fork������
���������fork
���������������������������������������������������������(task_struct)������Linux���������������������������������������������������������������������������������������������������������������������������������������������������������
fork
������������������
pid_t fork(void);���������������������������0������������������������������ID���������������-1
���������������
#includeint main() { pid_t pid = fork(); if (pid > 0) { printf("this is parent: %d\n", getpid()); } else if (pid == 0) { printf("this is child: %d\n", getpid()); } else { printf("error\n"); sleep(100); printf("this is process\n"); } return 0;}
1.2 fork������������������������
������������������������������������������������������������������������������
���������������������fork
������������������������������������������������������������������������������������������������������������������������������������������������������������������
1.3 fork���������������������
������������������fork
������������������������
#include#include int main() { pid_t pid = fork(); if (pid < 0) { perror("fork"); return 1; } if (pid == 0) { // ��������������� sleep(20); exit(10); } else { // ������������������������������ int status; pid_t ret = wait(&status); if (ret > 0 && (status & 0x7f) == 0) { printf("child exit code: %d\n", (status >> 8) & 0xff); } else if (ret > 0) { printf("sig code: %d\n", status & 0x7f); } } return 0;}
���������������
child is run, pid: 45110this is test for waitwait child 5s success, child return code: 1!
���������������
wait
������������������������������exit(10)
���������wait
������������������������������������3���������������
���������������������������������������������������������������������������������������������������������������������
3.1 ���������������������������������
1. ������������ _exit
- ���������������������
void _exit(int status)
- ���������������������
2. ������������ exit
- ���������������������
void exit(int status)
- ������������������������������������������
3. main������������������������
return n
���������exit(n)
3.2 ������������
- ���������������������������������������������������(ctrl + c������������������������
kill
���������������
3.3 exit���_exit���������
exit
���������������_exit
���������������������
4���������������
������������������������������������������������������������������������������������������������������������wait
���waitpid
������������
4.1 ������������������������
- ���������������������������������������������������������
- ���������������������������������������
4.2 ������������������
- ������
wait
���������waitpid
������
wait
���������
pid_t wait(int *status);���������������������������������ID���������������-1���������status������������NULL���
waitpid
���������
int waitpid(int pid, int *status, int option);���������pid������������ID���-1������������������status���������������������������������������������NULLoption������������������`WONOHANG`
4.3 ������������������������������������
#include#include #include #include #include int main() { pid_t child_pid = fork(); if (child_pid == -1) { perror("fork"); return 1; } if (child_pid == 0) { sleep(20); exit(10); } else { int status = 0; pid_t ret = waitpid(-1, &status, 0); if (WIFEXITED(status) && ret == child_pid) { printf("child exit code: %d\n", WEXITSTATUS(status)); } else { printf("wait failed\n"); } } return 0;}
���������������
child is run, pid: 45110this is test for waitwait child 5s success, child exit code:1!
5���������������������
������������������������������������������������fork
���������������������������exec
������������������������������������������
5.1 ���������������������������
- fork������������������������������������������������������
- ������
exec
������������������������������������������������������
5.2 ������������
exec
���������������������������
- ������������������������
- ������������������������������������������������/���������
- ���������������������������������p���������e���������
- ���������������������������e���������
���������
#include#include int main() { execl("/usr/bin/pwd", "pwd", NULL); while (1) { printf("������������������������\n"); sleep(1); } return 0;}
���������������������
execl
������������������������������ID���������������- ��������������������������������������������������������� -����������������������������������������������������������������������������������������������PrimeStar`
发表评论
最新留言
关于作者
