TError TContext::CreateDaemonCgs() { DaemonCgs[memorySubsystem] = memorySubsystem->GetRootCgroup()->GetChild(PORTO_DAEMON_CGROUP); DaemonCgs[cpuacctSubsystem] = cpuacctSubsystem->GetRootCgroup()->GetChild(PORTO_DAEMON_CGROUP); for (auto cg : DaemonCgs) { TError error = cg.second->Create(); if (error) return error; // portod-slave error = cg.second->Attach(GetPid()); if (error) return error; // portod master error = cg.second->Attach(GetPPid()); if (error) return error; } if (!config().daemon().debug()) { TError error = memorySubsystem->SetLimit(DaemonCgs[memorySubsystem], config().daemon().memory_limit()); if (error) return error; } return TError::Success(); }
/* PROGRAM: fibo_task This program will repeatedly receive synchronously a positive value k and compute the k-th fibonacci number. Each computed fibonacci number is sent to the "printer" message queue. It exits if it receives k==-1 */ int fibo_task(int argl, void* args) { unsigned int k,f; long msg; char *prbuf = malloc(1024); Pid_t pid; sprintf(prbuf,"Hello from fibo_task process %d.\n",GetPid()); print_string(prbuf); while(1) { // Wait for a request to compute a Fibonacci number. pid = ReceivePort(&msg, 1); assert(pid == GetPPid()); if(msg<0) { sprintf(prbuf, "fibo_task exiting (pid = %d). Bye!\n", GetPid()); print_string(prbuf); free(prbuf); Exit(0); /* Test Exit! */ } k = msg; sprintf(prbuf,"I will compute the %d-th Fibonacci number\n",k); print_string(prbuf); f = fibo(k); sprintf(prbuf,"The %d-th Fibonacci number is %d.\n",k,f); print_string(prbuf); } return 0; }
/* PPROGRAM: printer_task This task creates a mailbox and receives strings, which then prints to the terminal. Conceptually, this is a "service", started by boot_task and ending when boot_task asks it to quit. */ int printer_task(int argl, void* args) { int ok; Message m; ok = CreateMailBox("printer"); assert(ok==0); // synchronize with my parent, he waited for me to create my mailbox SendPort(GetPPid(), 0L); // print loop while(1) { GetMail("printer", &m); if(m.type == PRINTER_PRINT) printf("Process %d: %s",m.sender,(char*)(m.data)); else if(m.type == PRINTER_QUIT) { /* only if parent says so! */ if(m.sender==GetPPid()) break; } /* else ignore */ } return 0; }
TError InitializeDaemonCgroups() { std::vector<TSubsystem *> DaemonSubsystems = { { &MemorySubsystem }, { &CpuacctSubsystem }, }; for (auto subsys : DaemonSubsystems) { auto hy = subsys->Hierarchy; TError error; if (!hy) continue; TCgroup cg = hy->Cgroup(PORTO_DAEMON_CGROUP); if (!cg.Exists()) { error = cg.Create(); if (error) return error; } // portod-slave error = cg.Attach(GetPid()); if (error) return error; // portod master error = cg.Attach(GetPPid()); if (error) return error; } TCgroup cg = MemorySubsystem.Cgroup(PORTO_DAEMON_CGROUP); TError error = MemorySubsystem.SetLimit(cg, config().daemon().memory_limit()); if (error) return error; return TError::Success(); }