UNIX Lecture 4
As it appeared when it was first sent via email.
Greetings Everyone,
Today I would like to start our discussion by talking about “processes”.
A Process is an “instance” of a program that is running. As you know, when you issue a command in UNIX, a program is started. The program that is running is called a “process”. So issuing a command initiates a process.
All processes are identified by a number called a process id. A “process id” is referred to as a “pid”, shorthand for process id. A shell in UNIX is a process, and when we issue a command the shell process begins another process. This is often referred to as “spawning” a process. So lets say you are at your UNIX machine and you issue a command. The shell in which you issued your command is a process. The command that you typed in gives birth to another process. The original, shell process, is called the “parent” process and the new process you created when you issued your command is called the “child” process.
When the shell process creates a child process, the parent process is in stand by mode and waits for the child to finish whatever it is doing. When the child process is finished, the parent “wakes up”.
If you want to display the pids of all of your processes, type in “ps” at the prompt (without the quotation marks).
If you give the ps command, also notice that the display will show the terminal number, the elapsed time of the process, the command, in addition to the pids.
If you want more information about the processes, type in ps-f, this will display the pid of the parent (ppid) in addition to all of the above information.
Here is what happens when a parent process creates a child process:
The parent creates a duplicate of itself using a system call named “fork”. The duplicate of the parent will inherit copies of everything from the original process, except the pid. The child process will have a unique pid. At this time, the child may continue to execute the same code, or it may replace the code with a different executable code and run that instead. The overlaying of codes et al is done using the system call “exec”.
When the child process is finished doing whatever it is doing, the parent process is notified. The parent process is very polite and accommodating and will wait for the child to complete its task before it takes other actions.
Some basic commands:
The “exec” command
This command is given with an argument (an argument is a command name). The exec overlays the current shell with the command and then executes it. This closes the current shell you are in and the command you issued is executed.
Commands for reading the contents of a file:
The “cat” command
This command is used to read the contents of small files. To use it simply type in the following at your prompt:
cat filename
If the file is very large the file will scroll very quickly across your screen and you will not be able to read the contents. In this case, the cat command is not the command to use.
So to read a large file use the “less” command
The syntax is:
less filename
When you are using the less command to read a large file, your screen will be filled up from top to bottom. To read the next part of the file, simply hit enter and the next portion of the file will fill in the screen again. In this way, you will be able to read the entire file, a screen full at a time.
If you are in the middle of a very long file and you wish to get out of it, simply press “control” and “d” (ie, Ctrl and d) to terminate the less command.
We will return to some more commands later.
I would like to talk a bit about INODES.
The information about your files, such as the type of file, the owner and group ids, the permissions and so on are stored in a structure known as “inode”.
Every inode contains the following information about your files:
- the length of the file in bytes
- the file permissions
- the ownership and group ids
- the location of blocks on the disk
- the relevant dates
- the type of file (is it a directory or is it a regular file)
- the number of links the file has
Every inode has a unique number called the i-number. Each directory is essentially a table containing the list of all filenames and the i-numbers associated with each of them.
The command “ls –i” will display the i-numbers of the files.
Another thing to note is that the inode stores three times, they are:
1) the time the file was last modified
2) the time the file was last used
3) the time the inode itself was modified.
That will be all for today. As I said before, I know that the lectures have been slow in coming and I thank you all for your patience.
Happy Holidays,
Vladimir Polyakov
PS And a special thanks to the Russian News Network website for helping me in this endeavor.
http://www.russiannewsnetwork.com