Informace Fórum Stav sítě ČVUT IDS Hledání Přihlášení Mapa stránek

Master - C

/*Nasledujici priklad je prevzat z http://www.cs.colorado.edu/~mishras/courses/csci4273/fall01/course-stuff/fork-example/ a doplnen o subroutinu bindprocessor, ktera spojuje child process s urcenym procesorem. Pokud tedy uzijeme ruzne procesory, zkratime tim delku vypoctu (v tomto pripade na polovinu). Krome toho muzeme overit rychlost predavani udaju procedurou pipe tim, ze nastavime mnozstvi predavanych udaju pomoci cisla NUM_PIPE. V prvnim child procesu se scita NUM_PIPE nahodne vygenerovanych cisel a vysledek je predan do parent procesu (mainu) pomoci pipe a vytisten. Prekladame pomoci cc: cc -omaster master.c. Readme file z vyse uvedeneho odkazu Three files illustrate the use of Unix for fork(), execl(), pipe() and wait() system calls. The program in master.c creates two processes that execute the programs in executable files `sqr' and `cube'. The master process communicates integer numbers 0-99 to sqr process and cube process via pipes. The master process prints whole number 0-99 in a file called `whole_num'. The sqr process prints the square of the number received from the master process in a file called `squares'. The cube process prints the cube of the number received from the master process in a file called `cubes'. */

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/processor.h>
#include <errno.h>
#include <time.h>




#define NUM_PIPE 5000000

main()

{ extern int errno;
  int max = 100, i, c1, c2, num1, num2;
  clock_t zac, konec;
  time_t t;
  FILE *fd;
  int mcpipe1[2], mcpipe2[2], pid1,pid2;
  char mc0[10], mc1[10], cc0[10], cc1[10];
  int data;
  unsigned int seed; 
  
  time(&t);
  printf("\n local time (zacatek programu):  %s\n", ctime(&t) );
  zac = clock();
  if (c1 = pipe(mcpipe2)) /* Create a pipe for master and a child
                                                          process */ {
    perror("unable to create the first pipe");
    exit(1);
  }
  if (c1 = pipe(mcpipe1)) /* Create a pipe for master and the other
                                                    child process */ {
    perror("unable to create the second pipe");
    exit(1);
  }
  sprintf(mc0, "%d", mcpipe1[0]);
  sprintf(mc1, "%d", mcpipe1[1]);
  sprintf(cc0, "%d", mcpipe2[0]);
  sprintf(cc1, "%d", mcpipe2[1]);
 
  if ((c1 = fork()) == -1) /* Create the first child process */ {
    perror("unable to fork first child");
    exit(1);
  }
  else if (c1 == 0) {
    /* This is the first child process */
    pid1 = (int) getpid();
    printf("first child pid = %d\n", getpid());
    bindprocessor(BINDPROCESS,  (int)getpid(), (cpu_t)(0) ); 
    printf ("\n\n errno   1.proces   =    %i\n\n", errno);
    execl("sqr", "sqr", mc0, mc1,  NULL);
    exit(1);
  }

  else { 
    if ((c2 = fork()) == -1) /* Create the second child process */ {
      perror("unable to fork second child");
      exit(1);
    }
    else if (c2 == 0) {
      /* This is the second child process */
    pid2 = (int) getpid();
      printf("second child pid = %d\n", getpid());
    bindprocessor(BINDPROCESS,  (int)getpid(), (cpu_t)(1) ); 
    printf ("\n\n errno   2.proces   =    %i\n\n", errno);
      execl("cube", "cube", cc0, cc1, NULL);
      exit(1);
    }
  }
  printf("master pid = %d\n", getpid());
  fd = fopen("whole_num", "w");
  for (i = 0; i < max; i++) {
    fprintf(fd, "%d \n", i);
    write(mcpipe1[1], (void *)&i, sizeof(int));
    write(mcpipe2[1], (void *)&i, sizeof(int));
  }
  data = NUM_PIPE;
  write (mcpipe1[1], (void * ) & data, sizeof(int));
  
  for (i = 0; i < NUM_PIPE; i++)
  {
   data = rand_r (&seed) % 10; 
   write (mcpipe1[1], (void * ) & data, sizeof(int));
  }
  
  wait(&pid1);
  wait(&pid2);

  read (mcpipe1[0], (void *) &num1, sizeof(int));
  close(mcpipe1[0]);
  close(mcpipe1[1]);
  konec = clock();
  time(&t);
  printf("\n local time (konec programu):  %s\n", ctime(&t) );
  printf("\n\n soucet  %i  nahodnych cisel od 0 do 9 :  %i",NUM_PIPE,  num1);
  printf("\n program trval (!pozor: scita casy vsech procesu)  %6.2f\n\n",
	              (konec-zac)/(double) CLOCKS_PER_SEC);

}



Jednoduché příklady