C - 迭代多次分叉

发布于 2024-09-09 00:15:25 字数 2806 浏览 1 评论 0原文

我需要制作 4 个叉子 1000 次。我写了这个,但它永远运行:

#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 need to make 4 forks 1000 times. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

战皆罪 2024-09-16 00:15:25

因为在每次 fork() 之后,子进程都会从父进程所在的位置继续执行代码。因此,父进程和子进程都继续运行 for 循环,不仅父进程而且子进程也继续 fork。

Because after each fork() the child process continues the code from where the parent process is. Thus both parent and child processes continue running the for loop and not only parent process but also the child processes continue forking.

牛↙奶布丁 2024-09-16 00:15:25

您需要在每次调用 chunkXXX()(但最后一个(父进程))之后放置一个 break;,并从以下位置调用 exit()孩子们大块。

You need to put a break; after each call to chunkXXX() but the last one (father process), and call exit() from the children chunks.

审判长 2024-09-16 00:15:25

wait(NULL) 导致主线程等待,直到所有子线程退出。要打破无限循环,请让 chunk0、chunk1 和 chunk2 调用 exit 来终止。

此外,正如其他人指出的那样,每次调用 chunk0-chunk2 后都需要一个 break 语句。

wait(NULL) causes your main thread to wait until all children have exited. To break the infinite loop have chunk0, chunk1, and chunk2 call exit to terminate.

Furthermore, as others have pointed out, a break statement is needed after each call to chunk0-chunk2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文