Facebook
From Burly Dove, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 186
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #define PROJECTID 0xFF
  8. #define n 100
  9.  
  10. int main( void )
  11. {
  12.   key_t key;
  13.   int id,size,flag;
  14.   unsigned i;
  15.   unsigned *array;
  16.   printf( "GENERATOR [%u]\n",(unsigned)getpid() );
  17.   key = ftok( "/tmp",PROJECTID );
  18.   flag = IPC_CREAT | 0x1B6;
  19.   size = n*sizeof( unsigned );
  20.   id = shmget( key,size,flag );
  21.   if( id>0 )
  22.   {
  23.     printf( "...utworzono segment wspólny [%u][0x%x]\n\t",id,key );
  24.     array = (unsigned*)shmat( id,NULL,0 );
  25.     for( i=0;i<n;i++ )
  26.     {
  27.       *(array+i)=(4*i+1);
  28.       printf( "." ); fflush( stdout);
  29.     }
  30.     shmdt( (void*)array );
  31.     printf( "\n...zakończono inicjację segmentu wspólnego\n" );
  32.   }
  33.   else { perror( "!.!.!..shmget()..." ); exit( 1 ); }
  34.   return 0;
  35. }
  36.  
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <time.h>
  41. #include <unistd.h>
  42. #include <sys/ipc.h>
  43. #include <sys/shm.h>
  44. #define PROJECTID 0xFF
  45. #define n 100
  46.  
  47. int main( void )
  48. {
  49.   key_t key;
  50.   int id,size,flag;
  51.   struct shmid_ds buffer;
  52.   unsigned i,*array;
  53.   key = ftok( "/tmp",PROJECTID );
  54.   flag = IPC_CREAT | 0x1B6;
  55.   size = n*sizeof( unsigned );
  56.   id = shmget( key,size,flag );
  57.   if( id>0 )
  58.     {
  59.     array = (unsigned*)shmat( id,NULL,0 );
  60.     printf( "100 liczb D.HILBERTa odczytanych z [%u][0x%x]\n",id,key );
  61.     for( i=0;i<n;i++ )
  62.     {
  63.       printf( "%10u",*(array+i) );
  64.       printf( "%c",!((i+1)%5)?('\n'):(' ') );
  65.     }
  66.     printf( "%c",'\n' );
  67.     shmdt( (void*)array );
  68.     (void)shmctl( id,IPC_RMID,&buffer );
  69.   }
  70.   else {
  71.     perror( "!.!.!..shmget()..." );
  72.     exit( 1 );
  73.   }
  74.   return 0;
  75. }