COMP 3000 Lab 3 2012: Difference between revisions
(35 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
===Instructions=== | ===Instructions=== | ||
In this lab you will learn about execve, monitoring system calls, dynamic libraries. This lab has 30 points in total. | In this lab you will learn about execve, monitoring system calls, dynamic libraries. This lab has 30 points in total. | ||
Line 9: | Line 7: | ||
You should expect to complete Part A in tutorial. You should submit the answers to both Part A and Part B, however. | You should expect to complete Part A in tutorial. You should submit the answers to both Part A and Part B, however. | ||
'''Indicate where you got the information to answer each question.''' This can be as simple as "the TA told me" or instead "I looked at chapter 7 in book X" or, just, "man page for ls". If you do not include such information, you'll automatically have '''10 points''' deducted from your grade. | '''Indicate where you got the information to answer each question.''' This can be as simple as "the TA told me" or instead "I looked at chapter 7 in book X" or, just, "man page for ls". If you did some sort of experiment(s) to determine the answer, outline them briefly. If you do not include such information, you'll automatically have '''10 points''' deducted from your grade. | ||
You should turn in Lab 3 by 10 PM on Friday, October 12 via [https://www.carleton.ca/culearn/ cuLearn]. Your answers should be in '''plain text''' (the true UNIX file format) or '''PDF'''. No other formats are acceptable (and will result in a zero grade until re-submitted in the correct format). All your | You should turn in Lab 3 by 10 PM on Friday, October 12 via [https://www.carleton.ca/culearn/ cuLearn]. Your answers should be in '''plain text''' (the true UNIX file format) or '''PDF'''. No other formats are acceptable (and will result in a zero grade until re-submitted in the correct format). All your code should compile and run. Partial code fragments or explanations will not be sufficient. | ||
==Part A== | ==Part A== | ||
Line 24: | Line 22: | ||
<ol style="list-style-type: lower-alpha;"> | <ol style="list-style-type: lower-alpha;"> | ||
<li>[2] Is the line "Is this line printed?" printed when you execute '''./run-program /bin/ls -a /tmp'''? Why? </li> | <li>[2] Is the line "Is this line printed?" printed when you execute '''./run-program /bin/ls -a /tmp'''? Why? </li> | ||
<li>[2] Change the program to use execve instead of execvp. What is the difference between the two | <li>[2] Change the program to use execve instead of execvp. What is the difference between the two functions? </li> | ||
</ol> | </ol> | ||
<li> Linux signals is a simple form of IPC that is used for a variety of purposes.</li> | <li> Linux signals is a simple form of IPC that is used for a variety of purposes.</li> | ||
<ol style="list-style-type: lower-alpha;"> | <ol style="list-style-type: lower-alpha;"> | ||
<li> [2] What signal is generated when a program attempts to | <li> [2] What signal is generated when a program attempts to divide by zero? Give a simple program that generates such a signal. </li> | ||
<li> [1] How would you send a signal to a process to have it pause execution? Resume?</li> | <li> [1] How would you send a signal to a process to have it pause execution? Resume?</li> | ||
</ol> | </ol> | ||
Line 38: | Line 36: | ||
<ol> | <ol> | ||
<li value=9> [2] 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? | <li value=9> [2] 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? | ||
<li> [2] What is the relationship between | <li> [2] What is the relationship between function calls, system calls, and library calls? | ||
<li> [2] Create ''run-program- | <li> [2] 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-skips /bin/ls ignore -a ignore /tmp'' will do the same as ''./run-program /bin/ls -a /tmp''. </li> | ||
<li> [2] 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. | <li> [2] 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. | ||
<li> [2] Create a simple program ''receive- | <li> [2] 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.</li> | ||
<li> [2] Create a simple program ''nocrash.c'' that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash. </li> | <li> [2] Create a simple program ''nocrash.c'' that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash. </li> | ||
<li> [2] 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.</li> | <li> [2] 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.</li> | ||
Line 48: | Line 46: | ||
==Program Listings== | ==Program Listings== | ||
<code> | |||
/* hello.c */ | /* hello.c */ | ||
#include <stdio.h> | #include <stdio.h> | ||
Line 55: | Line 53: | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
printf("Hello, world!\n"); | pid_t p; | ||
p = getpid(); | |||
printf("Hello, world! I am process %d.\n", (int) p); | |||
return 0; | return 0; | ||
} | } | ||
</code> | |||
<code> | |||
/* run-program.c */ | /* run-program.c */ | ||
Line 66: | Line 69: | ||
int main( int argc, char *argv[], char *envp[] ) { | 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 ) { | if( argc < 2 ) { | ||
Line 75: | Line 84: | ||
return 0; | return 0; | ||
} | } | ||
</code> | |||
== Answers == | |||
<ol> | |||
<li> __libc_start_main getpid() | |||
__printf_chk </li> | |||
<li> none (it was compiled statically) </li> | |||
<li> | |||
for hello-static: | |||
<pre> | |||
strace -c ./hello-static | |||
Hello, world! I am process 5504. | |||
% time seconds usecs/call calls errors syscall | |||
------ ----------- ----------- --------- --------- ---------------- | |||
-nan 0.000000 0 1 write | |||
-nan 0.000000 0 1 execve | |||
-nan 0.000000 0 1 getpid | |||
-nan 0.000000 0 4 brk | |||
-nan 0.000000 0 1 uname | |||
-nan 0.000000 0 1 mmap2 | |||
-nan 0.000000 0 1 fstat64 | |||
-nan 0.000000 0 1 set_thread_area | |||
------ ----------- ----------- --------- --------- ---------------- | |||
100.00 0.000000 11 total | |||
</pre> | |||
for hello-dyn: | |||
<pre> | |||
strace -c ./hello-dyn | |||
Hello, world! I am process 5507. | |||
% time seconds usecs/call calls errors syscall | |||
------ ----------- ----------- --------- --------- ---------------- | |||
-nan 0.000000 0 1 read | |||
-nan 0.000000 0 1 write | |||
-nan 0.000000 0 2 open | |||
-nan 0.000000 0 2 close | |||
-nan 0.000000 0 1 execve | |||
-nan 0.000000 0 1 getpid | |||
-nan 0.000000 0 3 3 access | |||
-nan 0.000000 0 1 brk | |||
-nan 0.000000 0 1 munmap | |||
-nan 0.000000 0 3 mprotect | |||
-nan 0.000000 0 7 mmap2 | |||
-nan 0.000000 0 3 fstat64 | |||
-nan 0.000000 0 1 set_thread_area | |||
------ ----------- ----------- --------- --------- ---------------- | |||
100.00 0.000000 27 3 total | |||
</pre> | |||
</li> | |||
<li> ./hello-static > hello-output </li> | |||
<li> | |||
<pre> | |||
/* hello-modified-to-output-to-file.c */ | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <unistd.h> | |||
int main(int argc, char *argv[]) | |||
{ | |||
FILE *fp; | |||
pid_t p; | |||
p = getpid(); | |||
/* File open Error checking code */ | |||
if ((fp=fopen("hello-output", "w")) == NULL) { | |||
printf("Cannot open file.\n"); | |||
exit(1); | |||
} | |||
fprintf(fp,"Hello, world! I am process %d.\n", (int) p); | |||
return 0; | |||
} | |||
</pre> | |||
</li> | |||
<li> | |||
<ol style="list-style-type: lower-alpha;"> <li> No, because when execvp is called the program file given by the first argument will be loaded into the caller (process)'s address space and overwrite the program there. the original program in the caller's address space is gone and is replaced by the new program. Like all of the exec functions, execvp replaces the calling process image with a new process image. This has the effect of running a new program with the process ID of the calling process. Note that a new process is not started; the new process image simply overlays the original process image. The execvp function is most commonly used to overlay a process image that has been created by a call to the fork function. A successful call to execvp does not have a return value because the new process image overlays the calling process image. However, a -1 is returned if the call to execvp is unsuccessful. </li> | |||
<li> | |||
<pre> | |||
/* 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 < 3 ) { | |||
printf( "Insufficient arguments.\n" ); | |||
return -1; | |||
} | |||
execve( argv[1], argv + 1, envp); | |||
printf( "Is this line printed?\n" ); | |||
return 0; | |||
} | |||
</pre> | |||
</li> </ol></li> <li> | |||
<ol style="list-style-type: lower-alpha;"> <li> runtime error - Floating point exception (core dumped) | |||
<pre> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
int main( void ) | |||
{ | |||
int dividend = 50; | |||
int divisor = 0; | |||
int quotient; | |||
quotient = dividend / divisor; | |||
} | |||
</pre></li> | |||
<li> to pause SIGSTOP (ctrl-s) or SIGTSTP (ctrl-z) to resume SIGCONT or fg | |||
kill -SIGSTOP [pid] | |||
kill -SIGCONT [pid] | |||
</li> </ol> </li> | |||
<li> renice {priority} pid | |||
Users can only change the nice value of processes which they own. | |||
User cannot start processes with nice values less than 20. | |||
User cannot lower the nice values of their processes after they've raised them. | |||
As usual root has full access to renice command. | |||
</li> | |||
<li>Operating systems open files to 1) establish a mapping between a pathname and the contents of the file so that the pathname doesn't have to be parsed on every file access, and 2) so the OS can allocate resources (particularly buffers) to accelerate access to the file. Those buffers can be released (and data flushed to disk) when the file is closed. | |||
<li>Library calls are function calls, just to functions stored in dynamic libraries that are "included" with the system. (Note that what is included or not is an ambiguous concept in most Linux distributions.) System calls are invocations of the kernel (typically via a software interrupt). A system call's functionality is typically implemented by C functions; because the kernel must run in supervisor mode and regular programs run in user mode, regular function calls don't work between them. Indeed, userspace programs cannot even directly access the memory of the kernel for security reasons; that is why system calls must be vectored via software interrupts. | |||
<li><pre> | |||
/* run-program-skips.c */ | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
int main( int argc, char *argv[], char *envp[] ) { | |||
pid_t p; | |||
int i; | |||
char **nargv; | |||
int nargc; | |||
p = getpid(); | |||
printf("Hello again. I am process %d.\n", (int) p); | |||
if( argc < 2 ) { | |||
printf( "Insufficient arguments.\n" ); | |||
return -1; | |||
} | |||
nargv = argv + 1; | |||
nargc = argc/2; | |||
for (i=1; i < nargc; i++) { | |||
nargv[i] = nargv[(i*2)]; | |||
} | |||
nargv[nargc] = (char *) NULL; | |||
execvp( nargv[0], nargv ); | |||
printf( "Is this line printed?\n" ); | |||
return 0; | |||
} | |||
</pre> | |||
<li><pre> | |||
/* sanitize-env.c */ | |||
/* | |||
Test this program by giving it /usr/bin/env as the program to execute. | |||
This program dumps its environment to standard out. | |||
*/ | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <string.h> | |||
int main( int argc, char *argv[], char *envp[] ) { | |||
pid_t p; | |||
char *nenvp[2]; | |||
int nenvc = 0; | |||
int i; | |||
p = getpid(); | |||
printf("Hello again. I am process %d.\n", (int) p); | |||
if( argc < 2 ) { | |||
printf( "Insufficient arguments.\n" ); | |||
return -1; | |||
} | |||
for (i = 0; envp[i]; i++) { | |||
if (strncmp(envp[i],"PATH=",5)==0) { | |||
nenvp[nenvc] = envp[i]; | |||
nenvc++; | |||
break; | |||
} | |||
} | |||
for (i = 0; envp[i]; i++) { | |||
if (strncmp(envp[i],"TERM=",5)==0) { | |||
nenvp[nenvc] = envp[i]; | |||
nenvc++; | |||
break; | |||
} | |||
} | |||
nenvp[nenvc] = (char *) NULL; | |||
execve( argv[1], argv + 1,nenvp ); | |||
printf( "Is this line printed?\n" ); | |||
return 0; | |||
} | |||
</pre> | |||
<li><pre> | |||
/* receive-term.c */ | |||
/* To test handler, run killall -s SIGTERM receive-term */ | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <signal.h> | |||
static void ouch(int signal, siginfo_t *info, void *context) { | |||
printf("Ouch!\n"); | |||
exit(0); | |||
} | |||
int main(int argc, char *argv[]) { | |||
struct sigaction act; | |||
act.sa_sigaction = &ouch; | |||
act.sa_flags = SA_SIGINFO; | |||
sigaction(SIGTERM, &act, NULL); | |||
while (1) { | |||
printf("."); | |||
fflush(NULL); | |||
sleep(1); | |||
} | |||
return 0; /* main should never return */ | |||
} | |||
</pre> | |||
<li>Note that your program cannot do anything after the SEGV signal is handled as the invalid pointer will just be accessed again. There does exist libraries for allowing SEGV to be handled (e.g., libsigsegv), but this is in general for userspace virtual memory management. | |||
<pre> | |||
/* nocrash.c */ | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <signal.h> | |||
static void ouch(int signal, siginfo_t *info, void *context) { | |||
printf("Oops, my mistake\n"); | |||
exit(0); | |||
} | |||
int main(int argc, char *argv[]) { | |||
struct sigaction act; | |||
char test, *bad; | |||
act.sa_sigaction = &ouch; | |||
act.sa_flags = SA_SIGINFO; | |||
sigaction(SIGSEGV, &act, NULL); | |||
bad = (char *) 12346; | |||
test = bad[0]; | |||
printf("test = %c, bad = %s\n", test, bad); | |||
return 0; | |||
} | |||
</pre> | |||
<li><pre> | |||
/* run-program-dots.c */ | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <signal.h> | |||
#include <stdlib.h> | |||
#include <sys/types.h> | |||
#include <sys/wait.h> | |||
static void reapchild(int signal, siginfo_t *info, void *context) { | |||
int status; | |||
printf("\n"); /* end the line of dots */ | |||
wait(&status); /* don't really have to wait since we're exiting */ | |||
exit(0); | |||
} | |||
int main( int argc, char *argv[], char *envp[] ) { | |||
pid_t p; | |||
struct sigaction act; | |||
act.sa_sigaction = &reapchild; | |||
act.sa_flags = SA_SIGINFO; | |||
sigaction(SIGCHLD, &act, NULL); | |||
if ((p = fork())) { | |||
/* parent */ | |||
while (1) { | |||
sleep(1); | |||
printf("."); | |||
fflush(NULL); | |||
} | |||
} else { | |||
/* child */ | |||
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; | |||
} | |||
} | |||
</pre> | |||
<li>NI is the static priority of the process - its "niceness" value. This value can be increased (= priority lowered) by an unprivileged user for their processes using the renice command or the nice system call. (The nice command is used to start a process with a given priority.) The root user can decrease the niceness value as well (increasing the process's priority). The PR column is the process's dynamic priority, a value used by the scheduler to determine which process should run next. The PR value is a function a process's static priority (the NI value) and its recent execution history (is it waiting for I/O, has it used a lot of CPU time, etc). | |||
</ol> |
Latest revision as of 17:50, 13 November 2012
Instructions
In this lab you will learn about execve, monitoring system calls, dynamic libraries. This lab has 30 points in total.
You may use any machine running Ubuntu 12.04 or equivalent. You may need to install software if a given utility is not already installed; other than that, you do not need administrative access to the machine.
You should expect to complete Part A in tutorial. You should submit the answers to both Part A and Part B, however.
Indicate where you got the information to answer each question. This can be as simple as "the TA told me" or instead "I looked at chapter 7 in book X" or, just, "man page for ls". If you did some sort of experiment(s) to determine the answer, outline them briefly. If you do not include such information, you'll automatically have 10 points deducted from your grade.
You should turn in Lab 3 by 10 PM on Friday, October 12 via cuLearn. Your answers should be in plain text (the true UNIX file format) or PDF. No other formats are acceptable (and will result in a zero grade until re-submitted in the correct format). All your code should compile and run. Partial code fragments or explanations will not be sufficient.
Part A
- [1] 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?
- [1] 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?
- [2] Run strace on the static and dynamically compiled versions of hello. How many system calls do they each produce?
- [1] 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?
- [1] 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.
- [2] Is the line "Is this line printed?" printed when you execute ./run-program /bin/ls -a /tmp? Why?
- [2] 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.
- [2] What signal is generated when a program attempts to divide by zero? Give a simple program that generates such a signal.
- [1] How would you send a signal to a process to have it pause execution? Resume?
- [1] What command can lower the priority of an already running process?
Part B
- [2] 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?
- [2] What is the relationship between function calls, system calls, and library calls?
- [2] 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-skips /bin/ls ignore -a ignore /tmp will do the same as ./run-program /bin/ls -a /tmp.
- [2] 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.
- [2] 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.
- [2] Create a simple program nocrash.c that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.
- [2] 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.
- [1] 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;
}
Answers
- __libc_start_main getpid() __printf_chk
- none (it was compiled statically)
-
for hello-static:
strace -c ./hello-static Hello, world! I am process 5504. % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- -nan 0.000000 0 1 write -nan 0.000000 0 1 execve -nan 0.000000 0 1 getpid -nan 0.000000 0 4 brk -nan 0.000000 0 1 uname -nan 0.000000 0 1 mmap2 -nan 0.000000 0 1 fstat64 -nan 0.000000 0 1 set_thread_area ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000000 11 total
for hello-dyn:
strace -c ./hello-dyn Hello, world! I am process 5507. % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- -nan 0.000000 0 1 read -nan 0.000000 0 1 write -nan 0.000000 0 2 open -nan 0.000000 0 2 close -nan 0.000000 0 1 execve -nan 0.000000 0 1 getpid -nan 0.000000 0 3 3 access -nan 0.000000 0 1 brk -nan 0.000000 0 1 munmap -nan 0.000000 0 3 mprotect -nan 0.000000 0 7 mmap2 -nan 0.000000 0 3 fstat64 -nan 0.000000 0 1 set_thread_area ------ ----------- ----------- --------- --------- ---------------- 100.00 0.000000 27 3 total
- ./hello-static > hello-output
-
/* hello-modified-to-output-to-file.c */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { FILE *fp; pid_t p; p = getpid(); /* File open Error checking code */ if ((fp=fopen("hello-output", "w")) == NULL) { printf("Cannot open file.\n"); exit(1); } fprintf(fp,"Hello, world! I am process %d.\n", (int) p); return 0; }
-
- No, because when execvp is called the program file given by the first argument will be loaded into the caller (process)'s address space and overwrite the program there. the original program in the caller's address space is gone and is replaced by the new program. Like all of the exec functions, execvp replaces the calling process image with a new process image. This has the effect of running a new program with the process ID of the calling process. Note that a new process is not started; the new process image simply overlays the original process image. The execvp function is most commonly used to overlay a process image that has been created by a call to the fork function. A successful call to execvp does not have a return value because the new process image overlays the calling process image. However, a -1 is returned if the call to execvp is unsuccessful.
-
/* 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 < 3 ) { printf( "Insufficient arguments.\n" ); return -1; } execve( argv[1], argv + 1, envp); printf( "Is this line printed?\n" ); return 0; }
-
- runtime error - Floating point exception (core dumped)
#include <stdio.h> #include <stdlib.h> int main( void ) { int dividend = 50; int divisor = 0; int quotient; quotient = dividend / divisor; }
- to pause SIGSTOP (ctrl-s) or SIGTSTP (ctrl-z) to resume SIGCONT or fg kill -SIGSTOP [pid] kill -SIGCONT [pid]
- runtime error - Floating point exception (core dumped)
- renice {priority} pid Users can only change the nice value of processes which they own. User cannot start processes with nice values less than 20. User cannot lower the nice values of their processes after they've raised them. As usual root has full access to renice command.
- Operating systems open files to 1) establish a mapping between a pathname and the contents of the file so that the pathname doesn't have to be parsed on every file access, and 2) so the OS can allocate resources (particularly buffers) to accelerate access to the file. Those buffers can be released (and data flushed to disk) when the file is closed.
- Library calls are function calls, just to functions stored in dynamic libraries that are "included" with the system. (Note that what is included or not is an ambiguous concept in most Linux distributions.) System calls are invocations of the kernel (typically via a software interrupt). A system call's functionality is typically implemented by C functions; because the kernel must run in supervisor mode and regular programs run in user mode, regular function calls don't work between them. Indeed, userspace programs cannot even directly access the memory of the kernel for security reasons; that is why system calls must be vectored via software interrupts.
/* run-program-skips.c */ #include <unistd.h> #include <stdio.h> int main( int argc, char *argv[], char *envp[] ) { pid_t p; int i; char **nargv; int nargc; p = getpid(); printf("Hello again. I am process %d.\n", (int) p); if( argc < 2 ) { printf( "Insufficient arguments.\n" ); return -1; } nargv = argv + 1; nargc = argc/2; for (i=1; i < nargc; i++) { nargv[i] = nargv[(i*2)]; } nargv[nargc] = (char *) NULL; execvp( nargv[0], nargv ); printf( "Is this line printed?\n" ); return 0; }
/* sanitize-env.c */ /* Test this program by giving it /usr/bin/env as the program to execute. This program dumps its environment to standard out. */ #include <unistd.h> #include <stdio.h> #include <string.h> int main( int argc, char *argv[], char *envp[] ) { pid_t p; char *nenvp[2]; int nenvc = 0; int i; p = getpid(); printf("Hello again. I am process %d.\n", (int) p); if( argc < 2 ) { printf( "Insufficient arguments.\n" ); return -1; } for (i = 0; envp[i]; i++) { if (strncmp(envp[i],"PATH=",5)==0) { nenvp[nenvc] = envp[i]; nenvc++; break; } } for (i = 0; envp[i]; i++) { if (strncmp(envp[i],"TERM=",5)==0) { nenvp[nenvc] = envp[i]; nenvc++; break; } } nenvp[nenvc] = (char *) NULL; execve( argv[1], argv + 1,nenvp ); printf( "Is this line printed?\n" ); return 0; }
/* receive-term.c */ /* To test handler, run killall -s SIGTERM receive-term */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> static void ouch(int signal, siginfo_t *info, void *context) { printf("Ouch!\n"); exit(0); } int main(int argc, char *argv[]) { struct sigaction act; act.sa_sigaction = &ouch; act.sa_flags = SA_SIGINFO; sigaction(SIGTERM, &act, NULL); while (1) { printf("."); fflush(NULL); sleep(1); } return 0; /* main should never return */ }
- Note that your program cannot do anything after the SEGV signal is handled as the invalid pointer will just be accessed again. There does exist libraries for allowing SEGV to be handled (e.g., libsigsegv), but this is in general for userspace virtual memory management.
/* nocrash.c */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> static void ouch(int signal, siginfo_t *info, void *context) { printf("Oops, my mistake\n"); exit(0); } int main(int argc, char *argv[]) { struct sigaction act; char test, *bad; act.sa_sigaction = &ouch; act.sa_flags = SA_SIGINFO; sigaction(SIGSEGV, &act, NULL); bad = (char *) 12346; test = bad[0]; printf("test = %c, bad = %s\n", test, bad); return 0; }
/* run-program-dots.c */ #include <unistd.h> #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> static void reapchild(int signal, siginfo_t *info, void *context) { int status; printf("\n"); /* end the line of dots */ wait(&status); /* don't really have to wait since we're exiting */ exit(0); } int main( int argc, char *argv[], char *envp[] ) { pid_t p; struct sigaction act; act.sa_sigaction = &reapchild; act.sa_flags = SA_SIGINFO; sigaction(SIGCHLD, &act, NULL); if ((p = fork())) { /* parent */ while (1) { sleep(1); printf("."); fflush(NULL); } } else { /* child */ 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; } }
- NI is the static priority of the process - its "niceness" value. This value can be increased (= priority lowered) by an unprivileged user for their processes using the renice command or the nice system call. (The nice command is used to start a process with a given priority.) The root user can decrease the niceness value as well (increasing the process's priority). The PR column is the process's dynamic priority, a value used by the scheduler to determine which process should run next. The PR value is a function a process's static priority (the NI value) and its recent execution history (is it waiting for I/O, has it used a lot of CPU time, etc).