简体   繁体   English

C-迭代多叉

[英]C - iterative multiple forks

I need to make 4 forks 1000 times. 我需要做4把叉子1000次。 I wrote this but it runs forever: 我写了这个,但是它永远运行:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include  <sys/types.h>

#define   N  512

void  chunk0(void);
void  chunk1(void);
void  chunk2(void);
void  chunk3(void);
double get_time(void);

void  main(void)
{
    int i,j,k,iterations=0;
    unsigned int *a=(unsigned int *)malloc(N*N*(sizeof(unsigned int)));
    unsigned int *b=(unsigned int *)malloc(N*N*(sizeof(unsigned int)));
    unsigned int *c=(unsigned int *)malloc(N*N*(sizeof(unsigned int)));
    pid_t  pid;

    for(iterations=0;iterations<1000;iterations++){
        srand ( time(NULL) );
        double start=get_time();

        pid = fork();
        if (pid == 0) {
             chunk0();
        }else {
            pid = fork();
            if (pid == 0){ 
                 chunk1();
            }else {
                pid = fork();
                if (pid == 0){ 
                     chunk2();
                }else {
                    chunk3();
                    wait(NULL);
                    double end=get_time();
                    double diff=end-start;
                    printf("\n Time for run this code is: %lf seconds \n",diff);
                }
            }          
        }
    }       
}

void  chunk0(void)
{
/*  int i,j,k,iterations=0;
    unsigned int *a=(unsigned int *)malloc(N*N*(sizeof(unsigned int)));
    unsigned int *b=(unsigned int *)malloc(N*N*(sizeof(unsigned int)));
    unsigned int *c=(unsigned int *)malloc(N*N*(sizeof(unsigned int)));

    for(iterations=0;iterations<1000;iterations++){
        //printf("iteration #%d: Generating Matrix - ",iterations+1);
        for(i=0;i<N;i++){
            for(j=0;j<N;j++){
                //give int number between 0...1000 to a[i][j] , b[i][j] and reset c[i][j]
                *(a+(i*N+j))=(rand()%1001);
                *(b+(i*N+j))=(rand()%1001);
                *(c+(i*N+j))=0;
            }
        }
        //printf("Multiplying ... \n");
        for(i=0;i<N;i++){
            for(j=0;j<N;j++){
                for(k=0;k<N;k++){
                    *(c+(i*N+j))= *(c+(i*N+j)) + ((*(a+(i*N+k)))*(*(b+(k*N+j))));
                }
            }
        }
    }

    free(a);
    free(b);
    free(c);
*/  
     printf("   *** Child process 0 is done ***\n");
}
void  chunk1(void)
{
     int   i;
     printf("   *** Child process 1 is done ***\n");
}
void  chunk2(void)
{
     int   i;
     printf("   *** Child process 2 is done ***\n");
}
void  chunk3(void)
{
     int   i;
     printf("   *** Child process 3 is done ***\n");
}

double get_time(void){
    struct timeval stime;
    gettimeofday (&stime, (struct timezone*)0);
    return (stime.tv_sec+((double)stime.tv_usec)/1000000);
}

I know why, but don't know how to fix it 我知道为什么,但不知道如何解决

Because after each fork() the child process continues the code from where the parent process is. 因为在每个fork()之后,子进程将从父进程所在的位置继续执行代码。 Thus both parent and child processes continue running the for loop and not only parent process but also the child processes continue forking. 因此,父进程和子进程都继续运行for循环,不仅父进程而且子进程都继续派生。

You need to put a break; 您需要break; after each call to chunkXXX() but the last one (father process), and call exit() from the children chunks. 在每次调用chunkXXX()但最后一个调用(父亲进程)之后,然后从子块调用exit()

wait(NULL) causes your main thread to wait until all children have exited. wait(NULL)使您的主线程等待,直到所有子级都退出。 To break the infinite loop have chunk0, chunk1, and chunk2 call exit to terminate. 要打破无限循环,请使用chunk0,chunk1和chunk2调用出口终止。

Furthermore, as others have pointed out, a break statement is needed after each call to chunk0-chunk2. 此外,正如其他人指出的那样,每次调用chunk0-chunk2之后都需要一个break语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM