Question: Write a short program in C (with comments) that uses the open() system call to open the file /dev/null for writing. The file /dev/null is
Write a short program in C (with comments) that uses the open() system call to open the file /dev/null for writing. The file /dev/null is a special file. Any data written to this file simply goes away and is not saved to disk. You can think of /dev/null as a virtual resource that does not really correspond to any physical resource.
After you open /dev/null, you will use a for loop to write a lot of data to this file. In each iteration of the loop, write 10 bytes to /dev/null . The particular values you are trying to write dont really matter; just write the contents of a 10-character array. Run your for loop for 100,000,000 iterations. One note on the second parameter to read and write: write() is used to write sequences of bytes to a file or some other destination (e.g., another program doing a matching read). In C, the second parameter to write() is declared to have type const void * (void * in the case of read). You can think of this parameter as simply a pointer to a sequence of bytes in memory. If you want to write some data, you simply pass in a pointer to the first byte in memory that you want written. If you want to read some data, this pointer should point to a memory buffer that you have allocated large enough to hold all of the data you are trying to read.
Call your program writer.c or writer.py. You can compile this program by typing make writer. Make is a program that knows how to compile other programs from their source code. Using make is a little nicer than running the compiler directly since it will compile your program to an executable named writer rather than a.out. Once your program compiles, time its execution using the time command. If you type time writer (if the current directory is not in your PATH, you may have to type time ./writer), it will run your writer program then report its running time. The output of time will look something like the following (although probably with different times).
real 0m17.230s user 0m7.100s sys 0m8.45s
These numbers describe how long it took to run your program. Here, the value of 7.10 for user says that the 7.10 seconds of CPU time was used by my program (with the CPU running in user mode). The value of 8.45 for sys says that the kernel consumed 8.45 seconds of CPU time working on behalf of my program. These numbers make sense because each of the 100,000,000 calls to write requires a little work from my program (to make the system call) and a little work from the OS (to handle the system call).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
