LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 880|回复: 3

求助:请各位前辈帮我看看我这个进程问题,谢谢!

[复制链接]
发表于 2006-7-12 09:38:18 | 显示全部楼层 |阅读模式
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
pid_t pid;
if((pid=fork())<0)
{
printf("fork error! \n");
exit(1);
}
else if(pid==0)
{
printf("Child process is printing.\n");
}
else
{
printf("arent process is printing.\n");
}

exit(0);
}


[root@localhost linuxc]# gcc -o a12 a12.c
[root@localhost linuxc]# ./a12
Child process is printing.
Parent process is printing.


请教:
当程序运行
printf("Child process is printing.\n");

然后应该运行 exit(0);

然后应该退出去啊?

为什么会运行
printf("arent process is printing.\n");

呢?

谢谢!
发表于 2006-7-12 10:13:02 | 显示全部楼层
两行输出是两个进程分别打印的,这不就是fork的用法么
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-12 11:53:51 | 显示全部楼层
谢谢大哥:
还有一个问题:

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>

void h_exit(int status)
{
        if (WIFEXITED(status))
                 printf("normal termination ,exit status=%d \n",WEXITSTATUS(status));
        else if (WIFSIGNALED(status))
           printf("abnormal termination,signal number=%d %s \n",WTERMSIG(status));
}

//主函数。示范三种结束进程的不同的方式,并调用 h_exit 过程处理返回状态字

int main(void)
{
        pid_t pid;
        int status;
       
        //子进程正常退出
        if((pid=fork())<0)
                {
                        printf("error");
                        exit(0);
                }
               
        else if (pid==0) //子进程
                exit(7);
       
        if (wait(&status)!=pid)  //等待子进程
                {
                        printf("wait error");
                        exit(0);
          }
          
        h_exit(status); //打印状态
       
       
        //子进程终止
        if ((pid=fork())<0)
                {
                        printf("fork error");
                        exit(0);
          }
          
        else if (pid==0) //子进程
        abort();         //产生信号 SIGABRT 终止进程
       
        if(wait(&status)!=pid) //等待子进程
                {
                        printf("wait error");
                        exit(0);
                }
               
        h_exit(status);////打印状态
       
       
        //子进程除零终止
       
        if((pid=fork())<0)
                {
                        printf("fork error");
                        exit(0);
          }                                         
         
         else if (pid==0) //子进程
         status /=0;         //产生信号 SIGFPE终止进程        
         
         if(wait(&status)!=pid) //等待子进程
                {
                        printf("wait error");
                        exit(0);
                }
               
        h_exit(status);////打印状态
       
        exit(0);
       
}       

问题1:                                                                 
大哥:
当程序执
else if (pid==0) //子进程
exit(7);

是不是子进程就退出去了

问题2:
后面的

if (wait(&status)!=pid)  //等待子进程
{
   printf("wait error");
   exit(0);
}

是不是父进程执行的?

问题3:
还有
if (wait(&status)!=pid)

不明白意思?


问题4:
h_exit(status); //打印状态

是父进程执行的,还是子进程执行的?


问题5:
if ((pid=fork())<0)
   {
      printf("fork error");
     exit(0);
   }

是不是产生一个新的子进程?


谢谢!
回复 支持 反对

使用道具 举报

发表于 2006-7-29 10:23:53 | 显示全部楼层

小问题,网上答案很多的

问题1:子进程终止,等待父进程取其终止状态,并让父进程完成其善后工作
问题2:是父进程通过wait取子进程终止状态
问题3:wait成功返回子进程的id,这个指wait失败
问题4:是父进程执行的
问题5:产生新进程,如果失败则终止退出。
再给你一个建议,看书或网上先找资料,不要太依赖他人
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表