示例#1
0
文件: division.c 项目: mmhl/euler
int main(int argc, char *argv[])
{
	int number;
	long int result = 1;
	if(argc != 2)
	{
		return -1;
	}	
	number = strtol(argv[1], NULL, 0);
	numbers_count = calloc(number, sizeof(int)); // How many times does numer appeared?
	numbers_count[0] = 1; //Leave poor 1 alone
	printf("%d\n", number);

	int i,j;
	int mod;
	int all_good = 0;
	for(i = number; i > 1; i--)
	{
		for(j = i; j > 1; j--)
		{
			
			mod = i%j;
			if(mod == 0)
			{
				increase_count(j);
				printf("\n%d%%%d==0. Adding number %d to list\n", i,j,j);
				print_list(number);
				
			}
		}	
		if(all_nums_present(number)) // We got all coeffs! But we can not have
			break;
		result *= i;
		if(all_good)
			break;
	}
// Last step. divide number by outstanding coefficients. We only need one.	
	printf("Result before division: %ld\n", result);
	int coef_count;
	for(i = number; i > 1; i--)
	{
		if((coef_count = get_count(i)) > 1)	
		{
			result /= ((coef_count-1)* i);
			decrease_count_by(coef_count-1, i);			
		}
		print_list(number);
		printf("\n");
	}
	
	printf("\n%ld\n", result);
	
	for(i = number; i > 1; i--)
	{
		printf("\n%d%%%d=%d", result, i, result%i);
		if(result%i != 0) printf(" Test failed!");
	}

	printf("\n");
}
示例#2
0
文件: lab4.c 项目: Felttrip/CS4520
void* workerThread(void *resources){

  //Take the resource, make sure to access and check if there are enough
  do{
    //wait for access
    pthread_mutex_lock(&leMutex);
    //check status
    if((int)available_resources<(int)resources){
     //release mutex if there are not enough resources
     pthread_mutex_unlock(&leMutex);
    }
    else{
     //if there are enough resources use them
     decrease_count(resources);
     printf("Taking %d resources current count = %d\n",resources,available_resources);
     //release access to the resources
     pthread_mutex_unlock(&leMutex);
     break;
    }
  }while(1);

  //do that random sleep
  sleep(rand()%5);

  //Give the resources back, less strict because we are just returning.
  //wait for access
  pthread_mutex_lock(&leMutex);
  //recieve access to resources
  increase_count(resources);
  printf("Returning %d resources current count = %d\n",resources,available_resources);
  //release access to the resources
  pthread_mutex_unlock(&leMutex);

pthread_exit(NULL);
}
示例#3
0
 Reference& operator=(const Reference& rhs)
 {
     decrease_count();
     m_referenced = rhs.m_referenced;
     m_count = rhs.m_count;
     increase_count();
 }
示例#4
0
void task_queue::enqueue_internal(task* task)
{
    auto& sp = task->spec();
    auto throttle_mode = sp.rpc_request_throttling_mode;
    if (throttle_mode != TM_NONE)
    {        
        int ac_value = 0;
        if (_spec->enable_virtual_queue_throttling)
        {
            ac_value = _virtual_queue_length;
        }
        else
        {
            ac_value = count();
        }
               
        if (throttle_mode == TM_DELAY)
        {
            int delay_ms = sp.rpc_request_delayer.delay(ac_value, _spec->queue_length_throttling_threshold);
            if (delay_ms > 0)
            {
                auto rtask = static_cast<rpc_request_task*>(task);
                rtask->get_request()->io_session->delay_recv(delay_ms);

                dwarn("too many pending tasks (%d), delay traffic from %s for %d milliseconds",
                    ac_value,
                    rtask->get_request()->header->from_address.to_string(),
                    delay_ms
                    );
            }
        }
        else
        {
            dbg_dassert(TM_REJECT == throttle_mode, "unknow mode %d", (int)throttle_mode);

            if (ac_value > _spec->queue_length_throttling_threshold)
            {
                auto rtask = static_cast<rpc_request_task*>(task);
                auto resp = rtask->get_request()->create_response();
                task::get_current_rpc()->reply(resp, ERR_BUSY);

                dwarn("too many pending tasks (%d), reject message from %s with trace_id = %016" PRIx64,
                    ac_value,
                    rtask->get_request()->header->from_address.to_string(),
                    rtask->get_request()->header->trace_id
                    );

                task->release_ref(); // added in task::enqueue(pool)
                return;
            }
        }
    }

    tls_dsn.last_worker_queue_size = increase_count();
    enqueue(task);
}
示例#5
0
    referenced<T, N> &operator=(const referenced<T, N> &copy) noexcept
    {
        decrease_count();

        this->iterator = copy.iterator;

        increase_count();

        return *this;
    }
