Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 905391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:12:37+00:00 2026-05-15T16:12:37+00:00

I wrote this code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <sys/shm.h>

  • 0

I wrote this code:

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

#define   N  512

void  chunk0(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it);
void  chunk1(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it);
void  chunk2(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it);
void  chunk3(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it);
double get_time(void);

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

    srand ( time(NULL) );
    double start=get_time();

    int shmid;
    if ((shmid = shmget(IPC_PRIVATE, shmsz, IPC_CREAT | 0666)) < 0) {
        perror("shmget");
        exit(1);
    }
    //Now we attach the segment to our data space.
    char *shm;
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }   
    unsigned int *s = (unsigned int *) shm;

    for(iterations=0;iterations<1000;iterations++){
        printf("Iteration #%d\n",iterations+1);
        for(i=0;i<N;i++){
            for(j=0;j<N;j++){
                *(a+(i*N+j))=(rand()%1001);
                *(b+(i*N+j))=(rand()%1001);;
                *(s+(i*N+j))=0;
            }
        }

        pid = fork();
        if (pid == 0) {
             chunk0(s,a,b,plc,iterations);
             break;
        }else {
            pid = fork();
            if (pid == 0){ 
                 chunk1(s,a,b,plc,iterations);
                 break;
            }else {
                pid = fork();
                if (pid == 0){ 
                     chunk2(s,a,b,plc,iterations);
                     break;
                }else {
                    chunk3(s,a,b,plc,iterations);
                    wait(NULL);
                }
            }
        }
        wait(NULL);
    }

    double end=get_time();
    double diff=end-start;
    printf("\n Time for run this code is: %lf seconds \n",diff);

}

void  chunk0(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it)
{
    int i,j,k;

    for(i=0;i<MID;i++){
        for(j=0;j<N;j++){
            for(k=0;k<N;k++){
                *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j)));
            }   
        }
    }
    printf("\tChild process 0 (Iteration %d) is done ***\n",it);
    exit(0);
}
void  chunk1(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it)
{
    int i,j,k;

    for(i=MID;i<MID*2;i++){
        for(j=0;j<N;j++){
            for(k=0;k<N;k++){
                *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j)));
            }
        }
    }
     printf("\tChild process 1 (Iteration %d) is done ***\n",it);
     exit(0);
}
void  chunk2(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it)
{
    int i,j,k;

    for(i=MID*2;i<MID*3;i++){
        for(j=0;j<N;j++){
            for(k=0;k<N;k++){
                *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j)));
            }
        }
    }

     printf("\tChild process 2 (Iteration %d) is done ***\n",it);
     exit(0);
}
void  chunk3(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it)
{
     int i,j,k;

    for(i=MID*3;i<N;i++){
        for(j=0;j<N;j++){
            for(k=0;k<N;k++){
                *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j)));
            }
        }
    }

     printf("\tChild process 3 (Iteration %d) is done ***\n",it);
//     exit(0);
}

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

Program doesn’t wait until one iteration be complete and starts next iteration!
look at results:

Iteration #1
    Child process 0 (Iteration 0) is done ***
    Child process 3 (Iteration 0) is done ***
    Child process 1 (Iteration 0) is done ***
Iteration #2
    Child process 2 (Iteration 0) is done ***
    Child process 0 (Iteration 1) is done ***
    Child process 1 (Iteration 1) is done ***
    Child process 3 (Iteration 1) is done ***
Iteration #3
    Child process 2 (Iteration 1) is done ***
    Child process 0 (Iteration 2) is done ***
    Child process 3 (Iteration 2) is done ***
Iteration #4
    Child process 1 (Iteration 2) is done ***
    Child process 2 (Iteration 2) is done ***
    Child process 1 (Iteration 3) is done ***
    Child process 3 (Iteration 3) is done ***
Iteration #5
    Child process 0 (Iteration 3) is done ***
    Child process 2 (Iteration 3) is done ***
    Child process 0 (Iteration 4) is done ***
    Child process 1 (Iteration 4) is done ***
    Child process 2 (Iteration 4) is done ***
    Child process 3 (Iteration 4) is done ***
Iteration #6
    Child process 1 (Iteration 5) is done ***
    Child process 0 (Iteration 5) is done ***
    Child process 2 (Iteration 5) is done ***
    Child process 3 (Iteration 5) is done ***
Iteration #7
    Child process 0 (Iteration 6) is done ***
    Child process 1 (Iteration 6) is done ***
    Child process 2 (Iteration 6) is done ***
    Child process 3 (Iteration 6) is done ***

What about this?
I used wait(NULL) but …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:12:38+00:00Added an answer on May 15, 2026 at 4:12 pm

    wait() will only wait until a single child process exits. You want to wait until they all have exited. I think you simply need to call it 3 times in a row…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I wrote this code and compiled it, #include <windows.h> #include <stdio.h> #include <stdlib.h> #include
I need to make 4 forks 1000 times. I wrote this but it runs
I'm completely new to C and I use it very rarely. This time i
When I write this code, I get an error on the Sort() Method. ArrayList
This is from my study guide. From my perspective this is almost done but
Hi following code sample seems to have some problem or either it has to
This is literally the first thing I've ever written in C, so please feel
After writing a sample code for the question about converting between timezones , one
I wrote a program to calculate nth root of a number upto 2 decimal
I was going through someone's code where I came across a thread: while(TRUE) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.