Linux_fork 进程操作demo
2016-03-31 13:10
295 查看
main.7
main8.c
main9.c
main10.c
main11.c
main12.c
#include <stdio.h> #include <stdlib.h> int main(void) { system("ls -l"); printf("end!\n"); return 0; }
main8.c
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(void) { char * env[] = {"PATH=/bin", 0}; char * argv[] = {"ls", "-l", 0}; printf("ready...\n"); //printf("****************execl**************\n"); //execl("ls", "ls", "-l", 0); printf("****************execlp**************\n"); execlp("ls", "ls", "-l", 0); //printf("****************execle**************\n"); //execle("/bin/ls", "ls", "-l", 0, env); //printf("****************execv**************\n"); //execv("/bin/ls", argv); //printf("****************execvp**************\n"); //execvp("ls", argv); //printf("****************execve**************\n"); //execve("/bin/ls", argv, env); printf("end!\n"); return 0; }
main9.c
#include <stdio.h> #include <stdlib.h> int main(void) { int pid; char *msg; int n; printf("father process begin...\n"); pid = fork(); if (pid == -1) { printf("fork error!\n"); exit(1); } else if (pid == 0) { printf("In child process!\n"); msg = "child process "; n = 2; } else { printf("In father process!\n"); msg = "father process "; n = 1; } while(n--) { printf("%s\n", msg); sleep(1); } printf("%s end!\n", msg); return 0; }
main10.c
#include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> int main(void) { int pid; char *msg; int status; int child_pid; int n; printf("father process begin...\n"); pid = fork(); if (pid == -1) { printf("fork error!\n"); exit(1); } else if (pid == 0) { printf("In child process!\n"); msg = "child process "; n = 3; } else { printf("In father process!\n"); msg = "father process "; n = 1; } while(n--) { printf("%s\n", msg); sleep(1); } if (pid) { printf("father process wait...\n"); child_pid = wait(&status); if (WIFEXITED(status)) { printf("Wait finished and child process terminated normally, child pid = %d\n", child_pid); } else { printf("Wait finished and child process terminated unnormally, child pid = %d\n", child_pid); } } printf("%s end!\n", msg); return 0; }
main11.c
#include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> int main(void) { int pid; char *msg; int status; int child_pid; int n; printf("father process begin...\n"); pid = fork(); if (pid == -1) { printf("fork error!\n"); exit(1); } else if (pid == 0) { printf("In child process!\n"); msg = "child process "; n = 1; } else { printf("In father process!\n"); msg = "father process "; n = 10; } while(n--) { printf("%s\n", msg); sleep(5); } if (pid) { printf("father process wait...\n"); child_pid = wait(&status); if (WIFEXITED(status)) { printf("Wait finished and child process terminated normally, child pid = %d\n", child_pid); } else { printf("Wait finished and child process terminated unnormally, child pid = %d\n", child_pid); } } printf("%s end!\n", msg); return 0; }
main12.c
#include <stdlib.h> #include <stdio.h> int main(void) { int pid; int i; for (i=0; i<10; i++) { pid = fork(); if (pid == -1) { perror("fork error!\n"); exit(1); } else if (pid == 0) { //printf("Start run a new child pid, pid=%d\n", getpid()); } else { //printf("Add a new child, pid = %d\n", pid); } } printf("My pid = %d\n", getpid()); return 0; }
相关文章推荐
- Linux_ fork 进程操作
- Linux学习总结——实践
- 《Linux内核设计与实现》第3章读书整理
- Linux _文件操作demo
- 《Linux内核设计与实现》Chapter 3 读书笔记
- Linux信号处理
- 《Linux内核设计与实现》第三章学习笔记
- 分析Linux内核创建一个新进程的过程
- 《Linux内核设计与实现》第三章学习笔记
- Linux _文件操作
- Linux 中的零拷贝技术
- Linux内核设计与实现 第三章
- 通过19幅图为你展现CentOS安装全过程
- Centos上的双网卡绑定
- linux netstat 统计连接数
- Linux命令教程
- linux及安全第六周总结——20135227黄晓妍
- centos6.6下安装Gcc4.8.1
- linux下ioctl()函数使用的详细说明
- centos 正确 安装 jdk