linux - 如何在 C 中获取 CPU 和内存使用情况? (在 Linux 系统中)

标签 linux c cpu-usage

我写了一个C程序是关于Vigenere加密和解码的。然后,我想在此程序中添加 CPU 和内存使用情况。

但是,我不知道在这个程序中添加什么资源(CPU 和内存)利用代码。我只想知道此程序中的资源(CPU 和内存)利用率。

以下代码是我的vigenere加解密C程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <omp.h>

int char_to_index(char c)
{
    /*if(c>='A' && c<='Z'){
        c=c+32;
    }*/
    char x = c;
    char result = x - 'a';
    return (int)result;
}

char index_to_char(int c)
{
    int result = c + 97;
    return (char)result;
}

char * xorencrypt(char * message, char * key) {
    int i;
    size_t messagelen = strlen(message);
    size_t keylen = strlen(key);

    char * encrypted = malloc(messagelen+1);
    //start to parallel
    omp_set_num_threads(8);
    #pragma omp parallel \
    private(i)
#pragma omp for
    for (i = 0; i < messagelen; i++) {
        int buf1 = char_to_index(message[i]);
        int buf2 = char_to_index(key[i % keylen]);
        char r = index_to_char(buf1 ^ buf2);
        encrypted[i] = r;
    }

    encrypted[messagelen] = '\0';

    return encrypted;
}

char * xordecode(char * cript, char * key) {
    int i;
    size_t criptlen = strlen(cript);
    size_t keylen = strlen(key);

    char * message = malloc(criptlen+1);
    //start to parallel
    omp_set_num_threads(8);
    #pragma omp parallel \
    private(i)
#pragma omp for
    for (i = 0; i < criptlen; i++) {
        int buf1 = char_to_index(cript[i]);
        int buf2 = char_to_index(key[i % keylen]);
        char r = index_to_char(buf1 ^ buf2);
        message[i] = r;
    }

    message[criptlen] = '\0';

    return message;
}

int    time_substract(struct timeval *result, struct timeval *begin,struct timeval *end)
{
    if(begin->tv_sec > end->tv_sec)    return -1;
    if((begin->tv_sec == end->tv_sec) && (begin->tv_usec > end->tv_usec))    return -2;
    result->tv_sec    = (end->tv_sec - begin->tv_sec);
    result->tv_usec    = (end->tv_usec - begin->tv_usec);

    if(result->tv_usec < 0)
    {
        result->tv_sec--;
        result->tv_usec += 1000000;
    }
    return 0;
}

int main(int argc, const char * argv[]) {
    //count time!!!
    struct timeval start,stop,diff;
    memset(&start,0,sizeof(struct timeval));
    memset(&stop,0,sizeof(struct timeval));
    memset(&diff,0,sizeof(struct timeval));
    gettimeofday(&start,0);
    //count time!!!

    char * message = "By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. By using the article the, we’ve shown that it was one specific day that was long and one specific cup of tea that tasted good. ";
    printf("Input    : %s\n",message);
    char * key = "key";

    char * encrypted = xorencrypt(message, key);
    printf("Encrypted: %s\n", encrypted);

    char * message1 = xordecode(encrypted, key);
    printf("Decrypted: %s\n", message1);

    //count time!!!
    gettimeofday(&stop,0);
    time_substract(&diff,&start,&stop);
    printf("Total time : %d s,%d us\n",(int)diff.tv_sec,(int)diff.tv_usec);
    //count time!!!
    return 0;
}

最佳答案

您可能想看看 getrusage()系统调用。

它可以告诉您进程本身和内核中正在使用的内存类型以及使用了多少 CPU 时间的详细信息。

关于linux - 如何在 C 中获取 CPU 和内存使用情况? (在 Linux 系统中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56439545/

相关文章:

php - UTF-8贯穿始终

linux - 没有信号量的交替

linux - 从源代码构建软件并将其安装在 Linux 上的自定义文件夹中

c - 运行中断/计时器 x 秒?

c - C 中 do-while 循环内的 switch 语句

linux - 使用 vala 的示例 waf 项目

c - 在自定义函数中再次包含相同的头文件,哪些已经包含在主程序中?

c# - asp.net如何获取CPU使用率

ruby - 这是正常的吗? ruby 的 CPU 使用率 50%?

multithreading - 如何使用 CLI 工具确定 Wildfly 11 上哪些线程使用了大量 CPU?