int main() { const char *name = "OS_HW1"; // Name of shared memory object to be passed to shm_open int bufSize; // Bounded buffer size int itemCnt; // Number of items to be consumed int shm_fd; // Shared Memory File Descriptor // Write code here to create a shared memory block and map it to gShmPtr // Use the above name // **Extremely Important: map the shared memory block for both reading and writing // Use PROT_READ | PROT_WRITE shm_fd = shm_open(name, O_RDWR , 0666); gShmPtr = mmap(0,SHM_SIZE,PROT_READ | PROT_WRITE,MAP_SHARED,shm_fd,0); // Write code here to read the four integers from the header of the shared memory block // These are: bufSize, itemCnt, in, out // Just call the functions provided below like this: bufSize = GetBufSize(); itemCnt = GetItemCnt(); // Write code here to check that the consumer has read the right values: printf("Consumer reading: bufSize = %d\n",bufSize); printf("Consumer reading: itemCnt = %d\n",itemCnt); printf("Consumer reading: in = %d\n",GetIn()); printf("Consumer reading: out = %d\n",GetOut()); // Write code here to consume all the items produced by the producer // Use the functions provided below to get/set the values of shared variables in, out, bufSize // Use the provided function ReadAtBufIndex() to read from the bounded buffer // **Extremely Important: Remember to set the value of any shared variable you change locally // Use the following print statement to report the consumption of an item: // printf("Consuming Item %d with value %d at Index %d\n", i, val, out); // where i is the item number, val is the item value, out is its index in the bounded buffer int i; for(i=0;i < GetItemCnt(); i++){ while(GetIn() == GetOut()) ; printf("Consuming Item %d with value %d at Index %d\n", i+1, ReadAtBufIndex(GetOut()), GetOut()); //i+1 to show human readable count SetOut((GetOut()+1) % GetBufSize()); } // remove the shared memory segment if (shm_unlink(name) == -1) { printf("Error removing %s\n",name); exit(-1); } return 0; }
void Producer(int bufSize, int itemCnt, int randSeed) { srand(randSeed); // Write code here to produce itemCnt integer values in the range [0-100] // Use the functions provided below to get/set the values of shared variables "in" and "out" // Use the provided function WriteAtBufIndex() to write into the bounded buffer // Use the provided function GetRand() to generate a random number in the specified range // **Extremely Important: Remember to set the value of any shared variable you change locally // Use the following print statement to report the production of an item: // printf("Producing Item %d with value %d at Index %d\n", i, val, in); // where i is the item number, val is the item value, in is its index in the bounded buffer int i; for(i=0;i < GetItemCnt(); i++){ while(((GetIn()+1) % GetBufSize())==GetOut()) ; int rando = GetRand(0,100); WriteAtBufIndex(GetIn(),rando); printf("Producing Item %d with value %d at Index %d\n", i+1, rando, GetIn()); SetIn((GetIn()+1) % GetBufSize()); } printf("Producer Completed\n"); }
bool Game::Fail(){ StatusUnstable(); ExecTrials++; if(ExecTrials >= GetConfig()->MaxExecTrials) { Status = Failed; GetOut()->Put(Parent, OutPrefix, " Maximum execution attempts for game exceeded.", NULL); StatusStable(); return true; } else { StatusStable(); if(cleanup) return true; Halt(5); Start(); return false; } }
void flext_base::ToSysAtom(int n,const t_atom &at) const { outlet *o = GetOut(n); if(LIKELY(o)) { CRITON(); if(IsSymbol(at)) outlet_symbol((t_outlet *)o,const_cast<t_symbol *>(GetSymbol(at))); else if(IsFloat(at)) outlet_float((t_outlet *)o,GetFloat(at)); #if FLEXT_SYS == FLEXT_SYS_MAX else if(IsInt(at)) outlet_flint((t_outlet *)o,GetInt(at)); #endif #if FLEXT_SYS == FLEXT_SYS_PD else if(IsPointer(at)) outlet_pointer((t_outlet *)o,GetPointer(at)); #endif else error("Atom type not supported"); CRITOFF(); } }
void Game::Control(){ pthread_mutex_lock(&controlmutex); std::string line; boost::smatch regex_ret; while(clonk->ReadLine(line)){ StatusUnstable(); GetOut()->Put(Parent, OutPrefix, " ", line.c_str(), NULL); //Scan for events if(regex_match(line, regex_ret, rx::cm_base)){ if(!regex_ret[2].compare("hilf") || !regex_ret[2].compare("help")) { SendMsg("Liste aller Befehle:\n----%list\n--------Listet alle verfügbaren Szenarien auf\n----%start Szenname -lobby:Sekunden -passwort:\"pw\" -liga\n--------Startet ein Szenario. Alles ab -lobby ist optional.\n----%hilf\n--------Gibt das hier aus.\n", NULL); } else if(!regex_ret[2].compare("list")) { StringCollector list("Folgende Szenarien koennen gestartet werden:\n", false); const ScenarioSet * scn; GetConfig()->LockStatus(); for(int i=0; (scn = GetConfig()->GetScen(i)); i++){ const char * name = scn->GetName(0); if(name){ list.Push("-", false); int i=0; while(name) { list.Push(name, false); name = scn->GetName(++i); if(name) list.Push(", ", false); } list.Push("\n"); } } GetConfig()->UnlockStatus(); SendMsg(list.GetBlock(), NULL); } else if(!regex_ret[2].compare("start")) { char * cmd = new char [regex_ret[4].length() + 1]; strncpy(cmd, regex_ret[4].str().data(), regex_ret[4].length() + 1); //I know, .str().data() sucks... Do it better. char * params = strstr(cmd, " -"); ScenarioSet * scn; if(params){ *params = 0; scn = GetConfig()->GetScen(cmd); *params = ' '; } else scn = GetConfig()->GetScen(cmd); if(scn != 0) { scn = new ScenarioSet(*scn); //Make a Copy of it. if(strstr(cmd, " -liga")) scn -> SetLeague(1); else scn -> SetLeague(0); char * pos; if((pos = strstr(cmd, " -lobby:"))){ pos += 8; int time = atoi(pos); if(time > GetConfig()->LobbyTime) time = GetConfig()->LobbyTime; if(time < 10) time = 10; scn -> SetTime(time); } if((pos = strstr(cmd, " -pw:"))){ pos += 5; char chr = ' '; if(*pos == '"') {chr = '"'; pos++;} char * itr = pos; while(*itr++ != chr); chr = *itr; *itr = 0; scn -> SetPW(pos); *itr = chr; //Actually, that wouldn't be necessary. But I feel it is an itty bit more clean... } if(Parent -> Enqueue(scn)){ //Perhaps I should Fix() the scenario... SendMsg("Szenario \"", scn->GetPath(), "\" wurde der Warteliste hinzugefuegt.\n", NULL); } else { SendMsg("Maximale Groesse der Warteliste ueberschritten.\n", NULL); delete scn; //Retour } } else { SendMsg("Szenario nicht gefunden \"", cmd, "\"\n", NULL); } delete [] cmd; } else { SendMsg("Es gibt kein Kommando: \"", regex_ret[2].str().data(), "\". Gib %hilf ein, um alle Kommandos anzuzeigen.\n", NULL); } } else if(Status==Lobby){ if(regex_match(line, rx::gm_gohot)) { SendMsg("Jetzt geht es los!\n", NULL); SendMsg("Viel Glueck und viel Spass!\n", NULL); } else if(regex_match(line, regex_ret, rx::cl_join)){ if(!GetConfig()->GetBan(regex_ret[1].str().c_str())){ SendMsg("Hi! Viel Spass beim Spielen, ", regex_ret[1].str().data(), ".\n", NULL); SendMsg(2, "Mehr ueber diesen Server erfaehrst du unter cserv.game-server.cc\nJeder kann bestimmen, was gehostet wird. Gib %hilf ein!\n", NULL); } } else if(regex_match(line, regex_ret, rx::cl_conn)){ if(const char * reason = GetConfig()->GetBan(regex_ret[1].str().c_str())){ //SendMsg("Sorry, ", regex_ret[1].str().data(), " aber du stehst auf meiner Abschussliste. (", reason, ")\n"); SendMsg("/kick ", regex_ret[1].str().data(), "\n"); } } else if(regex_match(line, regex_ret, rx::cl_part)){ if(!GetConfig()->GetBan(regex_ret[1].str().data())) SendMsg("Boeh, ", regex_ret[1].str().data(), " ist ein Leaver.\n", NULL); /*mysqlpp::Connection conn(false); if (conn.connect(Login.db, Login.addr, Login.usr, Login.pw)) { std::string querystring; //Hier werden die einzelnen Bestandteile zusamemngesetzt querystring = "INSERT INTO 'banned_people'(`pc-name`, `ip-address`, `cause`, `time`) VALUES ('"; querystring += regex_ret[1].str().data(); querystring += "', "; querystring += "'255.255.255.0', "; //NOT YET IMPLENTENED!!! TODO querystring += "'Leaving', "; querystring += "'3')"; //Ende Bestandteile mysqlpp::Query query1 = conn.query(querystring.c_str()); } else { SendMsg("Hast Glück gehabt, ", regex_ret[1].str().data(), ", der MySQL-Server ist tot.\n", NULL); }*/ } else if(regex_match(line, rx::gm_load)){ Status=Load; } } else if(Status==Run) { if(regex_match(line, regex_ret, rx::pl_die)){ SendMsg("Wie die Fliegen, wie die Fliegen. Pass das naechste mal besser auf, ", regex_ret[1].str().data(), ", du Tropf.\n.", NULL); } else if(regex_match(line, rx::gm_tick0)){ SendMsg("/me r0kt!\n", NULL); if(!Settings.Scen->GetLeague()) { #ifdef NO_GAME_TIMED_MSGS Halt(10); #endif SendMsg(20, "/set maxplayer 1\n", NULL); } } } else if(Status==Load) { if(regex_match(line, rx::gm_go)){ Status=Run; } } else if(Status==PreLobby) { if(regex_match(line, rx::ctrl_err) || regex_match(line, rx::gm_exit)) { StatusStable(); Fail(); pthread_mutex_unlock(&controlmutex); return; } if(regex_match(line, rx::gm_lobby)){ Status=Lobby; if(!Settings.Scen->GetLeague()) SendMsg("/set maxplayer 1337\n", NULL); } if(regex_match(line, rx::ms_flood)) { GetOut()->Put(Parent, OutPrefix, " Masterserver complained about too much games from this IP.", NULL); Halt(300); Fail(); } } StatusStable(); } StatusUnstable(); delete clonk; clonk = NULL; StatusStable(); if(Status==PreLobby) Fail(); pthread_mutex_unlock(&controlmutex); return; }