Пример #1
0
// cow函数:输入年数,返回母牛数
int cow(int ye)
{
	if(ye < 4)
		return 1;
	else
		return cow(ye - 1) + cow(ye -3);
}
Пример #2
0
int moo(int x) {
    int y = cow(x);
    if (y == 0) {
    return cow(x);
    }
    else {
    return 1;
    }
}
Пример #3
0
 void Message::setValues(const std::vector<qi::AnyReference>& values, ObjectHost* context, StreamContext* streamContext)
 {
   cow();
   SerializeObjectCallback scb = boost::bind(serializeObject, _1, context);
   for (unsigned i = 0; i < values.size(); ++i)
     encodeBinary(&_p->buffer, values[i], scb, streamContext);
 }
Пример #4
0
 void Message::setAddress(const MessageAddress &address) {
   cow();
   _p->header.id = address.messageId;
   _p->header.service = address.serviceId;
   _p->header.object = address.objectId;
   _p->header.action = address.functionId;
 }
Пример #5
0
void String::paste(strsize_t offset, const char *cp, strsize_t size)
{
    if(!cp)
        return;

    if(!size)
        size = strlen(cp);

    if(!size)
        return;

    cow(size);

    if(!str) {
        String::set(str->text, ++size, cp);
        str->len = --size;
        str->fix();
        return;
    }

    if(offset >= str->len)
        String::set(str->text + str->len, size + 1, cp);
    else {
        memmove(str->text + offset + size, str->text + offset, str->len - offset);
        memmove(str->text + offset, cp, size);
    }
    str->len += size;
    str->fix();
}
Пример #6
0
 void Message::setValue(const AutoAnyReference &value, const Signature& sig, ObjectHost* context, StreamContext* streamContext) {
   cow();
   Signature effective = value.type()->signature();
   if (effective != sig)
   {
     TypeInterface* ti = TypeInterface::fromSignature(sig);
     if (!ti)
       qiLogWarning() << "setValue(): cannot construct type for signature " << sig.toString();
     std::pair<AnyReference, bool> conv = value.convert(ti);
     if (!conv.first.type()) {
       std::stringstream ss;
       ss << "Setvalue(): failed to convert effective value "
          << value.type()->signature().toString()
          << " to expected type "
          << sig.toString() << '(' << ti->infoString() << ')';
       qiLogWarning() << ss.str();
       setType(qi::Message::Type_Error);
       setError(ss.str());
     }
     else
       encodeBinary(&_p->buffer, conv.first, boost::bind(serializeObject, _1, context), streamContext);
     if (conv.second)
       conv.first.destroy();
   }
   else if (value.type()->kind() != qi::TypeKind_Void)
   {
     encodeBinary(&_p->buffer, value, boost::bind(serializeObject, _1, context), streamContext);
   }
 }
Пример #7
0
int main()
{
  ae.openAudio();
  cow();
  sine_2(4);

  sine_1(4);

}
Пример #8
0
 regex_token_iterator& operator++()
 {
    cow();
    if(0 == pdata->next())
    {
       pdata.reset();
    }
    return *this;
 }
Пример #9
0
 void Message::setFunction(qi::uint32_t function)
 {
   cow();
   if (type() == Type_Event)
   {
     qiLogDebug() << "called setFunction() on Type_Event message";
   }
   _p->header.action = function;
 }
Пример #10
0
 void Message::setEvent(qi::uint32_t event)
 {
   cow();
   if (type() != Type_Event)
   {
     qiLogDebug() << "called setEvent() on non Type_Event message";
   }
   _p->header.action = event;
 }
