Exemplo n.º 1
0
int main( void ) {

  /* create an a shared memory segment */
  seg_id = shmget ( IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666 );

  if ( seg_id == -1 )
       printf("error\n");   

  /* attach to segment and get a pointer to it */
  mem_ptr = shmat ( seg_id, NULL, 0 );

  /* create a semaphore pair, set both to 0 */ 
  semid = sem_init( 2, 0); 

  /* trap for CTRL-C - use sigaction in your code */
  signal(SIGINT, ctrlc_handler);


  /* create one Reader and one Writer thread. The Reader thread performs a 
     loop of read operations, reading only when the Writer has finished. 
     demonstrates the strong-reader preference protocol; e.g. the writer must 
     wait until the Reader finishes */

  pthread_t W1, W2, R1, R2, R3;

  if (  pthread_create( &W1, NULL,  Writer, (void *)"1" ) != 0)
      printf("Error creating thread\n");
  if (  pthread_create( &W2, NULL,  Writer, (void *)"2" ) != 0)
      printf("Error creating thread\n");
  
  if (  pthread_create( &R1, NULL,  Reader, (void *)"1" ) != 0)
      printf("Error creating thread\n");
  if (  pthread_create( &R2, NULL,  Reader, (void *)"2" ) != 0)
      printf("Error creating thread\n");
  if (  pthread_create( &R3, NULL,  Reader, (void *)"3" ) != 0)
      printf("Error creating thread\n");

   /* rejoin from threads */
  if (  pthread_join(W1, NULL) != 0 )
      printf("Error rejoining from thread\n");
  if (  pthread_join(W2, NULL) != 0 )
      printf("Error rejoining from thread\n");

  if (  pthread_join(R1, NULL) != 0 )
      printf("Error rejoining from thread\n");
  if (  pthread_join(R2, NULL) != 0 )
      printf("Error rejoining from thread\n");
  if (  pthread_join(R3, NULL) != 0 )
      printf("Error rejoining from thread\n");

  /* parent cleans up */
  sleep(5);
  removeid( semid);                // remove semaphore set
  shmctl( seg_id, IPC_RMID, NULL);  // remove shared memory

} // end main
Exemplo n.º 2
0
int main (void) {

    /* set stdout to autoflush - this call is not portable but works on linux */
    setvbuf(stdout, NULL, _IONBF, 0);

    /* create an a shared memory segment - IPC_PRIVATE creates unique set_id */
    shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);

    if (shmid == -1)
        perror("shmget:");   

    /* attach to segment and get a pointer to it */
    shared = shmat(shmid, NULL, 0 );

    /* initialize mem segment */
    *shared=0; 

    /* create a semaphore pair, set both to 0 */ 
    semid = init_my_sems(2, 0); 

    /* trap for CTRL-C */
    struct sigaction sa; 
    sa.sa_handler = ctrlc_handler;
    sigfillset(&sa.sa_mask);   /* block signals while in the handler */
    sigaction(SIGINT, &sa, NULL);

    /* create one Reader and one Writer thread. The Reader thread performs a 
     * loop of read operations, reading only when the Writer has finished. 
     * demonstrates the strong-reader preference protocol; e.g. the writer must 
     * wait until the Reader finishes 
     */

    pthread_t W1, W2, W3, R1, R2, R3, R4;

    if (pthread_create( &W1, NULL,  Writer, (void *)"1" ) != 0)
        perror("Error creating thread\n");
    if (pthread_create( &W2, NULL,  Writer, (void *)"2" ) != 0)
        perror("Error creating thread\n");
    if (pthread_create( &W3, NULL,  Writer, (void *)"3" ) != 0)
        perror("Error creating thread\n");

    if (pthread_create( &R1, NULL,  Reader, (void *)"1" ) != 0)
        perror("Error creating thread\n");
    if (pthread_create( &R2, NULL,  Reader, (void *)"2" ) != 0)
        perror("Error creating thread\n");
    if (pthread_create( &R3, NULL,  Reader, (void *)"3" ) != 0)
        perror("Error creating thread\n");
    if (pthread_create( &R4, NULL,  Reader, (void *)"4" ) != 0)
        perror("Error creating thread\n");

    /* rejoin from threads */
    if (pthread_join(W1, NULL) != 0 )
        perror("Error rejoining from thread\n");
    if (pthread_join(W2, NULL) != 0 )
        perror("Error rejoining from thread\n");
    if (pthread_join(W3, NULL) != 0 )
        perror("Error rejoining from thread\n");

    if (pthread_join(R1, NULL) != 0 )
        perror("Error rejoining from thread\n");
    if (pthread_join(R2, NULL) != 0 )
        perror("Error rejoining from thread\n");
    if (pthread_join(R3, NULL) != 0 )
        perror("Error rejoining from thread\n");
    if (pthread_join(R4, NULL) != 0 )
        perror("Error rejoining from thread\n");

    /* parent cleans up */
    removeid( semid);                // remove semaphore set
    shmctl( shmid, IPC_RMID, NULL);  // remove shared memory

} // end main