Esempio n. 1
0
// Due to undefined behavior with multiple processes and shm, I'm using four independent shm's at the same time
void synchro_logic(const char* my_shm_name, const char* other_shm_name, int number_offset, int times) {
    sem_t *     my_semdes = SEM_FAILED;
    sem_t *     other_semdes = SEM_FAILED;
    FILE* pts = fopen(PTS_NAME, "w");

    my_sem_create((char*)my_shm_name, &my_semdes);

    while(my_sem_open((char*)other_shm_name, &other_semdes) != 0) {
        usleep(500000);
    }

    my_sem_wait(&my_semdes);
    if (number_offset > 1) {
        my_sem_wait(&my_semdes);
    }

    for(int count = 0; count < times; count++) {

        fprintf(pts, "Program %i: Number %i\n", number_offset, (4 * count) + number_offset);

        my_sem_post(&other_semdes);
        if(count < times - 1) {
            my_sem_wait(&my_semdes);
        }
    }

    my_sem_post(&my_semdes);
    my_sem_close(&my_semdes);
    my_sem_close(&other_semdes);
    sem_unlink((char*)my_shm_name);
    fclose(pts);
    printf("Program %i: Exit\n", number_offset);
}
int main ( void )
{
    int r;
    pthread_t my_rescuer, grabber;

    r= pthread_mutex_init(&mx[0], NULL);
    assert(!r);
    r= pthread_mutex_init(&mx[1], NULL);
    assert(!r);
    r= pthread_mutex_init(&mx[2], NULL);
    assert(!r);
    r= pthread_mutex_init(&mx[3], NULL);
    assert(!r);

    r= pthread_cond_init(&cv, NULL);
    assert(!r);
    r= pthread_rwlock_init(&rwl, NULL);
    assert(!r);

    quit_now = my_sem_init( "quit_now", 0,0 );
    assert(quit_now);

    r= pthread_create( &grabber, NULL, grab_the_lock, NULL );
    assert(!r);
    sleep(1); /* let the grabber get there first */

    r= pthread_create( &my_rescuer, NULL, rescue_me, NULL );
    assert(!r);
    /* Do stupid things and hope that rescue_me gets us out of
       trouble */

    /* mx is bogus */
    r= pthread_cond_wait(&cv, (pthread_mutex_t*)(1 + (char*)&mx[0]) );

    /* mx is not locked */
    r= pthread_cond_wait(&cv, &mx[0]);

    /* wrong flavour of lock */
    r= pthread_cond_wait(&cv, (pthread_mutex_t*)&rwl );

    /* mx is held by someone else. */
    r= pthread_cond_wait(&cv, &mx[2] );

    r= my_sem_post( quit_now );
    assert(!r);
    r= my_sem_post( quit_now );
    assert(!r);

    r= pthread_join( my_rescuer, NULL );
    assert(!r);
    r= pthread_join( grabber, NULL );
    assert(!r);

    r= my_sem_destroy( quit_now );
    assert(!r);
    return 0;
}
Esempio n. 3
0
int main (int argc, char *argv[])
{
	int write_character = -1;

 g_prog_mode  = PRG_SENDER_MODE;
	
    /*check parameters */
	if ((buffer_size = get_buffer_size(argc, argv)) == RETURN_ERROR)
	{
		return EXIT_FAILURE;
	}
    /*create semaphore and ringbuffer*/
	if (create_environment() == RETURN_ERROR)
	{
		return EXIT_FAILURE;
	}
    /*write ringbuffer*/
	do
	{
	    /*read stdin input*/
		write_character = fgetc(stdin);

        /*decrement semaphore*/
		if (my_sem_wait() == RETURN_ERROR)
		{
			return EXIT_FAILURE;
		}
        /*write ringbuffer*/
		write_char_to_buffer(write_character);

        /*increment semaphore*/
		if (my_sem_post() == RETURN_ERROR)
		{
			return EXIT_FAILURE;
		}
	} while (write_character != EOF);

    /*check for errors in input of stdin*/
	if (ferror(stdin))
	{
		fprintf(stderr, "%s: %s%s\n", g_program_name, "Error while reading input from \"stdin\".", strerror(errno));

		destroy_environment(PRG_ERROR);
		return EXIT_FAILURE;
	}
    /*destroy semaphore and ringbuffer*/
	if (destroy_environment(PRG_SUCCESS) == RETURN_ERROR)
	{
		return EXIT_FAILURE;
	}
	else
	{
		return EXIT_SUCCESS;
	}
}
Esempio n. 4
0
void *down(void *ptr)
{
	int i;
	for (i = 0; i < TOTAL_RUNS; i++)
	{
		my_sem_wait();
		x--;
		my_sem_post();
	}

	printf("Finished subtracting 1 a total of %d times.\n", TOTAL_RUNS);
}
Esempio n. 5
0
void *up(void *ptr)
{
	int i;
	for (i = 0; i < TOTAL_RUNS; i++)
	{
		my_sem_wait();
		x++;
		my_sem_post();
	}

	printf("Finished adding 1 a total of %d times.\n", TOTAL_RUNS);
}