Пример #11
0
void Message::setError(const std::string &error)
{
    QI_ASSERT(type() == Type_Error && "called setError on a non Type_Error message");

    cow();
    // Clear the buffer before setting an error.
    _p->buffer.clear();

    // Error message is of type m (dynamic)
    AnyValue v(AnyReference::from(error), false, false);
    setValue(AnyReference::from(v), "m");
}
Пример #12
0
void String::add(const char *s)
{
    if(!s || !*s)
        return;

    if(!str) {
        set(s);
        return;
    }

    cow(strlen(s));
    str->add(s);
}
Пример #13
0
void fileview::open(const boost::filesystem::path& p)
{
   cow();
   std::ifstream is(p.c_str());
   if(!is)
   {
      std::string msg("Bad file name: ");
      msg += p.string();
      std::runtime_error e(msg);
      boost::throw_exception(e);
   }
   std::istreambuf_iterator<char> in(is);
   std::istreambuf_iterator<char> end;
   std::copy(in, end, std::back_inserter(pimpl->m_data));
}
Пример #14
0
int main()
{
	int year;
	int numbers;
	int i;

	printf("Enter year: ");	
	scanf("%d", &year);		// 获取年份

	numbers = cow(year);   // 求牛的数量

	printf("%d\n", numbers);

	return 0;
}
Пример #15
0
void String::add(char ch)
{
    char buf[2];

    if(ch == 0)
        return;

    buf[0] = ch;
    buf[1] = 0;

    if(!str) {
        set(buf);
        return;
    }

    cow(1);
    str->add(buf);
}
Пример #16
0
bool Modifier::remove(uint64_t key) {
    size_t pIdx;
    unsigned cnt = 0;
    for (auto& hash : {hash1, hash2, hash3}) {
        auto idx = hash(key);
        auto& entry = at(cnt, idx, pIdx);
        auto e = &entry;
        if (e->first == key && e->second != nullptr) {
            if (cow(cnt, pIdx)) {
                e = &at(cnt, idx, pIdx);
            }
            e->second = nullptr;
            --mSize;
            return true;
        }
        ++cnt;
    }
    return false;
}
Пример #17
0
int main(void) {
    initialize();
    srand(time(NULL));

    int SHEEP_INTERVAL = 0;
    int COW_INTERVAL = 0;
    int THIEF_INTERVAL = 0;
    int HUNTER_INTERVAL = 0;

    long long sheep_time = 0;
    long long cow_time = 0;
    long long thief_time = 0;
    long long hunter_time = 0;

    int sheepSleepTime = 0;
    int cowSleepTime = 0;
    int thiefPathTime = 0;
    int hunterPathTime = 0;

    //========================================================
    printf("Input the Sheep Interval:");
    scanf("%d", &SHEEP_INTERVAL);
    printf("Input the maximum time that a sheep grazes:");
    scanf("%d", &sheepSleepTime);
    //=======================================================
    printf("Input the Cow Interval:");
    scanf("%d", &COW_INTERVAL);
    printf("Input the maximum time that a cow grazes:");
    scanf("%d", &cowSleepTime);
    //=======================================================
    printf("Input the Thief Interval:");
    scanf("%d", &THIEF_INTERVAL);
    printf("Input the maximum time that a thief looks for the path:");
    scanf("%d", &thiefPathTime);
    //========================================================
    printf("Input the Hunter Interval:");
    scanf("%d", &HUNTER_INTERVAL);
    printf("Input the maximum time that a hunter looks for the path:");
    scanf("%d", &hunterPathTime);
    //========================================================

    sheep_time += SHEEP_INTERVAL;
    cow_time += COW_INTERVAL;
    thief_time += THIEF_INTERVAL;
    hunter_time += HUNTER_INTERVAL;
    int genflag = 0; // the flag tells what to generate

    long long elapsetime = 0;
    long long lastelapsetime = 0;
    struct timeval lasttime;
    struct timeval curtime;
    gettimeofday(&lasttime, NULL);

    //produce smaug
    pid_t result = fork();
    if (result < 0) {
        printf("fork error\n");
        exit(1);
    }
    if (result == 0) {
        smaug();
    }
    else {
        pid_t r;
        while (1) {
            gettimeofday(&curtime, NULL);
            elapsetime += (curtime.tv_sec - lasttime.tv_sec) * 1000000 + (curtime.tv_usec - lasttime.tv_usec);
//	        pid_t localpid = getpid();
            //printf("In process: %d Elapsed time: %lld\n", localpid,elapsetime);
//            if(elapsetime - lastelapsetime >= 500000)
            lasttime = curtime;

            if (checkTermination()) {

                printf("****************************terminating in parent process**************************************************\n");

                terminateSimulation();
                int status;
                // block till all children exits
                waitpid(-1, &status, 0);
                printf("****************************In main: all children have exited\n");
                releaseResource();
                exit(0);
            }

            if (elapsetime > sheep_time) {
                genflag = 0;
                sheep_time += SHEEP_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > cow_time) {
                genflag = 1;
                cow_time += COW_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > thief_time) {
                genflag = 2;
                thief_time += THIEF_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > hunter_time) {
                genflag = 3;
                hunter_time += HUNTER_INTERVAL;
                r = fork();
                if (r == 0) break;
            }
        }
        //=============================================================
        if (genflag == 0) sheep(rand() % sheepSleepTime);
        else if (genflag == 1) cow(rand() % cowSleepTime);
        else if (genflag == 2) thief(rand() % thiefPathTime);
        else if (genflag == 3) hunter(rand() % hunterPathTime);
        exit(0);
    }
}
Пример #18
0
 void Message::addFlags(qi::uint8_t flags)
 {
   cow();
   _p->header.flags |= flags;
 }
