0

I wrote a C program is about Vigenere encrypt and decode. And then, I want to add CPU and Memory usage in this program.

However, I don't know what to add Resource (CPU and Memory) utilization code in this program. I just want to know Resource (CPU and Memory) utilization in this program.

The following code is my C program of vigenere encrypt and decode

#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;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Cyrus
  • 65
  • 1
  • 1
  • 8
  • Possible duplicate of [How can I benchmark C code easily?](https://stackoverflow.com/q/2349776/608639), [Is there a better way to benchmark a C program than timing?](https://stackoverflow.com/q/7456146/608639), etc. – jww Jun 04 '19 at 07:52
  • Maybe I did not understand the question, but I do not think the code you posted here is helping, since it's about something else. However, you may have a look at this -> http://www.linuxhowtos.org/System/procstat.htm . Hope it helps – PhoenixBlue Jun 04 '19 at 08:12

1 Answers1

0

You probably want to have a look at the getrusage() system call.

It can tell you detailed information about what types of memory are in use and how much CPU time is used, both by the process itself and in the kernel.

marcolz
  • 2,880
  • 2
  • 23
  • 28