fork系统调用
2015-12-24 22:09
295 查看
fork()学习, 理解
例1: (独立的上下文)
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int num = 999;
int main(int argc,char *argv[])
{
pid_t pid;
char *s = NULL;
printf("hello, my pid is %d\n\n",getpid());
pid = fork();
ERROR(pid == -1);
if(pid == 0)
{
int i = 2;
while(i--)
{
printf("child process, pid = %d, ppid = %d\n\n",
getpid(),getppid());
s = "child process";
sleep(1);
}
}
else
{
int i = 2;
while(i--)
{
// getchar();
printf("parent process, pid = %d, child pid = %d\n\n",
getpid(),pid);
s = "parent process";
sleep(1);
}
}
printf("i'm %s, byebye!!!\n",s);
return 0;
}
编译链接成功后运行, 父进程输出当前及子进程号, 子进程输出当前及父进程号. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_145087810146z4.png)
例2: (独立的文件读写)
code:1
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int main(int argc,char *argv[])
{
#if 0
int fd = open(argv[1],O_RDONLY);
ERROR(fd == -1);
#endif
pid_t pid = fork();
ERROR(pid == -1);
#if 1
int fd = open(argv[1],O_RDONLY);
ERROR(fd == -1);
#endif
if(pid == 0)
{
char ch;
int ret = read(fd,&ch,1);
ERROR(ret < 0);
printf("child process got %c\n",ch);
}
else
{
char ch;
int ret = read(fd,&ch,1);
ERROR(ret < 0);
printf("parent process got %c\n",ch);
}
return 0;
}
编译链接成功后运行, 父子进程输出从文件中文件读出的内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450878284Avxt.png)
例3: 孤儿进程
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int main(int argc,char *argv[])
{
pid_t pid;
pid = fork();
ERROR(pid == -1);
if(pid == 0)
{
int i = 1000;
while(i--)
{
printf("child process, pid = %d, ppid = %d\n",getpid(),getppid());
sleep(5);
}
}
else
{
printf("parent process, byebye!!!\n");
sleep(3);
_exit(0);
}
return 0;
}
编译链接成功后运行, 父子进程输出内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450880187t5B5.png)
在另一个终端查看的进程间关系如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450880232U7zD.png)
例4: 父进程取得子进程的返回值
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/wait.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int main(int argc,char *argv[])
{
pid_t pid;
pid = fork();
ERROR(pid == -1);
if(pid == 0)
{
int i = 3;
while(i--)
{
printf("child process, pid = %d, ppid = %d\n\n",
getpid(),getppid());
sleep(1);
}
printf("chile process exit, byebye!!!\n\n");
_exit(99);
}
#if 1
int stat;
pid_t child_pid;
child_pid = wait(&stat);
printf("child process has exited, pid = %d\n\n",child_pid);
if(WIFEXITED(stat))
printf("child exited with code %d\n\n",WEXITSTATUS(stat));
else
printf("child exit abnormally\n\n");
#endif
printf("parent process, pid = %d\n",getpid());
getchar();
return 0;
}
编译链接成功后运行, 父子进程输出内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_14508820268syy.png)
例5: 进程退出
点击(此处)折叠或打开
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
int main(int argc, char *argv[])
{
printf("hello");
_exit(0);
return 0;
}
编译链接成功后运行, 无输出内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_145088396822v3.png)
注释掉_exit()函数后,如下
点击(此处)折叠或打开
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
int main(int argc, char *argv[])
{
printf("hello");
//_exit(0);
return 0;
}
编译链接成功后运行, 输出内容如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450883979X66r.png)
_exit()使得进程在字符输出到显示设备之前退出.
例1: (独立的上下文)
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int num = 999;
int main(int argc,char *argv[])
{
pid_t pid;
char *s = NULL;
printf("hello, my pid is %d\n\n",getpid());
pid = fork();
ERROR(pid == -1);
if(pid == 0)
{
int i = 2;
while(i--)
{
printf("child process, pid = %d, ppid = %d\n\n",
getpid(),getppid());
s = "child process";
sleep(1);
}
}
else
{
int i = 2;
while(i--)
{
// getchar();
printf("parent process, pid = %d, child pid = %d\n\n",
getpid(),pid);
s = "parent process";
sleep(1);
}
}
printf("i'm %s, byebye!!!\n",s);
return 0;
}
编译链接成功后运行, 父进程输出当前及子进程号, 子进程输出当前及父进程号. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_145087810146z4.png)
例2: (独立的文件读写)
code:1
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int main(int argc,char *argv[])
{
#if 0
int fd = open(argv[1],O_RDONLY);
ERROR(fd == -1);
#endif
pid_t pid = fork();
ERROR(pid == -1);
#if 1
int fd = open(argv[1],O_RDONLY);
ERROR(fd == -1);
#endif
if(pid == 0)
{
char ch;
int ret = read(fd,&ch,1);
ERROR(ret < 0);
printf("child process got %c\n",ch);
}
else
{
char ch;
int ret = read(fd,&ch,1);
ERROR(ret < 0);
printf("parent process got %c\n",ch);
}
return 0;
}
编译链接成功后运行, 父子进程输出从文件中文件读出的内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450878284Avxt.png)
例3: 孤儿进程
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int main(int argc,char *argv[])
{
pid_t pid;
pid = fork();
ERROR(pid == -1);
if(pid == 0)
{
int i = 1000;
while(i--)
{
printf("child process, pid = %d, ppid = %d\n",getpid(),getppid());
sleep(5);
}
}
else
{
printf("parent process, byebye!!!\n");
sleep(3);
_exit(0);
}
return 0;
}
编译链接成功后运行, 父子进程输出内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450880187t5B5.png)
在另一个终端查看的进程间关系如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450880232U7zD.png)
例4: 父进程取得子进程的返回值
点击(此处)折叠或打开
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/wait.h>
#define ERROR(flag) \
if(flag) \
{ \
printf("%d: ",__LINE__); \
fflush(stdout); \
perror("error"); \
exit(errno); \
}
int main(int argc,char *argv[])
{
pid_t pid;
pid = fork();
ERROR(pid == -1);
if(pid == 0)
{
int i = 3;
while(i--)
{
printf("child process, pid = %d, ppid = %d\n\n",
getpid(),getppid());
sleep(1);
}
printf("chile process exit, byebye!!!\n\n");
_exit(99);
}
#if 1
int stat;
pid_t child_pid;
child_pid = wait(&stat);
printf("child process has exited, pid = %d\n\n",child_pid);
if(WIFEXITED(stat))
printf("child exited with code %d\n\n",WEXITSTATUS(stat));
else
printf("child exit abnormally\n\n");
#endif
printf("parent process, pid = %d\n",getpid());
getchar();
return 0;
}
编译链接成功后运行, 父子进程输出内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_14508820268syy.png)
例5: 进程退出
点击(此处)折叠或打开
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
int main(int argc, char *argv[])
{
printf("hello");
_exit(0);
return 0;
}
编译链接成功后运行, 无输出内容. 结果如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_145088396822v3.png)
注释掉_exit()函数后,如下
点击(此处)折叠或打开
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
int main(int argc, char *argv[])
{
printf("hello");
//_exit(0);
return 0;
}
编译链接成功后运行, 输出内容如下图:
![](http://blog.chinaunix.net/attachment/201512/23/20718037_1450883979X66r.png)
_exit()使得进程在字符输出到显示设备之前退出.
相关文章推荐
- Java+MySQL实现网络爬虫程序
- 第三十八课 用break和continue改变流程 【项目1-2】
- iOS开发——单例的实现与使用
- MySql表中key的区别
- DOM扩展之Selectors API
- 并查集
- vc捕获matlab异常
- data-ng-show 指令
- 如何让C#像JavaScript一样编程
- 将GridView中的数据导出到Excel / Word中
- Swift 学习之集合的基本用法
- cgi stress test, mongoose dead
- jq实战-表单验证
- Use EnCase to acquire data from a smartphone
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- Oracle系列之存储过程
- Hibernate中HQL语句的使用
- 实时控制软件第三次编程作业
- log4j