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
  • 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(),...

  • 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...

  • 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...

  • 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...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • Below is the picture for the direction of my program and also the part where I...

    Below is the picture for the direction of my program and also the part where I have problems with I've already finished it and my code works correctly. But what I need to do is to "read the file name from the command line arguments" instead of the standard input (cin) that I did for step 1. I'm not sure how to do that. 2.13 PROGRAM 1: Calculating Coefficient Of Lift For this PROGRAM you will calculate the coefficient of...

  • 1. Suppose you wrote a program that reads data from cin. You are now required to...

    1. Suppose you wrote a program that reads data from cin. You are now required to reimplement it so that you can read data from a file. You are considering the following changes. I. Declare an ifstream variable in_file II. Replace all occurrences of cin with in_file III. Replace all occurrences of > > and get_line with the appropriate operations for ifstream objects What changes do you need to make? I, II, and III II and III I and III...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

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
ADVERTISEMENT