I cannot figure out why my ouput has a core dump. The program should display inf
ID: 3886672 • Letter: I
Question
I cannot figure out why my ouput has a core dump. The program should display info about the cpu, take the time of the report and take the averages of all the times.
Thanks
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#define SHORT 0;
#define LONG 1;
#define STANDARD 2;
int main(int argc, char *argv[])
{
char repTypeName[16]; // Name of Report Type
FILE *thisProcFile; // File pointer for this particular /proc file
char lineBuf[256]; // Buffer for reading a line in the /proc file
int reportType; // Report type = 0, 1, 2
char c1, c2; // for checking first argument (eg : -s)
struct timeval now; // for keeping time
int interval; // Interval between wakeup
int duration; // Duration of sleep
int iteration = 1; // used for loop iteration
int bufSize = 255;
// determine report type
reportType = STANDARD; // report = STANDARD by default
strcpy(repTypeName, "Standard"); //
// if there is argument in command line,(ie: argc is more than 1), read them
if (argc > 1)
{
sscanf(argv[1], "%c%c", &c1, &c2);
// if 1st char in 1st arg isn't "-", tell user how to give commands
if (c1!='-')
{
fprintf(stderr, "usage: observer [-s][-I int dur] ");
exit(1);
}
// if 1st char is "-" check 2nd char, if it is "s", short report.
if (c2=='s')
{
reportType = SHORT;
strcpy(repTypeName,"Short");
}
// else if it is "l", long report, get the interval and duration
if (c2=='l')
{
reportType = LONG;
strcpy(repTypeName, "Long");
interval = atoi(argv[2]);
duration = atoi(argv[3]);
}
}
// Finish Initializing then read time and display message
gettimeofday(&now, NULL); // Get the time of day
printf(" ------- Observer program to get info about system ------ ");
printf("1. Status report type: %s ", repTypeName);
printf("2. Time of report: %s", (float)ctime(&(now.tv_sec)));
// Get host filename and print info from it
thisProcFile=fopen("/proc/cpuinfo","r");
fgets(lineBuf, bufSize, thisProcFile);
printf("3. Machine hostname : %s ", lineBuf);
fclose(thisProcFile);
printf("interval: %d ", interval);
printf("duration: %d ", duration);
// Code to read the relevant /proc files
printf("4. Load averages ");
while (iteration *interval < duration)
{
sleep(interval);
// sample the LoadAvg file;
thisProcFile=fopen("/proc/loadavg","r");
fgets(lineBuf, bufSize, thisProcFile);
printf(" Iteration no %d (time: %d) : %s", iteration, iteration*interval, lineBuf);
iteration = iteration + 1;
fclose(thisProcFile);
}
exit(0);
}
My Output:
------- Observer program to get info about system ------
1. Status report type: Standard
2. Time of report:
Segmentation fault (core dumped)
Explanation / Answer
You have error here
printf("2. Time of report: %s", (float)ctime(&(now.tv_sec)))
You are trying to print float using %s causing segmentation fault. Please correct it to %f
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.