Question

I have included what I need to do for part b, and what I have so far.

I2.2 Part B Write a default version of program to report the behavior of the Linux kernel by inspecting kernel state. The prog3.2 Using argc and argv If you do Part C or D in addition to Part B, you will need to pass parameters to your program from th#include # i ncl ude #include <stdio.h> <stdi ib. h> <string.h> int main (int argc, char argv) char repTypeName [161 /*Determ

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

#define LB_SIZE 20
#define LONG "long"
#define SHORT "short"
#define STANDARD "standard"

int main(int argc, char *argv[])
{

    //open files from each proc/ file
    FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
    FILE *version = fopen("/proc/version", "r");
    FILE *uptime = fopen("/proc/uptime", "r");

    struct timeval currentTime;
    char repTypeName[16];

    int iteration = 0;
    int interval = 0;
    int duration = 0;
    FILE *thisProcFile;
    char c;
    char c1[1];
    char c2[1];
    char lineBuf[LB_SIZE];
    /* Determine report type */
    char *reportType = STANDARD;
    char line[1000];
    int count = 0;
    int t_days, t_hours, t_minutes, t_seconds, boot_time;

    strcpy(repTypeName, "Standard");
    if (argc > 1)
    {
        sscanf(argv[1], "%c%c", &c1, &c2);
        if (c1 != "-")
        {
            fprintf(stderr, "usage: observer [-s][-l int dur]\n");
            exit(1);
        }
        if (c2 == "s")
        {
            reportType = SHORT;
            strcpy(repTypeName, "Short");
        }
        else if (c2 == "l")
        {
            reportType = LONG;
            strcpy(repTypeName, "Long");
            interval = atoi(argv[2]);
            duration = atoi(argv[3]);
        }
    }
    /* Finish initialization */
    /* Code to read the relevant /proc files */

    while ((fgets(line, sizeof(line), cpuinfo)))
    {
        count++;
        if (count == 5)
        {
            printf("CPU %s", line);
        }
    }
    while ((c = fgetc(version)) != EOF)
    {
        printf("%c", c);
    }
    while ((fgets(line, sizeof(line), uptime)))
    {
        sscanf(line, "%d", &boot_time);
        t_days = boot_time / 60 / 60 / 24;
        t_hours = boot_time / 60 / 60 % 24;
        t_minutes = boot_time / 60 % 60;
        t_seconds = boot_time % 60;
        printf("Boot time: %02d:%02d:%02d:%02d\n", t_days, t_hours, t_minutes, t_seconds);
    }
    gettimeofday(&currentTime, NULL); // Get the current time

    printf("Status report type %s at %s", repTypeName, ctime(&(currentTime.tv_sec)));
    /* retrieve the current  the host filename and print it */
    thisProcFile = fopen("/proc/sys/kernel/hostname", "r");
    fgets(lineBuf, LB_SIZE + 1, thisProcFile);
    printf("Machine hostname: %s", lineBuf);
    fclose(thisProcFile);

    while (iteration < duration)
    {
        sleep(interval);
        //fill this out later
        iteration += interval;
    }
    return (0);
}

partc.c

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

#define LB_SIZE 20
#define LONG "long"
#define SHORT "short"
#define STANDARD "standard"

