Exemple #1
0
static void explore(struct STEP* step)
{
    explored[lenghtE++] = step;
    struct CELL* empty = get_empty(step->state);
    struct CELL* taken = (struct CELL*) malloc(sizeof(struct CELL));
    
    for(enum TURN turn = UP; turn <= LEFT; turn++) {
        taken->row = 0;
        taken->col = 0;
        switch (turn) {
            case UP:
                taken->row = empty->row + 1;
                taken->col = empty->col;
                break;
            case RIGHT:
                taken->row = empty->row;
                taken->col = empty->col - 1;
                break;
            case DOWN:
                taken->row = empty->row - 1;
                taken->col = empty->col;
                break;
            case LEFT:
                taken->row = empty->row;
                taken->col = empty->col + 1;
                break;
        }
        if(taken->row < 0 || taken->row >= SIDE || taken->col < 0 || taken->col >= SIDE) {
            continue;
        }
        
        struct STEP* next = create_step(step);
        next->state[empty->row][empty->col] = next->state[taken->row][taken->col];
        next->state[taken->row][taken->col] = EMPTY;
        int equals = 0;
        for(int i = 0; i < lenghtF; i++) {
            if(equals_states(frontier[i]->state, next->state)) {
                equals = 1;
                break;
            }
        }
        for(int i = 0; i < lenghtE; i++) {
            if(equals_states(explored[i]->state, next->state)) {
                equals = 1;
                break;
            }
        }
        if(equals) {
            free(next);
            continue;
        }
        next->turn = turn;
        rate(next);
        frontier[lenghtF++] = next;
    }
    
    free(empty);
    free(taken);
};
/*!
  Compares this grasp direction to \a p .  If the point and direction
  are the same and the value of empty is the same, then it returns TRUE.  
*/
bool            
GraspDirection::operator==(const GraspDirection& p)
{

  //ATM: Hmmmm why is steffen comparing pointer values?
    if (p.get_point() == get_point() &&
	p.get_dir() == get_dir() &&
	p.get_empty() == get_empty())
	return true;
    return false;
}
int main(int argc, char **argv)
{
    int status = 0;
    int child_pid = 0;
    int child_count = 0;
    int read_count = 0;
    int write_count = 0;
    int empty = 0;
    int full = 0;
    void *shm_addr = NULL;
    int shm_id = 0;
    int sem_id = 0;

    shm_id = safe_shmget();
    sem_id = safe_semget();

    while ( child_count < 5 )
    {

        child_pid = fork();
        if ( child_pid < 0 )    // error
        {
            perror("fork");
            exit(-1);
        }
        else if ( child_pid == 0 ) // child
        {
            shm_id = safe_shmget();
            sem_id = safe_semget();
            struct buffer msg;

            shm_addr = shmat(shm_id, NULL, 0);
            if ( shm_addr == ( (void *) -1 ))
            {
                perror("shmat");
                exit(-1);
            }

            if ( child_count < 2 )  // producer
            {
                while( write_count < 6 )
                {
                    if(is_full(shm_addr))
                    {
                        simple_sleep();
                        continue;
                    }

                    p(sem_id); 
                    fprintf(stdout, "child %d get into critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    report(shm_addr);
                    do
                    {
                        if(is_full(shm_addr))
                        {
                            break;
                        }
                        bzero(&msg, sizeof(msg));
                        snprintf((char *)msg.msg, 128, "child %d is putting msg %d\n", child_count, write_count); 
                        fprintf(stdout, "%s", msg.msg);
                        put_in(shm_addr, &msg);
                        write_count++;
                    }while(0);
                    report(shm_addr);
                    fprintf(stdout, "child %d get out of critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    v(sem_id); 

                }

            }
            else                    // consumer
            {
                while ( read_count < 4 )
                {
                    if ( is_empty(shm_addr) )
                    {
                        simple_sleep();
                        continue;
                    }
                    
                    p(sem_id);
                    fprintf(stdout, "child %d get into critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    report(shm_addr);
                    do
                    {
                        if ( is_empty(shm_addr) )
                        {
                            break;
                        }
                        bzero(&msg, sizeof(msg));
                        read_out(shm_addr, &msg);
                        fprintf(stdout, "child %d read out msg: %s\n", child_count, msg.msg);
                        read_count++;
                    }while(0);
                    report(shm_addr);
                    fprintf(stdout, "child %d get out of critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    v(sem_id);
                }
            }

            exit(0);
        }
        else                    //parent
        {
            child_count = child_count + 1;
            continue;
        }
    }

    for ( child_count=0; child_count<5; child_count++ )
    {
        wait();
    }
    safe_shmdel(shm_id);
    safe_semdel(sem_id);

    return 0;
}