void* operation_executer(void * args){
    //pthread_mutex_lock(&mutex);
    exec_info * info = (exec_info*) args;

    while(*(info -> exit) == 0) {
        pthread_mutex_lock(&mutex);

        while (operations_queue_empty(info->market->stock_operations) == 1) {
            pthread_cond_wait(&not_operations, &mutex);
        }
        //if(operations_queue_empty(info->market->stock_operations) == 0){
        dequeue_operation(info->market->stock_operations, &op);
        process_operation(info->market, &op);
        //}
        pthread_cond_signal(&operations);
        pthread_mutex_unlock(&mutex);
    }
    while(operations_queue_empty(info->market->stock_operations) == 0){
        pthread_mutex_lock(&mutex);

        dequeue_operation(info->market->stock_operations, &op);
        process_operation(info->market, &op);

        pthread_cond_signal(&operations);
        pthread_mutex_unlock(&mutex);
    }
    //pthread_mutex_unlock(&mutex);
    pthread_exit(0);
}
예제 #2
0
void task4_6::solution::process_line( std::string line )
{
	int eq_pos = line.find("=");
	const std::string& value_name = line.substr( 0, eq_pos-1 );
	line = line.substr( eq_pos + 2 );

	if( value.count( value_name ) )
		throw std::logic_error( "such variable '"+value_name+"' already exists ("+boost::lexical_cast< std::string >( line_index )+")" );

	std::stack< char > operations;
	std::stack< double > numbers;
	double result = 0;

	for( size_t i = 0; i < line.length(); i++)
		if( line[i] != ' ' )
			if( line[i] == '(' )
				operations.push('(');
			else if( line[i] ==')' )
			{
				while( !operations.empty() && operations.top() != '(' )
				{
					process_operation( numbers, operations.top() );
					operations.pop();
				}

				if( operations.empty() )
					throw std::logic_error("not correct expression at "+boost::lexical_cast< std::string >( line_index )+" line");
				
				operations.pop();
			}
			else if( is_operation( line[i] ) )
			{
				while( !operations.empty() && get_priority( operations.top() ) >= get_priority( line[i] ) )
				{
					process_operation( numbers, operations.top() );
					operations.pop();
				}
				operations.push( line[i] ); 
			}
			else
			{
				std::string operand;
				while (i < line.length() && line[i]!=' ' && !is_operation(line[i]) && line[i]!='(' && line[i]!=')')
					operand += line[i++];
				--i;
				double to_push;
				parse_variable ( operand, to_push );
				numbers.push ( to_push );
			}

	while ( !operations.empty() )
	{
		process_operation( numbers, operations.top() );
		operations.pop();
	}

	result = numbers.top();
	value[ value_name ] = result;
}
예제 #3
0
/* Handle a request from a client */
void *handle_request(void *ptr){
        int bytes_read, bytes_write;
        char message[256];
	struct operation operation;
	struct pool_list *node;
	int clientfd;

	while (1) {
		pthread_mutex_lock(&mutex);
		while (list_head == NULL) {
			pthread_cond_wait(&cond, &mutex);
		}
		node = list_head;
		list_head = node->next;
		pthread_mutex_unlock(&mutex);
                clientfd = node->fd;
	        
                //for test	
                bytes_read = read(clientfd, message, 256);
		if(bytes_read < 0) {
			perror("ERROR reading socket");
		}
		else if (bytes_read == 0){
			printf("Client disconnected");
		}
		else {
			parse_message(message, bytes_read, &operation);
			process_operation(&operation, message, &bytes_write);
			write(clientfd, message, bytes_write);
                        printf("thread: %d, received:%s\n", pthread_self(), message);
		}
                //

                close(clientfd);
		free(node);
	}
}
예제 #4
0
파일: child.c 프로젝트: SimoGira/ipc
void child(int id_number, int NPROC, int my_semaphores[], int p1[], int p2[]){
    
    sem_computing = my_semaphores[0];
    sem_wait_data = my_semaphores[1];
    sem_request_result = my_semaphores[2];
    sem_parent = my_semaphores[3];
    
    /* support variable to perform the operation */
    float res;
    int val1, val2, op_id;
    char op;
    
    //###################################################
    int childs_ready;
    //###################################################
    
    /* sem_parent inizialized to {1, 0, 0}:
     * 0: mutex
     * 1: result_ready
     * 2: data_read */
    
    sem_p(sem_parent, 0);
    
    //####################################################
                                                       //#
    read(p1[0], &childs_ready, sizeof(childs_ready));
    
    childs_ready++;
    
    write(p2[1], &childs_ready, sizeof(childs_ready));
                                                       //#
    //####################################################
    
    /* the last child will unlock the parent */
    if(childs_ready == NPROC)
        sem_v(sem_parent, 1);
    
    sem_v(sem_parent, 0);
    
    
    print_child_info("[Child %d] ready!\n", id_number);
    
    
    while(true)
    {
        //child_isFree[id_number] = true;
        
        // si mette in attesa di essere chiamato per il calcolo
        sem_p(sem_wait_data, id_number);
        
        /* make this child busy */
        
        //child_isFree[id_number] = false;
        
        
        /* read data sent from parent */
        print_child_info("[Child %d] unlocked from parent, reading data...\n", id_number);
        
        read(p1[0], &op_id, sizeof(op_id));
        read(p1[0], &val1, sizeof(val1));
        read(p1[0], &val2, sizeof(val2));
        read(p1[0], &op, sizeof(op));
        
        //val1 = current_operation->val1;
        //val2 = current_operation->val2;          /***************************/
        //op = current_operation->operator;        /***** CRITICAL SECTION ****/
        //op_id = current_operation->id;           /***************************/
        
        // avvisa che ho finito di leggere
        
        sem_v(sem_parent, 2);
        
        // termina col comando k
        if(op == 'k'){
            print_child_info("[Child %d] read: 'k' exiting...\n", id_number);
            return;
        }
        
        print_operation_info("[Child %d] read: %d %c %d\n", id_number,val1,op,val2);
        
        // calcola
        print_child_info("[Child %d] processing operation...\n", id_number);
        
        res = process_operation(val1, val2, op);
        
        // avvisa di aver terminato il calcolo
        sem_v(sem_computing, id_number); // calcolo terminato
        
        print_child_info("[Child %d] wait parent for request of result...\n", id_number);
        
        // attende che il padre richieda i dati
        sem_p(sem_request_result, id_number);
        
        print_child_info("[Child %d] write the result...\n", id_number);
        
        
        //scrivi risultato calcolo            /****************************/
        //current_result->val = res;          /***** CRITICAL SECTION *****/
        //current_result->id = op_id;         /****************************/
        
        write(p2[1], &op_id, sizeof(op_id));
        write(p2[1], &res, sizeof(res));
        
        print_child_info("[Child %d] result ready to be read!\n", id_number);
        
        // dice al padre che i dati sono pronti per essere letti
        sem_v(sem_parent, 1);
    }
}
    void server_control_executor::operator()() {

        int port = 0, num_hash_rounds = 0;
        buffer_crypt::array_t shared_secret;
        std::string encryption_algorithm;
        error ret = get_server_properties(
                        port_prop_,
                        port,
                        num_hash_rounds,
                        shared_secret,
                        encryption_algorithm );
        if ( !ret.ok() ) {
            irods::log( PASS( ret ) );
            return;

        }

        if ( shared_secret.empty() ||
                encryption_algorithm.empty() ||
                0 == port ||
                0 == num_hash_rounds ) {
            rodsLog(
                LOG_NOTICE,
                "control plane is not configured properly" );
            return;
        }

        zmq::context_t zmq_ctx( 1 );
        zmq::socket_t  zmq_skt( zmq_ctx, ZMQ_REP );

        int time_out = SERVER_CONTROL_POLLING_TIME_MILLI_SEC;
        zmq_skt.setsockopt( ZMQ_RCVTIMEO, &time_out, sizeof( time_out ) );
        zmq_skt.setsockopt( ZMQ_SNDTIMEO, &time_out, sizeof( time_out ) );

        std::stringstream port_sstr;
        port_sstr << port;
        std::string conn_str( "tcp://*:" );
        conn_str += port_sstr.str();
        zmq_skt.bind( conn_str.c_str() );

        rodsLog(
            LOG_NOTICE,
            ">>> control plane :: listening on port %d\n",
            port );

        server_state& s = server_state::instance();
        while ( server_state::STOPPED != s() ) {

            zmq::message_t req;
            zmq_skt.recv( &req );
            if ( 0 == req.size() ) {
                continue;

            }

            // process the message
            std::string output;
            std::string rep_msg( SERVER_CONTROL_SUCCESS );
            error ret = process_operation( req, output );
            if ( !ret.ok() ) {
                log( PASS( ret ) );
                rep_msg = ret.result();

            }
            else if ( !output.empty() ) {
                rep_msg = output;

            }

            buffer_crypt crypt(
                shared_secret.size(), // key size
                0,                    // salt size ( we dont send a salt )
                num_hash_rounds,      // num hash rounds
                encryption_algorithm.c_str() );

            buffer_crypt::array_t iv;
            buffer_crypt::array_t data_to_send;
            buffer_crypt::array_t data_to_encrypt(
                rep_msg.begin(),
                rep_msg.end() );
            ret = crypt.encrypt(
                      shared_secret,
                      iv,
                      data_to_encrypt,
                      data_to_send );
            if ( !ret.ok() ) {
                irods::log( PASS( ret ) );

            }

            zmq::message_t rep( data_to_send.size() );
            memcpy(
                rep.data(),
                data_to_send.data(),
                data_to_send.size() );

            zmq_skt.send( rep );

        } // while

    } // control operation