标签:admin temp echo long gre getchar cond space 进程
1.设置监控,当某个进程(monitor_sure_onlyme)不在时就自动把它拉起来#! /bin/bash
while [ 1 ]
do
ranktest=`ps aux | grep rank_test_sure_onlyme | grep -v "grep"`
if [ "$ranktest" == "" ]; then
#echo ranktest not exist
nohup /home/admin/cpuuse/rank_test_sure_onlyme >> /dev/null 2>&1 &
fi
sleep 60
done
2.占用内存的脚本(C++)
(1)RunTempThread,是为了每天7:30~8:30多占资源的
(2)while(1)是死循环,会占掉一个cpu核心
(3)正常情况下,一直是cpu 300%,就是因为main里的这个for循环,启动了三个线程
(4)后来又启动了12个,到7:30的时候,就会再多启动12个线程,会占到1500%
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <thread>
#include <atomic>
#include <chrono>
#include <ctime>
using namespace std;
const int begintime = 730;
const int endtime = 830;
const long long global_gsize = 1024 * 1024 * 1024;
int GetCurTime()
{
time_t timep;
time(&timep);
struct tm *now = localtime(&timep);
return 100 * (now->tm_hour) + now->tm_min;
}
//RunTempThread,是为了每天7:30~8:30多占资源的
void RunTempThread(int count)
{
atomic<bool> running(false);
while (1)
{
int curtime = GetCurTime();
if (!running && curtime > begintime && curtime < endtime)
{
running = true;
for (size_t i = 0; i < count; i++)
{
thread th([&running] {
long long memsize = global_gsize;
char *ptest = new char[memsize];
memset(ptest, 1, memsize); // 比较耗时,多线程申请内存;
while (running)
{
};
delete[] ptest;
});
th.detach();
}
}
if (running && curtime > endtime)
{
running = false;
}
this_thread::sleep_for(chrono::seconds(20));
}
}
int main()
{
int num = 3;
for(int i=0;i < num; ++i)
{
thread th([] {
long long memsize = 4 * global_gsize;
char *ptest = new char[memsize];
memset(ptest, 1, memsize); // 比较耗时,多线程申请内存;
while (1)
{
}
delete[] ptest;
});
th.detach();
}
//getchar();
RunTempThread(12);
return 0;
}
标签:admin temp echo long gre getchar cond space 进程
原文地址:https://blog.51cto.com/10707460/2478025