int main(int argc, char *argv[])
{

    //open files from each proc/ file
    FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
    FILE *version = fopen("/proc/version", "r");
    FILE *uptime = fopen("/proc/uptime", "r");
    //partc begins here: open more proc files
    FILE *stat = fopen("/proc/stat", "r");
    FILE *disks = fopen("/proc/diskstats", "r");
    FILE *contact = fopen("/proc/stat", "r");
    FILE *btime = fopen("/proc/stat", "r");
    FILE *processes = fopen("/proc/stat", "r");

    struct timeval currentTime;
    char repTypeName[16];

    int iteration = 0;
    int interval = 0;
    int duration = 0;
    FILE *thisProcFile;
    char c;
    char c1[1];
    char c2[1];
    char lineBuf[LB_SIZE];
    /* Determine report type */
    char *reportType = STANDARD;
    char line[1000];
    int count = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int t_days, t_hours, t_minutes, t_seconds, boot_time;
    int user_mode, system_mode, idle_mode, filler, reads_completed, writes_completed, contact_switch, b_time, process;

    //partb
    while ((fgets(line, sizeof(line), cpuinfo)))
    {
        count++;
        if (count == 5)
        {
            printf("CPU %s", line);
        }
    }
    while ((c = fgetc(version)) != EOF)
    {
        printf("%c", c);
    }
    while ((fgets(line, sizeof(line), uptime)))
    {
        sscanf(line, "%d", &boot_time);
        t_days = boot_time / 60 / 60 / 24;
        t_hours = boot_time / 60 / 60 % 24;
        t_minutes = boot_time / 60 % 60;
        t_seconds = boot_time % 60;
        printf("Boot time: %02d:%02d:%02d:%02d\n", t_days, t_hours, t_minutes, t_seconds);
    }

    //partc
    /* Finish initialization */
    /* Code to read the relevant /proc files */
    strcpy(repTypeName, "Standard");
    if (argc > 1)
    {
        sscanf(argv[1], "%s", c1);
        //testing edge case input
        if (c1[0] != '-' || c1[1] != 's' && c1[1] != 'l')
        {
            fprintf(stderr, "usage: observer [-s][-l int dur]\n");
            exit(1);
        }

        if (c1[1] == 's')
        {
            reportType = SHORT;
            strcpy(repTypeName, "Short");

            //check stat
            fgets(line, sizeof(line), stat);
            sscanf(line, "cpu %d %d %d %d", &user_mode, &filler, &system_mode, &idle_mode);
            printf("User mode :%d\n", user_mode);
            printf("System mode :%d\n", system_mode);
            printf("Idle mode :%d\n\n", idle_mode);

            while ((fgets(line, sizeof(line), disks)))
            {
                count2++;
                if (count2 == 26)
                {
                    sscanf(line, "%d %d %s %d %d %d %d %d", &filler, &filler, &filler, &reads_completed, &filler, &filler, &filler, &writes_completed);
                    printf("Reads Completed: %d\n", reads_completed);
                    printf("Writes Completed: %d\n\n", writes_completed);
                }
            }

            while ((fgets(line, sizeof(line), contact)))
            {
                count3++;
                if (count3 == 11)
                {
                    sscanf(line, "%s %d", &filler, &contact_switch);
                    printf("Contact Switches: %d\n\n", contact_switch);
                }
            }

            while ((fgets(line, sizeof(line), btime)))
            {
                count4++;
                if (count4 == 12)
                {
                    sscanf(line, "%s %d", &filler, &b_time);
                    printf("Boot Time: %d\n\n", b_time);
                }
            }

            while ((fgets(line, sizeof(line), processes)))
            {
                count5++;
                if (count5 == 13)
                {
                    sscanf(line, "%s %d", &filler, &process);
                    printf("Processes: %d\n", process);
                }
            }
        }
        else if (c1[1] == 'l')
        {
            if (argv[2] == NULL || argv[3] == NULL)
            {
                fprintf(stderr, "usage: observer [-s][-l int dur]\n");
                exit(1);
            }
            else
            {
                reportType = LONG;
                strcpy(repTypeName, "Long");
                interval = atoi(argv[2]);
                duration = atoi(argv[3]);
            }
        }
    }

    gettimeofday(&currentTime, NULL); // Get the current time

    printf("Status report type %s at %s", repTypeName, ctime(&(currentTime.tv_sec)));
    /* retrieve the current the host filename and print it */
    thisProcFile = fopen("/proc/sys/kernel/hostname", "r");
    fgets(lineBuf, LB_SIZE + 1, thisProcFile);
    printf("Machine hostname: %s", lineBuf);
    fclose(thisProcFile);

    while (iteration < duration)
    {
        sleep(interval);
        //fill this out later
        iteration += interval;
    }
    return (0);
}

partd.c

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

#define LB_SIZE 20
#define LONG "long"
#define SHORT "short"
#define STANDARD "standard"

