fork wait
2016-05-12 21:47
495 查看
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
//daemon
int main(int argc, char *argv[])
{
int num = argc;
int i=0;
int status = 0;
int fd = -1;
// printf("num is %d\n",num);//line Cache need \n to output
// for(i=0; i < num; i++)
// {
// printf("argv %d:%s\n",i,argv[i]);
// }
pid_t old_pid = getpid();
pid_t new_pid = fork();
if (new_pid > 0)
{
printf ("father pid %d,%d\n",getppid(), getpid() );
_exit(0);
}
else if (new_pid == 0)
{
printf("child pid %d,%d\n",getppid(), getpid() );
}
//std io -> dev/null
//if (-1 != (fd = open( "/dev/null", O_RDWR, 0)))
//{
//dup2(fd, STDIN_FILENO);
//dup2(fd, STDOUT_FILENO);
//dup2(fd, STDERR_FILENO);
//if (2 < fd)
//{
//close(fd);
//}
//}
if (-1 == setsid())
{
printf("error");
return (-1);
}
(void)chdir("/");
pid_t second_child = fork();
if (0 < second_child)
{
printf("second child pid %d,%d\n",getppid(), getpid() );
while (0 == status)
{
pid_t dead_pid = wait(&status);
printf("dead pid=%d\n",dead_pid);
int exitStatus = (int) WEXITSTATUS (status);
printf("Child Process %d wait exited with pid=%d status=%d exit status=%d\n",getpid(), dead_pid, status, exitStatus);
}
}
else if (0 == second_child)
{
printf("grandson pid %d,%d\n",getppid(), getpid() );
sleep (5);
// exit(7);
}
while (1);
return 1;
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
//daemon
int main(int argc, char *argv[])
{
int num = argc;
int i=0;
int status = 0;
int fd = -1;
// printf("num is %d\n",num);//line Cache need \n to output
// for(i=0; i < num; i++)
// {
// printf("argv %d:%s\n",i,argv[i]);
// }
pid_t old_pid = getpid();
pid_t new_pid = fork();
if (new_pid > 0)
{
printf ("father pid %d,%d\n",getppid(), getpid() );
_exit(0);
}
else if (new_pid == 0)
{
printf("child pid %d,%d\n",getppid(), getpid() );
}
//std io -> dev/null
//if (-1 != (fd = open( "/dev/null", O_RDWR, 0)))
//{
//dup2(fd, STDIN_FILENO);
//dup2(fd, STDOUT_FILENO);
//dup2(fd, STDERR_FILENO);
//if (2 < fd)
//{
//close(fd);
//}
//}
if (-1 == setsid())
{
printf("error");
return (-1);
}
(void)chdir("/");
pid_t second_child = fork();
if (0 < second_child)
{
printf("second child pid %d,%d\n",getppid(), getpid() );
while (0 == status)
{
pid_t dead_pid = wait(&status);
printf("dead pid=%d\n",dead_pid);
int exitStatus = (int) WEXITSTATUS (status);
printf("Child Process %d wait exited with pid=%d status=%d exit status=%d\n",getpid(), dead_pid, status, exitStatus);
}
}
else if (0 == second_child)
{
printf("grandson pid %d,%d\n",getppid(), getpid() );
sleep (5);
// exit(7);
}
while (1);
return 1;
相关文章推荐
- Error Domain=NSOSStatusErrorDomain Code=1718449215 "The operation couldn’t be completed. (OSStatus error 1718449215.)"
- 启动hive报错:[ERROR] Terminal initialization failed; falling back to unsupported
- Message与obtainMessage()
- CodeForces 312C The Closest Pair(构造)
- Gmail迁移到Office365设置
- bunoj 29140 Taiko taiko
- Execution failed for task CapacitySms:mergeDebugResources Some file crunching failed 错误解决
- 人工智能-机器学习-数据挖掘-模式识别
- Convolutional Neural Networks at Constrained Time Cost
- WPF[调用线程无法访问此对象,因为另一个线程拥有该对象]主窗口MainWindow 创建的子线程 调用窗口线程的控件方法
- 修改git全部已提交的用户名和邮箱
- CodeForces 627B Factory Repairs
- http://blog.csdn.net/cswhale/article/details/16941281
- 【BaiduMapSDK系列】定位到我的位置
- http://blog.csdn.net/testcs_dn/article/details/45225413
- hdu 1702 ACboy needs your help again!
- B. Factory Repairs---cf627B(线段树)
- main 函数参数中 argc/argv的相关讨论和使用
- unresolved external symbol __report_rangecheckfailure 解决思路
- SVN:cleanup failed to process thefollowing paths 错误的解决方案