0

Hi ,
here is my code where i tried to separate the computation into a process and the I/O into another process. when i do compile there is no error, but when i run it; it goes spinning and spinning without stoping..
even i declared the lock variabe in a separate header file like this:
extern bool lock;
bool lock= false;
and included as #include"shared.h"
Can any one correct it , thank u

#include <sys/types.h> /* pid_t */
#include <sys/wait.h>  /* waitpid */
#include <stdio.h>     /* printf, perror */
#include <stdlib.h>    /* exit */
#include <unistd.h>    /* _exit, fork */
 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <errno.h>
#include <sys/time.h>
#include <stdbool.h>

#define DATASIZE (10<<20)
bool lock= false;

bool TestAndSet(bool *target)
{
bool rv = *target;
*target = true;
return rv;
}



int main()
{
int counter= 0;

    double A[200][200];
    double B[200][200];
    double C[200][200];
    int i, j, k, m; 
    int outfile, result, count; 
    unsigned seed;

    struct timeval before, after;

    char *buff;

    buff = (char *)malloc(sizeof(char)*(10<<20));

    srandom(seed);
/* ==================================================================*/

    for (i=0; i<200; i++)
    for (j=0; j<200; j++) {
        A[i][j] = random()/100.0;
        B[i][j] = random()/100.0;
    }

/* ==================================================================*/

    gettimeofday(&before, NULL);

   pid_t pid;

   pid = fork();

   if (pid ==-1) {
      /*
       * When fork() returns -1, an error happened.
       */
      perror("fork failed");
      exit(EXIT_FAILURE);
   }

/* ================================================================*/

  else

   if(pid >=0)
   {

/* =================================================================*/
   if (pid == 0) {
      /*
       * When fork() returns 0, we are in the child process.
       */
gettimeofday(&before, NULL);

 for (m=0 ; m < 10; m++) {


while(TestAndSet(&lock))

printf("spinning");

    /* Computation */ /* STARTING COMPUTATION CRITICAL SECTION */ 

    printf("Comuptation process iteration was allowed to start \n");
    for (i=0; i<200; i++)
        for (j=0; j<200; j++) {
        C[i][j] = 0;
        for (k=0; k<200; k++)
            C[i][j] += A[i][k]*B[k][j];
           }

        counter++;
    printf("Comuptation process iteration is finished \n");

    /* ENDING COMPUTATION CRITICAL SECTION */





      } /*===computation iterations=======for*/
lock = false;   

}  /* ================if pid == 0 ==================*/






   else {

      /*
       * When fork() returns a positive number, we are in the parent process
       * and the return value is the PID of the newly created child process.
       */

for (m=0 ; m < 10; m++)
          {

while(TestAndSet(&lock))

printf("spinning");

/*======================critical section =========================*/

    /* I/O */ /* STARTING I/O CRITICAL SECTION */
    printf("I/O process iteration was allowed to start \n");

           outfile = open("testfile", O_RDWR|O_CREAT|O_APPEND, 0777);
           if (outfile <= 0)
            perror("Error opening file\n");
           else {
            result = write(outfile, buff, DATASIZE);
            if (result <= 0)
           perror("Error writing file\n");
                       }   /* if*/



          close(outfile);
          counter++;
    printf("I/O process iteration is finished \n");

}
/* ENDING I/O CRITICAL SECTION */

int status;
   (void)waitpid(0, &status, 0);
   // wait(NULL);
         // } /* for */
//} /*else*/

           free(buff);



    gettimeofday(&after, NULL);

    count = (after.tv_sec - before.tv_sec) * 1e6;
    count += (after.tv_usec - before.tv_usec);


    printf("Total time in usec: %d\n", count);
printf("Counter final value: %d\n", counter);





  } /* parent*/

 } /* parent & child*/

return 0;
}
2
Contributors
1
Reply
12
Views
1 Year
Discussion Span
Last Post by rubberman
0

Your section bracing is bad. Look at it, especially in the child section. Example:

while(TestAndSet(&lock))
    printf("spinning");

There is no brace starting the block after the while() statement. This is not an uncommon beginner problem. BRACE ALL BLOCKS OF CODE! This shows exactly what is related, and what is not.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.