Process 新手编程fork进程子孙曾孙

Process 新手编程fork进程子孙曾孙,process,fork,Process,Fork,疯狂的叉子过程。我在以下工作过程中进步了很多,但仍然没有得到正确的结果。 这个想法是,大孙子在孩子被关上后被展示出来。我认为问题在于我必须将I%6集成到I%2中,但我尝试了,没有更多的想法 结果必须是:显示所有请求的孩子,然后显示孙辈对,然后是曾孙辈或可被6整除的孙辈。 例如:孩子2,孙子20=总共22。 然后是中间体的总量 非常感谢。我对此感到绝望。一小时又一小时 致以最良好的祝愿 #include <stdlib.h> #include <stdio.h> #incl

疯狂的叉子过程。我在以下工作过程中进步了很多,但仍然没有得到正确的结果。 这个想法是,大孙子在孩子被关上后被展示出来。我认为问题在于我必须将I%6集成到I%2中,但我尝试了,没有更多的想法

结果必须是:显示所有请求的孩子,然后显示孙辈对,然后是曾孙辈或可被6整除的孙辈。 例如:孩子2,孙子20=总共22。 然后是中间体的总量

非常感谢。我对此感到绝望。一小时又一小时

致以最良好的祝愿

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>

#include "rutines.h"


void show_help();

int main(int argc, char *argv[])
{
    int p,pid[100];
    int d=10;
    int e=100;
    int fills;
    int status;
    int sumador;
    char msg[MAXSTR];

    if (argc>1)
    fills=atoi(argv[1]);
    if (argc==1)
    {
        show_help("Error");
        exit(1);
    }


    // Creem N processos fill
    for(p=1;p<fills+1;p++)
    {
        status=0;       
        pid[p] = fork();
        if (pid[p]<0)
            error("Error");
        else
            if (pid[p]==0)
            {
            sprintf(msg, "CHILD process %d creat -> Ordre: %d Pare: %d.\n",getpid(),p,getppid());
                write_string(msg,"");

                /* Proces del fill */
                if (p%2 == 0)
                {
                    // Creant al net
                    pid[p] = fork();
                    if (pid[p]<0)
                        error("Error");
                    else

                        if (pid[p]==0)
                        {
                        sprintf(msg, "  GRANDCHILD process %d creat -> Ordre: %d Pare: %d.\n",getpid(),p*d,getppid());
                        write_string(msg,"");

                            if (p%6 == 0)
                            {
                            //Creant el besnet
                            pid[p] = fork();
                            if (pid[p]<0)
                                error("Error");
                            else

                            if (pid[p]==0)
                            {
                            sprintf(msg, "    GREAT-GRAND CHILD process %d creat -> Ordre: %d Pare: %d \n",getpid(),p*e,getppid());
                            write_string(msg,"");

                                exit(p*100); //Besnet finalitzat
                            }
                            else
                                wait(&status);  
                                exit(WEXITSTATUS(status)+p*10); //Net finalitzat
                            }
                            else                                
                                exit(p*10);

                        }
                        else
                        wait(&status);
                        exit(WEXITSTATUS(status)+p); 

                    }
                    else
                        exit(p);

                }
                else
                wait(&status);
                sprintf(msg, "CHILD process acabat %d -> %d \n\n",getpid(),WEXITSTATUS(status));
                write_string(msg,"");


    sumador=sumador+WEXITSTATUS(status);
    }



    sprintf(msg, "Proces principal pare %d -> Total return values: %d.\n",getpid(),sumador);
    write_string(msg,"");

    exit(EXIT_SUCCESS);
}


void show_help(char *err_message)
{
    write_string(err_message,"");
    write_string("Us de Pas1ok","");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“rutines.h”
void show_help();
int main(int argc,char*argv[])
{
int p,pid[100];
int d=10;
int e=100;
int填充;
智力状态;
内苏马多;
char-msg[MAXSTR];
如果(argc>1)
填充=atoi(argv[1]);
如果(argc==1)
{
显示帮助(“错误”);
出口(1);
}
//克里姆工艺填料

对于(p=1;p)你的问题毫无意义。你想做什么?现在代码运行良好。我已经和老师检查过了。但是我在完成子进程时出错了。它显示所有子进程的父进程,必须显示子进程。我正在使用命令sprintf getpid()并显示getppid()。知道吗?