UNIX Processes
Prof. Brian D. Davison
Computer Science & Engineering, Lehigh University
Announcements
- SQ1: Low was 1, high 10, median 5, mean 5.4.
- Office hours: Mon/Thu 11-noon
- Why are man and info better than other alternatives?
- Lessons from first lab?
Man is OS-specific; both are installation-specific. Google will usually
get you a reference for a different OS or at least a different version of the OS.
Announcements
- SQ1: Low was 1, high 10, median 5, mean 5.4.
- Office hours: Mon/Thu 11-noon
- Why are man and info better than other alternatives?
- Lessons from first lab:
- There are many windowing environments (visual interfaces)
- Paths are important for figuring out what to run
- Many Sun keyboards have keys in positions different than what you expect
- Yes, printing was fixed later Monday morning.
- Programming Assignment #1 is online, due Monday.
- Review UNIX commands for Friday's quiz.
I recycled your printouts. Consider reading man pages for unix commands!
Processes
- A process is the memory, open files, and other system resources needed while a program runs. The kernel creates a process when you run a program.
- For example, try:
io:~% more /etc/termcap (e.g., view a large file with more)
Then, press CTRL-Z to suspend the process.
[1]+ Stopped more /etc/termcap
io:~% ps
PID TTY TIME CMD
2556 pts/7 0:00 more
2540 pts/7 0:00 bash
io:~%
- ps reports information about processes (owner, cpu/memory usage, etc.)
- The top command can also be used to see what programs are running, their memory and cpu usage, etc.
SysV ps has different options than BSD-derived ps. Use whereis to find the other.
Managing Processes
- Normally, running a program in a shell will stop the shell while the other program runs.
- To allow both to run, start the program and put it into the background.
- Pressing CTRL-Z in a running program will suspend that process.
- The jobs will show you what processes are running or stopped from this shell:
io:~% jobs
[1]+ Stopped more /etc/termcap
- The fg and bg commands can move the currently stopped program into the foreground or background (and change status of other jobs currently stopped or running).
Can also give arguments with %jobid to the fg and bg and kill commands.
Killing Processes
- Sometimes, you get a program that you can't stop with CTRL-C.
- To kill your process that won't otherwise stop, type
kill <processid>
or if needed,
kill -9 <processid>
- The killall command can be used to kill a process using its name.
Actually, killall will kill all processes with that name (that are owned by the current user).
C Programming
- There are many more useful UNIX utilities that we will discuss in the
future, but let's consider building one.
- We saw previously that cat can display the contents of a file.
- Let's write our own version in class. Since cat reads from files, we'll need to use the open(2), read(2), and close(2) system calls.
- Note that the man system also has entries for system calls and standard library functions as well as for programs.
Note the man page sections for the system calls.
Use man -s to specify which section of the manual pages to search.