COMP 3000 2012 Midterm Material: Difference between revisions
No edit summary |
|||
Line 143: | Line 143: | ||
# Create a C program "mycopy-blocks.c" that takes three arguments: a block size and two filenames A and B as command line arguments. This program should copy the contents of A to B, making B a byte-for-byte copy of A, except that the length of B should be evenly divisible by the block size. The file should be padded with nulls as necessary. Zero bytes in A should consume little to no space in B, however, including the padding. | # Create a C program "mycopy-blocks.c" that takes three arguments: a block size and two filenames A and B as command line arguments. This program should copy the contents of A to B, making B a byte-for-byte copy of A, except that the length of B should be evenly divisible by the block size. The file should be padded with nulls as necessary. Zero bytes in A should consume little to no space in B, however, including the padding. | ||
# How could loopback mounts be useful when working with virtual machines? What conditions must the VM software meet for this to work? | # How could loopback mounts be useful when working with virtual machines? What conditions must the VM software meet for this to work? | ||
Revision as of 19:34, 24 October 2012
Sample Test Questions
- You can create a sparse file in UNIX by writing sequences of zeros to a file.
- Are environment variables ``global variables? Specifically, if you change the value of an environment variable X in one process, what happens to the value of X in other processes?
- In UNIX, there are three permissions associated with the user, the user's group, and everyone else. What are those three permissions?
- If you see a zombie process during normal system operation, how can you get rid of it?
- I can copy a file using I/O redirection as follows: cat /bin/ls > bar. If I now type ./bar, it won't run. However, if I run another command before typing ./bar, I will get a file listing. What needed to be changed to get bar to run?
- System calls cannot normally be made using standard function calls. What CPU mechanism is used by regular processes to invoke kernel functionality?
- After a fork system call, what runs first, the child process runs or the parent process?
- Signal handlers can be called at almost any point in a process's execution. (T/F)
- System V init scripts run programs sequentially or in parallel?
- Without errors, what does the execve system call return? # Who calls a signal handler?
- What program is used to check the consistency of the data structures of a filesystem?
- What allocates the storage for environment variables and command-line arguments?
- A file that has a logical size of one gigabyte but only takes up 100K on disk is called what kind of file in UNIX?
- What is an easy (but not foolproof way) way to tell that you are running in a virtual environment on Linux? What command do you run to get this information?
- What is the purpose of the PATH environment variable?
- When would you expect ltrace to output more lines than strace? When should strace output more than ltrace?
- What command is used to change the priority of a process?
- What is the difference between a library call and a function call?
Lab Questions
Questions from Lab 1
- What Linux distribution are you using? What version? You should look at the file /etc/issue generally to find out.
- What shell do you get by default when you log in? Check by running echo $SHELL. If it is not bash, then type exec bash to switch to it.
- Is your Linux environment running virtualized or on bare metal? How do you know? (If you aren't sure, say so, but explain why.)
- Who installed and configured your Linux environment? What level of access do you have to it?
- A shell command can be one of four things. What are those four things? Explain very briefly.
- On the machine you are on, in what directories are commands external to the shell stored? How can you get this information on the command line?
- Compare the commands ls /usr/bin | more and ls /usr/bin | less. How is the output similar? How are they different?
- How would you create an empty file with the filename empty file.txt? (Yes, that is a space in the filename.)
- How would you output the contents of this file?
- Some shell commands, such as pwd, are both built-in and are external. What is one reason why both versions might be present?
- What are the permissions on your Linux home directory? With those permissions and your knowledge of the other accounts on the system, who has access to your home directory, and what sort of access do they have?
- Give one or more command line strings that use the following operators: >, <, >>, <<, |. Explain briefly what each operator is doing in your examples, both concretely and in terms of STDIN and STDOUT.
- Are there operators that work specifically with STDERR? Explain briefly.
- How do you use these operators with other file descriptors? Explain briefly.
- Give an example of a bash for loop and explain what it does.
- Give an example of a bash if statement and explain what it does.
- With the & you can put processes in the "background". Given that external commands are always run as separate processes from the command shell, what is the key difference between foreground and background processes?
- What are the differences between shell and environment variables? Specifically, what processes have each of them, and to what extent are they shared? HINT: look at the execve system call.
Questions from Lab 2
- How can you get a list of all of the processes running on the system from the command line? Please give the command and required arguments, if any.
- What are five processes that are running on your system as non-regular users (i.e., as users other than the one you logged in as)? What does each do, briefly? Note: please exclude all processes enclosed in [], as those are built in to the kernel.
- How can I restart the graphical login screen on Ubuntu? (Hint: use virtual terminals and Ctrl-Alt-F? combinations to access a terminal that is independent of the GUI. Also, make sure you are aren't logged into a graphical session when you try this!)
- What happens when you send a kill -9 signal to one of the getty processes that are running by default? Why?
- What starts the upstart daemon? When is it started?
- How would you "change the system runlevel" to reboot? Shut down?
- Look at the output of ls -l /etc/init.d/. You will note -> for many of the entries. This arrow shows that those entries are symbolic links. Where do most of these symbolic links point to? Why?
- Install the openssh-server package in your virtual machine from the command line. What command(s) did you use?
- Note that sshd has separate entries in /etc/init and /etc/init.d differ. Why do both exist (when most services have either one or the other)?
- Log messages for the system are stored in /var/log. What are four log files that are present on your system? (Ignore the ones with numeric extensions, those are old versions.) What program wrote each of those files directly?
- In /etc/init.d/ssh there are two lines near the beginning that end with output || exit 0 (should be lines 16 and 17). What do each of these lines do exactly? And what is there purpose?
- What are the equivalent lines, if any, to these || exit 0 lines in /etc/init/ssh?
- What is plymouth?
- BONUS: Trace plymouth's behavior throughout the boot process.
Questions from Lab 3
- Compile the program hello.c (below) with gcc -O hello.c -o hello-dyn and then run it using the command ltrace ./hello-dyn . What dynamic functions does the program call?
- Compile the same program with gcc -O -static hello.c -o hello-static . Run this second binary with ltrace as before. What dynamic functions does the program now call?
- Run strace on the static and dynamically compiled versions of hello. How many system calls do they each produce?
- How can you make the output of hello.c go to the file "hello-output" by changing how it is invoked at the command line?
- How can you make the output of hello.c go to the file "hello-output" by changing its code?
- Compile run-program.c with the command gcc -g run-program.c -o run-program and use it to answer the following questions.
- Is the line "Is this line printed?" printed when you execute ./run-program /bin/ls -a /tmp? Why?
- Change the program to use execve instead of execvp. What is the difference between the two functions?
- Linux signals is a simple form of IPC that is used for a variety of purposes.
- What signal is generated when a program attempts to divide by zero? Give a simple program that generates such a signal.
- How would you send a signal to a process to have it pause execution? Resume?
- What command can lower the priority of an already running process?
- Why do we "open" files? Specifically, to what extent is it possible to do file I/O without open and close operations - assuming you could change the UNIX API?
- What is the relationship between function calls, system calls, and library calls?
- Create run-program-skips.c that is the same as run-program.c except that it ignores every other argument to the program to be run (starting with the first non-program-name argument). Hence, ./run-program-skip /bin/ls ignore -a ignore /tmp will do the same as ./run-program /bin/ls -a /tmp.
- Create sanitize-env.c that does the same as run-program.c except it exec's the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined). Be sure to document how you tested your program.
- Create a simple program receive-term.c that uses sigaction in order to print "Ouch!" when you send the program the TERM signal and then exits. Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal). As part of your answer, give the command for sending the signal.
- Create a simple program nocrash.c that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.
- Create a program run-program-dots.c that works the same as run-program.c, except that it prints one dot every second while a given program runs. Note that it should stop printing dots once the exec'd program terminates. Your solution should use the fork(), sleep(), execve(), and sigaction() calls.
- What is the difference between the PR and NI columns in the top command? Explain in the context of running a CPU-intensive program. (Hint: you should try running a CPU intensive program or two and observe these columns...)
Program Listings
/* hello.c */
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
pid_t p;
p = getpid();
printf("Hello, world! I am process %d.\n", (int) p);
return 0;
}
/* run-program.c */
#include <unistd.h>
#include <stdio.h>
int main( int argc, char *argv[], char *envp[] ) {
pid_t p;
p = getpid();
printf("Hello again. I am process %d.\n", (int) p);
if( argc < 2 ) {
printf( "Insufficient arguments.\n" );
return -1;
}
execvp( argv[1], argv + 1 );
printf( "Is this line printed?\n" );
return 0;
}
Questions from Lab 4
- Run the command truncate -s 1G foo. What is the logical size of foo, and how much space does it consume on disk?
- Run mkfs.ext4 foo. (Say "yes" to operating on a regular file.) What is the logical size of foo now, and how much space does it now consume on disk?
- What command do you run to check the filesystem in foo for errors?
- Run mount foo /mnt. What does this command do?
- Run df. What device is mounted on /mnt? What is this device?
- Run rsync -a -v /etc /mnt. What does this command do? Explain the arguments as well.
- Run umount /mnt. What files can you still access, and what have gone away?
- Run dd if=/dev/zero of=foo conv=notrunc count=10 bs=512. What does this command do?
- Run mount foo /mnt. What error do you get?
- What command can you run to make foo mountable again? What characteristic of the file system enables this command to work?
- Create a simple C program to generate a file consisting of the word "Hello", one thousand bytes of blank space (null bytes), and then "world\n", and then another thousand null bytes, such that the file produced consumes the maximum amount of space given its contents.
- Create a C program "mycopy-blocks.c" that takes three arguments: a block size and two filenames A and B as command line arguments. This program should copy the contents of A to B, making B a byte-for-byte copy of A, except that the length of B should be evenly divisible by the block size. The file should be padded with nulls as necessary. Zero bytes in A should consume little to no space in B, however, including the padding.
- How could loopback mounts be useful when working with virtual machines? What conditions must the VM software meet for this to work?