Пример #19
0
void fileview::close()
{
   cow();
   pimpl->m_data.clear();
}
Пример #20
0
int main()
{
 initialize();
 
 printf("Welcome to Smaug World Simulator\n");

 const int seed = 1; //askUserValue("Enter the random value seed");
 const long int maximumCowInterval = 10000000;//askUserValue("Enter maximumCowInterval in (us)");
 const long int maximumThiefInterval = 10000000;//askUserValue("Enter maximumThiefInterval in (us)");
 const long int maximumHunterInterval = 10000000;//askUserValue("Enter maximumHunterInterval in (us)");
 const long int maximumSheepInterval = 10000000;//askUserValue("Enter maximumSheepInterval in (us)");
 const int smaugWinChance = 50; //askUserValue("smaugWinProb (0 to 100)");
 srand(seed);

 
 double cowTimer = 0;
 double sheepTimer = 0;
 double thiefTimer = 0;
 double hunterTimer = 0;
 
 
 parentProcessID = getpid();

 smaugProcessID = -1;
 cowProcessGID = parentProcessID - 1;
 thiefProcessGID = parentProcessID - 2;
 hunterProcessGID = parentProcessID - 3;
 sheepProcessGID = parentProcessID - 4;
  
  pid_t childPID = fork();
  
 if(childPID < 0) {
 	 printf("FORK FAILED\n");
 	 return 1;
 } else if(childPID == 0) {
 	 smaug(smaugWinChance); // run the smaug
 	 return 0;
 }
 
 smaugProcessID = childPID;
 gettimeofday(&startTime, NULL);
 int zombieRemoveCounter = 0; // Variable to kill zombie
 while(*terminateFlagp == 0) 
 {
 	 zombieRemoveCounter++;
 	 double simDuration = timeChange(startTime);
 
 
 	 if(cowTimer - simDuration <= 0) {
 	 	 cowTimer = simDuration + (rand() % maximumCowInterval) / 1000.0;
 	 	 printf("COW CREATED! next cow at: %f\n", cowTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) {
 	 	 	 cow((rand() % maximumCowInterval) / 1000.0);
 	 	 	 return 0;
 	 	 }
 	 }
 	 if(thiefTimer - simDuration <= 0) 
 	 {
 	 	 thiefTimer = simDuration + (rand() % maximumThiefInterval) / 1000.0;
 	 	 printf("THIEF CREATED! next thief at: %f\n", thiefTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) 
 	 	 {
 	 	 	thief((rand() % maximumThiefInterval) / 1000.0);
 	 	 	return 0;
 	 	 }
 	 }
 	 
 	if(hunterTimer - simDuration <= 0) 
 	 {
 	 	 hunterTimer = simDuration + (rand() % maximumHunterInterval) / 1000.0;
 	 	 printf("HUNTER CREATED! next hunter at: %f\n", hunterTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) 
 	 	 {
 	 	 	hunter((rand() % maximumHunterInterval) / 1000.0);
 	 	 	return 0;
 	 	 }
 	 }
 	if(sheepTimer - simDuration <= 0) 
 	 {
 	 	 sheepTimer = simDuration + (rand() % maximumSheepInterval) / 1000.0;
 	 	 printf("SHEEP CREATED! next sheep at: %f\n", sheepTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) 
 	 	 {
 	 	 	sheep((rand() % maximumSheepInterval) / 1000.0);
 	 	 	return 0;
 	 	 }
 	 }

 	 // remove zombie processes once in a 10 runs ~ at most 10 process
 	 if(zombieRemoveCounter % 10 == 0) 
 	 {
 	 	 zombieRemoveCounter -= 10;
 	 	 int w = 0; int status = 0;
 	 	 while( (w = waitpid( -1, &status, WNOHANG)) > 1)
 	 	 {
			printf("REAPED zombie process %d from main loop\n", w);
 	 	 }
 	 }
 }

 terminateSimulation();
 return 0;
}
Пример #21
0
bool Modifier::insert(uint64_t key, void* value, bool replace /*= false*/) {
    LOG_ASSERT(value != nullptr, "Value must not be null");

    // we first check, whether the value exists
    bool res = false;
    bool increment = true;
    unsigned cnt = 0;
    for (auto& h : {hash1, hash2, hash3}) {
        size_t pageIdx;
        auto idx = h(key);
        auto& entry = at(cnt, idx, pageIdx);
        if (entry.first == key && entry.second != nullptr) {
            if (replace) {
                auto e = &entry;
                if (cow(cnt, pageIdx)) {
                    e = &at(cnt, idx, pageIdx);
                }
                e->second = value;
                increment = false;
                res = true;
            }
            goto END;
        }
        ++cnt;
    }
    if (replace) {
        goto END;
    }
    // actual insert comes here
    while (true) {
        // we retry 20 times at the moment
        for (int i = 0; i < 20; ++i) {
            cnt = 0;
            for (auto& h : {hash1, hash2, hash3}) {
                size_t pageIdx;
                auto idx = h(key);
                auto& entry = at(cnt, idx, pageIdx);
                auto e = &entry;
                if (e->second == nullptr) {
                    if (cow(cnt, pageIdx)) {
                        e = &at(cnt, idx, pageIdx);
                    }
                    e->first = key;
                    e->second = value;
                    res = true;
                    goto END;
                } else {
                    assert(e->first != key);
                    if (cow(cnt, pageIdx)) {
                        e = &at(cnt, idx, pageIdx);
                    }
                    std::pair<uint64_t, void*> p = *e;
                    e->first = key;
                    e->second = value;
                    key = p.first;
                    value = p.second;
                }
                ++cnt;
            }
        }
        rehash();
    }
END:
    if (res && increment)
        ++mSize;
    return res;
}
Пример #22
0
 void Message::setService(qi::uint32_t service)
 {
   cow();
   _p->header.service = service;
 }
 void set_size(unsigned int size) { cow(); _ptr()->set_size(size); }
Пример #24
0
 void Message::setFlags(qi::uint8_t flags)
 {
   cow();
   _p->header.flags = flags;
 }
 void memset(unsigned char value) { cow(); _ptr()->memset(value); }
Пример #26
0
 void Message::setId(qi::uint32_t id)
 {
   cow();
   _p->header.id = id;
 }
Пример #27
0
 void Message::setVersion(qi::uint16_t version)
 {
   cow();
   _p->header.version = version;
 }
Пример #28
0
 void Message::setObject(qi::uint32_t object)
 {
   cow();
   _p->header.object = object;
 }
Пример #29
0
 void Message::setBuffer(const Buffer &buffer)
 {
   cow();
   _p->buffer = buffer;
 }
Пример #30
0
 void Message::setType(Message::Type type)
 {
   cow();
   _p->header.type = type;
 }