示例#6
0
String::String(const String &right)
{
	length = right.length; // Assign length to that of object on right side.

	index = new char[length]; // Creating a new char array based on the length found.

	for(int i = 0; i < length; ++i) // Assigning each char in the index to the corresponding char from the right side.
	{
		index[i] = right.index[i];
	}

	increase_count(); // Increases total object count.
}
示例#7
0
String::String(const char *chars)
	:length(0) // Assigning initial length.
{
	while(chars[length] != NULL) // Increasing the length of our char array based on the amount of chars given.
	{
		length++;
	}

	index = new char[length]; // Creating a new char array based on the length found.

	for(int i = 0; i < length; ++i) // Assigning each char in the index to the corresponding char given.
	{
		index[i] = chars[i];
	}

	increase_count(); // Increases total object count.
}
示例#8
0
    referenced()
    {
        // map that keeps track of every referenced type (for cleanup)
        if (references == nullptr)
            references = new std::map<void *, unsigned int>;

        this->iterator = references->end();
        this->size = sizeof(T) * N;

        T *t = new T[N];

        std::pair<std::map<void *, unsigned int>::iterator, bool> result = references->insert(std::pair<void *, unsigned int>(reinterpret_cast<void *>(t), 0));

        if (result.second == false)
            throw std::runtime_error("Inserting reference");

        this->iterator = result.first;

        increase_count();
    }
示例#9
0
void* thread_task(void* param) {
    int random = (int)(5*(rand()/(double)RAND_MAX) + 1);
    decrease_count(random);
    increase_count(random);
    pthread_exit(0);
}
示例#10
0
String::String()
	:length(0) // Assigning initial length.
{
	increase_count(); // Increases total object count.
}
示例#11
0
 Reference(const Reference& rhs):
     m_referenced(rhs.m_referenced),
     m_count(rhs.m_count)
 {
     increase_count();
 }
示例#12
0
    referenced(const referenced<T, N> &copy) noexcept
    {
        this->iterator = copy.iterator;

        increase_count();
    }
示例#13
0
void task_queue::enqueue_internal(task* task)
{
    // only apply admission control on rpc request typed tasks
    if (TASK_TYPE_RPC_REQUEST == task->spec().type)
    {
        int ac_value = 0;
        if (_enable_virtual_queue_throttling)
        {
            ac_value = _virtual_queue_length;
        }
        else
        {
            ac_value = approx_count();
        }

        _delayer.delay(ac_value);

        //auto controller = _controllers[idx];
        //if (controller != nullptr)
        //{
        //    int i = 0;
        //    while (!controller->is_task_accepted(t))
        //    {
        //        // any customized rejection handler?
        //        if (t->spec().rejection_handler != nullptr)
        //        {
        //            t->spec().rejection_handler(t, controller);

        //            ddebug("task %s (%016llx) is rejected",
        //                t->spec().name.c_str(),
        //                t->id()
        //                );

        //            return;
        //        }

        //        if (++i % 1000 == 0)
        //        {
        //            dwarn("task queue %s cannot accept new task now, size = %d",
        //                q->get_name().c_str(), q->approx_count());
        //        }
        //        std::this_thread::sleep_for(std::chrono::milliseconds(1));
        //    }
        //}
        //else if (t->spec().type == TASK_TYPE_RPC_REQUEST
        //    && _spec.queue_length_throttling_threshold != 0x0FFFFFFFUL)
        //{
        //    int i = 0;
        //    while (q->approx_count() >= _spec.queue_length_throttling_threshold)
        //    {
        //        // any customized rejection handler?
        //        if (t->spec().rejection_handler != nullptr)
        //        {
        //            t->spec().rejection_handler(t, controller);

        //            ddebug("task %s (%016llx) is rejected because the target queue is full",
        //                t->spec().name.c_str(),
        //                t->id()
        //                );

        //            return;
        //        }

        //        if (++i % 1000 == 0)
        //        {
        //            dwarn("task queue %s cannot accept new task now, size = %d",
        //                q->get_name().c_str(), q->approx_count());
        //        }
        //        std::this_thread::sleep_for(std::chrono::milliseconds(1));
        //    }
        //}
    }

    increase_count();
    enqueue(task);
}