创建进程的两种方式
来源:互联网 发布:legoev3编程软件 编辑:程序博客网 时间:2024/06/07 02:24
fork:#include <sys/types.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <signal.h>#include <errno.h>#include <signal.h>int main(void ){ int fd; pid_t pid; signal(SIGCHLD, SIG_IGN); printf("befor fork pid:%d \n", getpid());int num = 10; //思考打印 fd = open("11.txt", O_WRONLY); if (fd == -1) { return 0; } pid = fork(); if (pid == -1) { printf("pid < 0 err.\n"); return -1; } if (pid > 0) { printf("parent: pid:%d \n", getpid()); write(fd, "parent", 6); close(fd); //sleep(1); } else if (pid == 0) { printf("child: %d, parent: %d \n", getpid(), getppid()); write(fd, "child", 5); close(fd); //sleep(100); } printf("fork after....\n"); return 0;}vfork:#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <signal.h>#include <errno.h>#include <signal.h>int main01(void ){ pid_t pid; int ret = 0; printf("befor fork pid:%d \n", getpid()); int abc = 10; pid = vfork(); //errno if (pid == -1) { //printf("pid < 0 err.\n"); perror("tile"); return -1; } if (pid > 0) { printf("parent: pid:%d \n", getpid()); } else if (pid == 0) { printf("child: %d, parent: %d \n", getpid(), getppid()); //printf("abc:%d\n", abc); ret = execve("./hello", NULL, NULL); if (ret == -1) { perror("execve:"); } printf("execve 测试有没有执行\n"); exit(0); } printf("fork after....\n"); return 0;}int main(void ){ pid_t pid; int ret = 0; printf("befor fork pid:%d \n", getpid()); int abc = 10; pid = vfork(); //errno if (pid == -1) { //printf("pid < 0 err.\n"); perror("tile"); return -1; } if (pid > 0) { printf("parent: pid:%d \n", getpid()); } else if (pid == 0) { printf("child: %d, parent: %d \n", getpid(), getppid()); //printf("abc:%d\n", abc); char *const argv[] = {"ls", "-l", NULL}; ret = execve("/bin/ls", argv, NULL); if (ret == -1) { perror("execve:"); } printf("execve 测试有没有执行\n"); exit(0); } //printf("fork after....\n"); return 0;}
0 0
- 创建进程的两种方式
- 创建进程的两种方式
- JAVA 创建进程的两种方式
- 进程与线程,线程的两种创建方式
- Python中进程创建的两种方式
- 多进程-multiprocessing-process两种方式创建子进程
- Python中创建进程的两种方式以及进程池
- delphi杀进程的两种方式
- 获取进程信息的两种方式
- android杀死进程的两种方式
- 枚举进程的两种方式
- 进程协调运行的两种方式
- [GDI+] 创建Images的两种方式
- 创建XMLHttpRequest对象的两种方式
- 动态创建option的两种方式
- mysql下创建的两种方式
- Javascript创建对象的两种方式
- 创建线程两种方式的比较
- 17浙江省赛 数位dp or xjb乱搞
- 内联函数
- Android中实现多个Activity共享数据
- 用OpenJTAG烧写程序到Flash—— 韦东山嵌入式Linux视频学习笔记03
- 案例—商品展示
- 创建进程的两种方式
- 常用dos命令详解
- C++调用C#生成的dll
- LCD设备驱动(三)
- 关于tomcat+nginx的一道面试题
- UVA 11768
- hiho1514偶像的条件
- vue.js 父子间通信
- Win10Pcap网络驱动学习(2)