CSC414 “Introduction to UNIX/ Linux” Lecture 3
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Schedule Introduction to Unix/ Linux Kernel Structure and Device Drivers. System and Storage Structure. Processes and Inter- Process Communication. Shell Programming. User Management and Disk Quota. System Management. Network Management. Security. Advanced System Administration.
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Contents: Definition. Process types. Process Attributes. Creation of a Process Signals. Managing Processes ( commands).
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Definition: A process represents a running program. It’s the object through which a program’s use of memory, processor time, and I/O resources can be managed and monitored. Not every command starts a single process. Some commands initiate a series of processes, such as mozilla; others, like ls, are executed as a single command. You need to be aware of the four execution states : A runnable process is ready to execute whenever CPU time is available. It has acquired all the resources it needs and is just waiting for CPU time to process its data. As soon as the process makes a system call that cannot be immediately completed (such as a request to read part of a file), the kernel puts it to sleep. Sleeping processes are waiting for a specific event to occur. Interactive shells and system daemons spend most of their time sleeping, waiting for terminal input or network connections. Since a sleeping process is effectively blocked until its request has been satisfied, it will get no CPU time unless it receives a signal or a response to one of its I/O requests. Zombies are processes that have finished execution but have not yet had their status collected. If you see zombies hanging around, check their PPIDs with ps to find out where they’re coming from. Stopped processes are administratively forbidden to run. Processes are stopped on receipt of a STOP or TSTP signal and are restarted with CONT. Being stopped is similar to sleeping, but there’s no way for a process to get out of the stopped state other than having some other process wake it up (or kill it).
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Process types: Interactive processes: Initialized and controlled through a terminal session. There has to be someone connected to the system to start them. They are not started automatically as part of the system functions. Other applications can't be run as long as this process is running in the foreground Running a process in the background is only useful for programs that don't need user input (via the shell), used for long time execution jobs.
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Cont. Automatic processes Automatic or batch processes are not connected to a terminal. They wait to be executed on a FIFO basis. can be executed using one of two criteria: At a certain date and time: done using the at command At times when the total system load is low enough to accept extra jobs: done using the batch command. Daemons Daemons are server processes that run continuously Initialized at system startup and then wait in the background until their service is required
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Process Attributes: A process has a series of characteristics: The process ID or PID: a unique identification number used to refer to the process. The parent process ID or PPID: the number of the process (PID) that started this process. Nice number: the degree of friendliness of this process toward other processes (not to be confused with process priority, which is calculated based on this nice number and recent CPU usage of the process). The priority or importance of a job is defined by it's nice number. A program with a high nice; it is not an important job. The lower the nice number, the more important a job is. Terminal or TTY: terminal to which the process is connected. User name of the real and effective user (RUID and EUID): the owner of the process.
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Creation of a Process. Process creation (fork-and-exec mechanism ) A new process is created because an existing process makes an exact copy of itself. This child process has the same environment as its parent, only the process ID number is different. This procedure is called forking. After the forking process, the address space of the child process is overwritten with the new process data. This is done through an exec call to the system.
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Signals: Signals are process-level interrupt requests. About thirty different kinds are defined, and they’re used in a variety of ways: They can be sent among processes as a means of communication. They can be sent by the terminal driver to kill, interrupt, or suspend processes when keys such as <Control-C> and <Control-Z> are typed. They can be sent by an administrator (with kill) to achieve various ends. They can be sent by the kernel when a process commits an infraction such as division by zero. They can be sent by the kernel to notify a process of an “interesting” condition such as the death of a child process or the availability of data on an I/O channel.
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Cont. KILL is unblockable and terminates a process at the kernel level. A process can never actually receive this signal. • INT is sent by the terminal driver when you type <Control-C>. It’s a request to terminate the current operation. Simple programs should quit (if they catch the signal) or simply allow themselves to be killed, which is the default if the signal is not caught. Programs that have an interactive command line (such as a shell) should stop what they’re doing, clean up, and wait for user input again. • TERM is a request to terminate execution completely. It’s expected that the receiving process will clean up its state and exit. • HUP has two common interpretations. First, it’s understood as a reset request by many daemons. If a daemon is capable of rereading its configuration file and adjusting to changes without restarting, a HUP can generally be used to trigger this behavior. Second, HUP signals are sometimes generated by the terminal driver in an attempt to “clean up” (i.e., kill) the processes attached to a particular terminal. This behavior is largely a holdover from the days of wired terminals and modem connections, hence the name “hangup.” • QUIT is similar to TERM, except that it defaults to producing a core dump if not caught. A few programs cannibalize this signal and interpret it to mean something else.
Managing Processes ( commands) 2015 Qassim University, College of Computer, 9th level Managing Processes ( commands)
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Cont. ps Get the status of one or more processes. Eg: ps ax |more to see all processes including daemons Eg : ps –ef | grep <process> top The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel. Information are updated every 5 seconds. The first line of top contains the same information displayed by the uptime command:
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Cont. pstree Display the tree of running processes. relations between processes pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. All the criteria have to match. EX: pgrep -u root sshd will only list the processes called sshd AND owned by root.
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Cont. kill Ex: "kill 34" - Effect: Kill or stop the process with the process ID number 34. killall Kill processes by name. Can check for and restart processes. pid Find the process ID of a running program
Qassim University, College of Computer, 9th level 2015 Qassim University, College of Computer, 9th level Reading Chapter 5 from the textbook. Chapter 4 from “Introduction to Linux A Hands on Guide” (online http://www.tldp.org/LDP/intro-linux/html/intro-linux.html )