linux process fork related
来源:互联网 发布:windows whistler 2428 编辑:程序博客网 时间:2024/05/21 09:49
to demonstrate the underlying principle of the unix api fork and fork-related contents(wait waitpid) etc, i wrote these two pieces of code as following , both of which repersented my personal understanding the fork.
i strongly reconmend you to read the linux source code concerning with fork if you really want to grasp the these knowlegement and application of them , if so, it will be definitely inducive for our practice projects.
So here is the deal:
No 1 : process_fork.c
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
main()
{
pid_t pid, status;
printf("parent id is %d/n",getpid());
if((pid=fork())==0){
sleep(1);
printf("child id is %d/n",getpid());
return;
}
if((pid=fork())==0){
sleep(2);
printf("child id is %d/n",getpid());
return;
}
if((pid=fork())==0){
sleep(3);
printf("child id is %d/n",getpid());
return;
}
//here, if u want to reclaim the spicific process according to its pid, just use waitpid()
pid = wait(&status);
if(pid>0)
printf("%d reclaimed/n",pid);
sleep(4);//enable the parent to wait until the termination of all its children
}
No 2 : line_buffer.c
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
main ()
{
pid_t pid,status;
printf("fork!/n"); // totally different circumstance u will find when u place printf("fork!") or printf("fork!/n")
pid=fork();
if (pid < 0)
printf("error in fork!/n");
else if (pid == 0){
printf("i am the child process, my process id is %d/n",getpid());
sleep(5);
exit(5);
}
else{
printf("i am the parent process, my process id is %d/n",getpid());
pid = wait(&status);
if ( WIFEXITED(status) ) /* 如果WIFEXITED返回非零值 */
{
printf("The child process %d exit normally./n", pid);
printf("the return code is %d./n", WEXITSTATUS(status));
}
}
exit(0);
}
there's inevitablely some bugs or mistakes, if so , really sorry about that !
i strongly reconmend you to read the linux source code concerning with fork if you really want to grasp the these knowlegement and application of them , if so, it will be definitely inducive for our practice projects.
So here is the deal:
No 1 : process_fork.c
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
main()
{
pid_t pid, status;
printf("parent id is %d/n",getpid());
if((pid=fork())==0){
sleep(1);
printf("child id is %d/n",getpid());
return;
}
if((pid=fork())==0){
sleep(2);
printf("child id is %d/n",getpid());
return;
}
if((pid=fork())==0){
sleep(3);
printf("child id is %d/n",getpid());
return;
}
//here, if u want to reclaim the spicific process according to its pid, just use waitpid()
pid = wait(&status);
if(pid>0)
printf("%d reclaimed/n",pid);
sleep(4);//enable the parent to wait until the termination of all its children
}
No 2 : line_buffer.c
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
main ()
{
pid_t pid,status;
printf("fork!/n"); // totally different circumstance u will find when u place printf("fork!") or printf("fork!/n")
pid=fork();
if (pid < 0)
printf("error in fork!/n");
else if (pid == 0){
printf("i am the child process, my process id is %d/n",getpid());
sleep(5);
exit(5);
}
else{
printf("i am the parent process, my process id is %d/n",getpid());
pid = wait(&status);
if ( WIFEXITED(status) ) /* 如果WIFEXITED返回非零值 */
{
printf("The child process %d exit normally./n", pid);
printf("the return code is %d./n", WEXITSTATUS(status));
}
}
exit(0);
}
there's inevitablely some bugs or mistakes, if so , really sorry about that !
- linux process fork related
- Linux - Linux Command Related with Process
- oma dm related process
- Linux Related
- 【linux】fork() && fork() || fork()
- fork a process
- Fork and exit process
- wait && child process && fork
- The warning related the arch process!
- linux shell related points
- linux shutdown related
- fork,Process,Pool的比较
- Linux Firewall-related /proc Entries
- Linux Firewall-related /proc Entries
- linux fork()
- Linux - fork
- linux fork
- linux fork
- sql单引号问题解决 用转义
- windows快捷键,photoshop快捷键,qq快捷键
- mdb导入sql
- Installing vsftp on ubuntu 8.04
- Flex及AS3的百条常用知识(转载)一
- linux process fork related
- Flex及AS3的百条常用知识(转载)二
- CFileDialog文件选择对话框格式备忘
- ASCII码表
- SQL2005一些小技巧、小归纳
- 快考试了,愁啊
- 怎么做需求分析
- Context Menus
- 明天考四级了