int main(int argc, char *argv[])
{

//open files from each proc/ file
FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
FILE *version = fopen("/proc/version", "r");
FILE *uptime = fopen("/proc/uptime", "r");
FILE *stat = fopen("/proc/stat", "r");
//partc begins here
FILE *disks = fopen("/proc/diskstats", "r");
FILE *contact = fopen("/proc/stat", "r");
FILE *btime = fopen("/proc/stat", "r");
FILE *processes = fopen("/proc/stat", "r");
//partd begins here
FILE *memTotal = fopen("/proc/meminfo", "r");
FILE *memFree = fopen("/proc/meminfo", "r");

struct timeval currentTime;
char repTypeName[16];

int iteration = 0;
int interval = 0;
int duration = 0;
FILE *thisProcFile;
char c;
char c1[1];
char c2[1];
char lineBuf[LB_SIZE];

/* Determine report type */
char *reportType = STANDARD;
char line[1000];
int count = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int t_days, t_hours, t_minutes, t_seconds, boot_time;
int user_mode, system_mode, idle_mode, filler, reads_completed, writes_completed, contact_switch, b_time, process;
int mem_Total, mem_Free;

//iterating through the cpuinfo procfile by grabbing each "line" and
//printing it to console
while ((fgets(line, sizeof(line), cpuinfo)))
{
    count++;
    if (count == 5)
    {
      printf("CPU %s\n", line);
    }
}
while ((c = fgetc(version)) != EOF)
{
    printf("%c", c);
}

printf("\n"); //add newline here
//normally we could check if proc is null here as an edge case test...
while ((fgets(line, sizeof(line), uptime)))
{
    sscanf(line, "%d", &boot_time);
    t_days = boot_time / 60 / 60 / 24;
    t_hours = boot_time / 60 / 60 % 24;
    t_minutes = boot_time / 60 % 60;
    t_seconds = boot_time % 60;
    printf("Boot time: %02d:%02d:%02d:%02d\n\n", t_days, t_hours, t_minutes, t_seconds);
}

//copied from notes
strcpy(repTypeName, "Standard");
if (argc > 1)
{
    sscanf(argv[1], "%s", c1);
    if (c1[0] != '-' || c1[1] != 's' && c1[1] != 'l')
    {
      fprintf(stderr, "usage: observer [-s][-l int dur]\n");
      exit(1);
    }
    if (c1[1] == 's')
    {
      reportType = SHORT;
      strcpy(repTypeName, "Short");

      fgets(line, sizeof(line), stat);
      sscanf(line, "cpu %d %d %d %d", &user_mode, &filler, &system_mode, &idle_mode);
      printf("User mode :%d\n", user_mode);
      printf("System mode :%d\n", system_mode);
      printf("Idle mode :%d\n\n", idle_mode);

      //checking if any of the proc files are empty...
      if (stat == NULL || disks == NULL || contact == NULL || btime == NULL || processes == NULL)
      {
        printf("Files could not be opened, proc may or may not be empty :( ");
        exit(0);
      }
      else
      {
        fgets(line, sizeof(line), stat);
        sscanf(line, "CPU: %d %d %d %d", &user_mode, &filler, &system_mode, &idle_mode);
        printf("User Mode :%d\n", user_mode);
        printf("System Mode :%d\n", system_mode);
        printf("Idle Mode :%d\n\n", idle_mode);
      }

      while ((fgets(line, sizeof(line), disks)))
      {
        count2++;
        if (count2 == 26)
        {
          sscanf(line, "%d %d %s %d %d %d %d %d", &filler, &filler, &filler, &reads_completed, &filler, &filler, &filler, &writes_completed);
          printf("Reads Completed: %d\n", reads_completed);
          printf("Writes Completed: %d\n\n", writes_completed);
        }
      }
      while ((fgets(line, sizeof(line), contact)))
      {
        count3++;
        if (count3 == 11)
        {
          sscanf(line, "%s %d", &filler, &contact_switch);
          printf("Contact Switches: %d\n\n", contact_switch);
        }
      }

      while ((fgets(line, sizeof(line), btime)))
      {
        count4++;
        if (count4 == 12)
        {
          sscanf(line, "%s %d", &filler, &b_time);
          printf("Boot Time: %d\n\n", b_time);
        }
      }
      while ((fgets(line, sizeof(line), processes)))
      {
        count5++;
        if (count5 == 13)
        {
          sscanf(line, "%s %d", &filler, &process);
          printf("Processes: %d\n\n", process);
        }
      }

      if (memTotal == NULL)
      {
        printf("File not opened");
        exit(0);
      }
      else
      {
        fgets(line, sizeof(line), memTotal);
        sscanf(line, "%s %d", &filler, &mem_Total);
        printf("Memory Configured:%d\n\n", mem_Total);
      }

      while ((fgets(line, sizeof(line), memFree)))
      {
        count6++;
        if (count6 == 2)
        {
          sscanf(line, "%s %d", &filler, &mem_Free);
          printf("Memory Available:%d\n\n", mem_Free);
        }
      }
    }
    else if (c1[1] == 'l')
    {

      if (argv[2] == NULL || argv[3] == NULL)
      {
        fprintf(stderr, "usage: observer [-s][-l int dur]\n");
        exit(1);
      }
      else
      {
        reportType = LONG;
        strcpy(repTypeName, "Long");
        interval = atoi(argv[2]);
        duration = atoi(argv[3]);
      }
    }
}

/* Finish initialization */
/* Code to read the relevant /proc files */

gettimeofday(&currentTime, NULL); // Get the current time
printf("Status Report Type %s at %s\n", repTypeName, ctime(&(currentTime.tv_sec)));
/* Get the host filename and print it */
thisProcFile = fopen("/proc/sys/kernel/hostname", "r");
fgets(lineBuf, LB_SIZE + 1, thisProcFile);
printf("Machine hostname: %s\n", lineBuf);
fclose(thisProcFile);

while (iteration < duration)
{
    sleep(interval);
    FILE *loadavg = fopen("/proc/loadavg", "r");
    fgets(line, sizeof(line), loadavg);
    printf("Interval: %d Iteration: %d Load Average: %s", interval, iteration, line);
    //Fill this out at Part D
    //        /*sampleLoadAvg();*/
    iteration += interval;
}
return (0);
}

Add a comment
Know the answer?
Add Answer to:
I have included what I need to do for part b, and what I have so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C Programming Adding to your program in part A, go through the command line arguments...

    In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A:             Three two one And then it would display   The smallest string was:...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

  • If you already answer this question, please skip, thanks C Programming. Fill in ... This program...

    If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • In the following i need to print the number of parameters that are passed to main...

    In the following i need to print the number of parameters that are passed to main (args) as well as the actual parameters (*argv). These parameters are all printed to an output file named stdout.log. I run my program 3 times with a shell script and pass different number of parameters each time, however, after opening stdout.log the only thing in stdout.log is the content from the last call and the 3 prior runs are not there at all. What...

  • Hi, need this question ansered in c++, has multiple levels will post again if you can...

    Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that. here is a sketch of the program from the screenshot int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -=...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT