COMP 3000 2012 Week 8 Notes: Difference between revisions
No edit summary |
Shahanahmed (talk | contribs) No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 62: | Line 62: | ||
Register a normal C function as a signal handler<br> | Register a normal C function as a signal handler<br> | ||
They every signal has a default signal handler defined in the C Standard Library.<br> | |||
Most can be overwritten to have user-defined signal handlers, however some, (e.g. sigkill) cannot be overwritten. <br> | |||
Signals can be called at any point in execution.<br> | Signals can be called at any point in execution.<br> | ||
Line 99: | Line 100: | ||
Q: What allocates the storage for environment variables and command-line arguments?<br> | Q: What allocates the storage for environment variables and command-line arguments?<br> | ||
A: The kernel. The kernel is the only thing that can allocate.(execve)<br> | A: The kernel. The kernel is the only thing that can allocate this space.(execve)<br> | ||
This also makes sense because execve is a system call. | This also makes sense because execve is a system call. | ||
Note that the kernel is the only thing that can change the size of a program's data segment directly (the wrapper for this system call is sbrk), but malloc is often implemented to call sbrk in advance, then divide up the space as malloc is called. | |||
</li> | </li> |
Latest revision as of 17:24, 31 October 2012
Practice questions and their associated answers:
-
Q: You can create a sparse file in UNIX by writing sequences of zeros to a file.
A: False. Sparse file means holes. Zeroes aren't holes. You need to move the file pointer. You have to use a seek. The data will be READ as zeros, though... Look up truncate command and strace it. It ain't just writing zeros -
Q: 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?
A:Nothing happens. Env vars aren't globals. They're passed in with Execve. Environments variables are typically process specific. -
Q: In UNIX, there are three permissions associated with the user, the user's group, and everyone else. What are those three permissions?
A: rwx (Read, Write, Execute) -
Q: If you see a zombie process during normal system operation, how can you get rid of it?
A: Kill the parent Why? init will own the zombie process and then kill it -
Q: 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?
A: chmod +x Default non executable. Need to make it executable. (--x) -
Q: System calls cannot normally be made using standard function calls. What CPU mechanism is used by regular processes to invoke kernel functionality?
A: Software interrupt. Signals are a form of of software interrupt.
Note: "A system call is an entry point into the Linux kernel. Usually, system calls are not invoked directly: instead, most system calls have corresponding C library wrapper functions which perform the steps required (e.g., trapping to kernel mode) in order to invoke the system call. Thus, making a system call looks the same as invoking a normal library function." from man 2 intro
"The call to the library function itself does not cause a switch to kernel mode (if the execution was not already in kernel mode) and is usually a normal subroutine call (using, for example, a "CALL" assembly instruction in some Instruction set architectures (ISAs)). The actual system call does transfer control to the kernel (and is more implementation-dependent and platform-dependent than the library call abstracting it). For example, in Unix-like systems, "fork" and "execve" are C library functions that in turn execute instructions that invoke the "fork" and "execve" system calls. Making the system call directly in the application code is more complicated and may require embedded assembly code to be used (in C and C++) as well as knowledge of the low-level binary interface for the system call operation, which may be subject to change over time and thus not be part of the application binary interface; the library functions are meant to abstract this away." from http://en.wikipedia.org/wiki/System_call -
Q: After a fork system call, what runs first, the child process runs or the parent process?
A: Don't know... Undefined. Can not depend on ordering -
Q: Signal handlers can be called at almost any point in a process's execution. (T/F)
A: True. That's the purpose of signals
Register a normal C function as a signal handler
They every signal has a default signal handler defined in the C Standard Library.
Most can be overwritten to have user-defined signal handlers, however some, (e.g. sigkill) cannot be overwritten.
Signals can be called at any point in execution.
"When a signal is sent, the operating system interrupts the target process's normal flow of execution. Execution can be interrupted during any non-atomic instruction. If the process has previously registered a signal handler, that routine is executed. Otherwise the default signal handler is executed." from http://en.wikipedia.org/wiki/Unix_signal -
Q: System V init scripts run programs sequentially or in parallel?
A: Sequentially (sysV). Shell script that runs every command sequentially
Upstart does it in parallel though. This is the reason sysV is being replaced by Upstart. -
Q: Without errors, what does the execve system call return? # Who calls a signal handler?
A: On success, execve() does not return, on error -1 is returned, and errno is set appropriately.
The program itself calls the signal handler -
Q: What program is used to check the consistency of the data structures of a filesystem?
A: fsck <- wrapper function
fsck.FMT <- called by fsck for a specific format (ex: fsck.ext4) -
Q: What allocates the storage for environment variables and command-line arguments?
A: The kernel. The kernel is the only thing that can allocate this space.(execve)
This also makes sense because execve is a system call. Note that the kernel is the only thing that can change the size of a program's data segment directly (the wrapper for this system call is sbrk), but malloc is often implemented to call sbrk in advance, then divide up the space as malloc is called. -
Q: 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?
A: Sparse file. -
Q: 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?
A: lspci, lshw.
In /proc/, you may be able to also.
These commands display the machine's "hardware".
Not /dev/ because that's the abstraction. -
Q:What is the purpose of the PATH environment variable?
A: At the command line, the $PATH variable is where the shell looks for binaries/files. The path is walked when the fullPATH/filename of the command line hasn't been specified -
Q: When would you expect ltrace to output more lines than strace? When should strace output more than ltrace?
A: Some libraries make no sys calls (strlen). Libraries that do basic string and math operations -> no sys calls
Programs with lots of I/O will use lot of sys calls -
Q: What command is used to change the priority of a process?
A: nice and renice
nicess is policy value (default value and you can modify this)
priority (dynamic) is based on niceness and the amount of time the process has run on cpu
Priority doesn't apply to I/O. I/O will continue despite other processing having high priority. I/O will always slow down the system. -
Q: What is the difference between a library call and a function call?
A: The former requires a library card and a telephone.
Spenner2 (talk) nice