Question: This assignment is referring to using a Linux Virtual Machine terminal and writing in C. Assignment 2: Standard I/O library buffer sizes Questions The following

This assignment is referring to using a Linux Virtual Machine terminal and writing in C.

Assignment 2: Standard I/O library buffer sizes Questions

The following examplecalls.c code will be needed to answer the questions below:

/* 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 n, fd, k, nprimes, j, 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); }

Details: The strace(1) utility can trace and show all system calls made by a process (and, optionally, all descendent processes). To illustrate its use, we can run the examplecalls.c program (template below) under strace(1). However, since with the above values of NLIM and NCALLS, the program makes millions of system calls, the output of strace(1) will be too verbose to be useful. Therefore, we want to redefine NLIM to 4 and NCALLS to 2. This can be done either by changing the code, or on the gcc commandline as follows:

gcc -m32 -static -DNLIM=4 -DNCALLS=2 -o examplecalls examplecalls.c

Now we run the program under strace(1) to see all the system calls it makes. We are redirecting standard output to /dev/null to get a less cluttered output:

$ strace ./examplecalls > /dev/null 

execve("./examplecalls", ["./examplecalls"], [/* 21 vars */]) = 0

uname({sys="Linux", node="rho", ...}) = 0

brk(NULL) = 0x8ef8000

brk(0x8ef8d40) = 0x8ef8d40

set_thread_area(0xffa53580) = 0

readlink("/proc/self/exe", "/z/abhijit/math/udm/2019-winter/"..., 4096) = 66

brk(0x8f19d40) = 0x8f19d40

brk(0x8f1a000) = 0x8f1a000

access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)

open("/etc/passwd", O_RDONLY) = 3

read(3, "root:x:0:0:root:/root:/bin/bash "..., 1024) = 1024

read(3, "x daemon,,,:/home/usbmux:/bin/fa"..., 1024) = 1024

read(3, "y:/bin/false festival:x:117:29::"..., 1024) = 251

read(3, "", 1024) = 0

close(3) = 0

write(1, "A. 42 lines in /etc/passwd file ", 32) = 32

write(1, "B. 2 primes among 1..4 ", 23) = 23

open("/dev/null", O_WRONLY) = 3

write(3, "aaaabbbbccccddddeeeeffffgggg0000"..., 60) = 60

write(3, "aaaabbbbccccddddeeeeffffgggg0000"..., 60) = 60

close(3) = 0

write(1, "C. Made 2 write(2) calls ", 25) = 25

write(1, "D. Sleeping 2 seconds ... ", 26) = 26

nanosleep({2, 0}, NULL) = 0

exit_group(0) = ?

+++ exited with 0 +++

run the appropriate strace(1) tracing tests (shown above), and answer the following questions:

  1. How does standard I/O library buffer input and output when reading from a terminal and writing to a terminal?
  2. What is the standard I/O library output buffer size when writing to a pipe?
  3. What are the standard I/O library input and output buffer sizes when reading from and writing to regular files?
  4. How does standard I/O library buffer input and output when reading from a regular file and writing to a terminal?

All your answers for this part should be placed in a single text file (Unix format) named stdio-answers.txt.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!