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) 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.
Your answer to this part, the modified version of examplecalls.c, should be a single C source code text file.
Some examples of error codes:
#define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #define ESRCH 3 /* No such process */ #define EINTR 4 /* Interrupted system call */ #define EIO 5 /* I/O error */ #define ENXIO 6 /* No such device or address */ #define E2BIG 7 /* Argument list too long */ #define ENOEXEC 8 /* Exec format error */ #define EBADF 9 /* Bad file number */ #define ECHILD 10 /* No child processes */ #define EAGAIN 11 /* Try again */ #define ENOMEM 12 /* Out of memory */ #define EACCES 13 /* Permission denied */ #define EFAULT 14 /* Bad address */ #define ENOTBLK 15 /* Block device required */ #define EBUSY 16 /* Device or resource busy */ #define EEXIST 17 /* File exists */ #define EXDEV 18 /* Cross-device link */ #define ENODEV 19 /* No such device */ #define ENOTDIR 20 /* Not a directory */ #define EISDIR 21 /* Is a directory */ #define EINVAL 22 /* Invalid argument */ #define ENFILE 23 /* File table overflow */ #define EMFILE 24 /* Too many open files */ #define ENOTTY 25 /* Not a typewriter */ #define ETXTBSY 26 /* Text file busy */ #define EFBIG 27 /* File too large */ #define ENOSPC 28 /* No space left on device */ #define ESPIPE 29 /* Illegal seek */ #define EROFS 30 /* Read-only file system */ #define EMLINK 31 /* Too many links */ #define EPIPE 32 /* Broken pipe */ #define EDOM 33 /* Math argument out of domain of func */ #define ERANGE 34 /* Math result not representable */
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