Boiled down:
ps |
jobs |
kill |
fg |
bg |
real ID |
Group ID |
export |
nohup |
at |
cron |
crontab |
Life of a process
- Born when the program begins to run
- Dies when program execution ends
- A process is an instance of a program
- One program on disk, but many processes
- Shell is running, interprets the command
- Finds the command by checking (in order) $PATH
- Shell execs the command and goes to sleep
- When command finished, shell wakes up, issues prompt and waits for another command
- Process has a name and Process ID
- PID & CMD
- $$ is shell environment ariable that holds the PID
- issue the ps command
Creating Processes
- Execute a command (like ps)
- Almost all Linux commands are programs
- Shell scripts are programs too
- who | sort
- Creates two processes - one for who & one for sort
- All processes (except the first one) are created by some other process . it.s parent
- PPID - parent process id
Process Attributes - ps command in POSIX
- See table 7.1 page 191 of your book. Flags for ps are different for the
versions of Linux
- ps -f :full listing
- ps -e or -A : all processes including user and system
- -u usr
- -a : all
- -l : long listing
- -t term : processes runing on terminal term (e.g., /dev/console)
- -j : Dispalys GID
- Long listing items
- F - flags
- S - status code
- UID - user ID
- PID - process ID
- PPID - parent process ID
- C - CPU usage (scheduling)
- PRI - priority
- NI - "nice" value
- ADDR - memory address of process
- SZ - virtual memory usage
- WCHAN - memory address of event being waited for
- TTY - (/dev/) controlling terminal
- TIME - total CPU usage for this command
- CMD - command name
- Note the inset on pp 192-193
- Try the top command
Daemons, zombies, parents, children
Daemons
Job control from linuz org
What does child process know?
- When you call a method in C++, what information is available in the method? (by-value v. by-reference)
- Most environment variable are "inherited"
- realUID, realGID, effectiveUID, effectiveGID
- PWD - current working directory
- Open files - provided as "file descriptors"
(0 - stdin, 1 - stdout, 2 - stderr)
- HOME, PATH, other environment variables
- Child has a copy (by-value) of these variables
Real v. Effective IDs
- Real user/group ID never changes
- Linux always knows "who you are" and uses that information for access control (permissions)
- Setting the suid or sgid bit for an executable file allows anyone that runs the program to temporarily get the permissions of (become) the (group) owner of the file
- Allows a normal user to do things they otherwise couldn't, like access the password file
- Generally dangerous - hacking implications
- ls -l /usr/bin/locate /usr/bin/passwd
User variables
- user defined variables are not inherited unless you export them
- myAge=22; export myAge
- Once "exported", all child processes (recursively) inherit a copy of the variable's value
- Changing the value of a variable in a child process does not change the value in the parent
- experiment
- x=5
- bash
- echo $x
- exit
- x=5
- export x
- bash
- echo $x
- x=10 ; echo $x
- exit
- echo $x
Process Control
- To run a program in the background use &
- Process states
- O - running
- S - sleeping, off the CPU waiting for an event
- R - Runnable, waiting for its turn on the CPU
- T - suspended (^z)
- fg starts it in the "foreground"
- bg starts it in the "background"
- Z - a zombie process, it "died" and parent wasn't waiting for it
(This will keep a slot in the process table)
- kill is used to terminate processes
- kill PID
- sends a terminate signal SIGTERM
- kill -9 SIGKILL cannot be trapped
- kill%jobNum
- Normally a logoff will kill all process you have, even if they are run in the background
- A "hangup" signal (SIGHUP) is sent to all children
- The nohup command tell Linux to ignore this
- ctrl-z interrupts a running process
- bg %n places an interrupted process into the background. (n is the job number)
- jobs tells what processes are running and there status
- fg %n brings a job to the foreground
- nohup no hangup/ log out and let processes run
-bash-4.1$ cat sleeper.sh
echo sleeper is running
sleep 10
echo still running
sleep 10
echo can you hear me now
sleep 10
echo how about now
sleep 10
echo sleeper is ready to finish
sleep
echo bye
-bash-4.1$
- ./sleeper.sh &
- nohup ./sleeper.sh &
- fg %1
- bg %1
- jobs
- kill $!
- kill %2
- du - disk usage,
Managing time process is run
- at
- at TIME [enter]
- cmd ctrl-d
- TIME has many formats (15:08, 3:08pm, 3:08 + 1 day)
- cron - repeated scheduling. check /var/spool/cron/crontabs
- crontab
- 6 entries: minute(s), hour(s), day(s) of month, month(s), days of week,
commend to be executed
All work herein is subject to copyright. Original content to Dr. Deborah Whitfield, text content (Your unix/Linux) to Prentice Hall publishing.