Question
This assignment is referring to using a Linux Virtual Machine terminal and writing in C. Assignment 1: Add error checking to examplecalls.c Details: The example
This assignment is referring to using a Linux Virtual Machine terminal and writing in C.
Assignment 1: Add error checking to examplecalls.c
Details: The example system calls program examplecalls.c (Skeleton template is shown below) in the system calls notes page has no error checking! Modify the code by adding all necessary error checking to the system calls: If a system call made in the program returns an error, the program should handle the situation properly. (The man page of each system call, especially the return value section, describes the conditions under which the system call may return an error.)
Your answer to this part, the modified version of examplecalls.c, should be a single C source code text file.
Please add error checking to the following examplecalls.c skeleton file:
/* File examplecalls.c */ /* Standard I/O and string libraries */ #include#include /* POSIX API, for many system call wrappers */ #include /* For nanosleep below */ #include /* For open */ #include #include #include #define BUFLEN 1024 #define SLEEPSECS 2 /* Conditional #defines, for passing macros on gcc command line */ #if !defined(NLIM) #define NLIM (1 << 22) #endif #if !defined(NCALLS) #define NCALLS (1 << 24) #endif int main(void) { int j, k, n, fd, nprimes, nl; char buf[BUFLEN]; /* Read buffer */ char msgbuf[BUFLEN]; /* Write buffer */ struct timespec ts; /* NO ERROR CHECKING!!! */ /* A. Count number of lines in the /etc/passwd file */ fd = open("/etc/passwd", O_RDONLY); nl = 0; while ( (n = read(fd, buf, BUFLEN)) > 0) { for (k = 0; k < n; k++) { if (buf[k] == ' ') nl++; } } close(fd); /* Report result of Part A */ k = snprintf(msgbuf, BUFLEN, "A. %d lines in /etc/passwd file ", nl); write(STDOUT_FILENO, msgbuf, k); /* B. (Usermode) Find the number of primes in 1 through NLIM */ nprimes = 0; for (k = 2; k <= NLIM; k++) { for (j = 2; j*j <= k; j++) { if ((k % j) == 0) { break; } } if (j*j > k) { nprimes++; } } /* Report result of Part B */ k = snprintf(msgbuf, BUFLEN, "B. %d primes among 1..%d ", nprimes, NLIM); write(STDOUT_FILENO, msgbuf, k); /* C. Make NCALLS write() system calls */ fd = open("/dev/null", O_WRONLY); strncpy(msgbuf, "aaaabbbbccccddddeeeeffffgggg00001111uuuuvvvvwwwwxxxxyyyyzzzz", 60); for (k = 0; k < NCALLS; k++) { write(fd, msgbuf, 60); } close(fd); /* Report result of Part C */ k = snprintf(msgbuf, BUFLEN, "C. Made %d write(2) calls ", NCALLS); write(STDOUT_FILENO, msgbuf, k); /* D. Suspend process for SLEEPSECS seconds (blocking sleep) */ ts.tv_sec = SLEEPSECS; /* seconds */ ts.tv_nsec = 0; /* nanoseconds */ /* Inform user */ k = snprintf(msgbuf, BUFLEN, "D. Sleeping %d seconds ... ", SLEEPSECS); write(STDOUT_FILENO, msgbuf, k); nanosleep(&ts, NULL); _exit(0); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started