Note: All assignments are required. The trailing R in a conference title means the conference is required. The trailing O means that the conference is optional.
This week we learn what the terms "multi-user" and "multi-processing" really mean. Are we running processes in parallel on a single processor system? How can each user perceive that the system is totally dedicated to them when they are typing commands at the terminal?
First, how can a system run multiple programs on a single processor system? The answer is time-slicing of time-sharing. Today's CPUs operate at blinding speed. A CPU allocates slices of time for each process under its control. These slices of time are so small as to be unnoticeable to the user. Humans perceive time as chunks of milliseconds. CPUs operate in chunks of nanoseconds. The CPU can service many, many processes in a queue and each individual user perceives that the machine belongs to him or her.
Even if you are running Linux on your desktop machine, the operating system is maintaining concurrent processes. For instance if you are running an X windows application such as KDE you can launch multiple processes from within this graphical shell. The operating system is still maintaining itself and other concurrent processes under the hood of this graphical shell. The user never knows.
All of this is transparent to the user. The only time that the illusion breaks down is if too many processes are running at the same time. Then the user gets irate, not realizing that all along it has just been smoke and mirrors. Maybe in today's climate we should say, "smoke, and windows."
In UNIX/Linux, the programmer has some unique tools. For instance, he or she may launch a process (program) from within a program. The way this is done is with a system function known as fork(). This function creates a new process. We call this new process a child process and it runs concurrently (or the perception is that it runs concurrently) with the parent process. The function does not take an argument and returns an integer. The forking function is not a part of ANSI C, although all UNIX/Linux systems implement it. You can also shell out in Windows but I prefer the fork.
The operating system assigns a unique number to each process. The system kernel maintains information on all process in a system wide process table. If you would like information about existing process on your particular terminal, you may use the UNIX command, ps. The following is a list of some of the "interesting" process information provided by the command:
USER: the user's login name
PID: the Process Identification Number
%CPU: the percentage of CPU resources used in running the process
START: the start time of the process
TIME: execution time
COMMAND: the command and its switches that launched the process
and so on
The following C program illustrates some of the power a programmer can gain by manipulating the processes on a machine:
#include <stdio.h>
int main()
{
int pid;
pid = fork(); /*Take a fork in the road*/
if (pid == 0) /*The child is born*/
{
printf("My name is %d, and my mama's name is %d\n",getpid(), getppid());
_exit(0);/*The child dies*/
}/*The end of the child code, some would say epithet*/
if (pid < 0)
{
fprintf(stderr, "Even Robert Frost could not love this fork. It failed.\n"
exit(1);
}
printf ("My daughter's name is %d\n", pid);
return 0;
}
I did the following using my Cygwin bash emulator:
cdekle@CharlesII ~
$ gcc mother_and_child.c -o mac
cdekle@CharlesII ~
$ ./mac
My daughter's name is 11008
cdekle@CharlesII ~
$ My name is 11008, and my mama's name is 1
cdekle@CharlesII ~
$
gcc just calls up the free compiler. When your prgram forks a process the OS gives it its own resources. It is impossible to do this in Windows. You will need to master the much more complex topic of threads to do anything simular. If you take CMIS 415, you can learn much more about processes and threads some of the other truly powerful tools that UNUX offers the developer.
As an example of the power of the command line tool set that UNIX provides this week I would like to introduce the cut command. Sometimes you may need to extract fields of data or character from a file or the result of running one of the other commands. The form to use is :
cut -cchars file
As an example I did the following at my terminal (Cygwin):
cdekle@CharlesII ~
$ ls -l
total 23
-rwx------+ 1 cdekle None 99 Feb 19 11:54 PhoneLog.txt
-rw-r--r-- 1 cdekle None 11 Feb 12 16:06 date_input
-rwx------+ 1 cdekle None 11 Feb 12 16:13 date_input.txt
-rw-r--r-- 1 cdekle None 372 Feb 19 11:32 listfile
-rwxr-xr-x 1 cdekle None 10853 Feb 19 14:05 mac.exe
-rwx------+ 1 cdekle None 459 Feb 19 14:05 mother_and_child.c
-rw-r--r-- 1 cdekle None 9 Feb 12 15:48 result.txt
-rwx------ 1 cdekle None 87 Feb 12 19:47 script.sh
-rw-r--r-- 1 cdekle None 5 Feb 12 15:35 test
-rw-r--r-- 1 cdekle None 0 Feb 12 15:56 user.txt
cdekle@CharlesII ~
$
Now let's configure the command to display only the 33rd character using our knowledge of pipes:
cdekle@CharlesII ~
$ ls -l | cut -c33
F
F
F
F
F
F
F
F
F
F
cdekle@CharlesII ~
$
It is a simple illustration but with few keystrokes, you can do some very powerful operations on files. Consider that I have a simple little file called, PhoneLog.txt and it has the following information:
Cooper, Dan 703-555-1212
Rey, Fahe 703-555-8282
Darian, Bobby 703-555-777
Charles, Ray 703-555-3322
The name field is separated from the phone number field by a tab character. I want to print out just the names and not the telephone numbers. Furthermore, I also want the names sorted in normal alphabetical sequence. One thing that I could do is copy all of the data from the file into a spreadsheet and operate on it there or I could do the this with one command lin:
cdekle@CharlesII ~
$ cut -f1 PhoneLog.txt | sort
Charles, Ray
Cooper, Dan
Darian, Bobby
Rey, Fahe
cdekle@CharlesII ~
$
Voila!
I know that doing things this way seems counterintuitive to those of your raised on the GUI but I just love this. It is like solving a puzzle. Mastering the command line utilities is not easy. There is a pretty steep learning curve, but sometimes doing it from the CLI can be simplest way to accomplish a task. It is all about control. Okay, that is enough soapbox for one week. The rest is up to you. Have fun.
Read Chapters 8 and 9 of the textbook . Read Chapter 7, Multiprogramming: Separating Processes to Separate Function, at http://www.faqs.org/docs/artu/ - The Art of UNIX Programming by Eric S. Raymond ISBN 0-13-142901-1. This will be our discussion topic for the week. As always feel free to provide other sources for this topic.
Read the chapter from Raymond. I know that he can be a bit pompous but it will give us a good starting point for our discussion. Processes are one of the more powerful ideas to come out of the UNIX world, and a former VP's claims not withstanding, Interprocess Communication made the Internet possible.
Go to Conference C4.1R - Processes and post your comments under my topic. You should read and respond to several of the other comments that interest you. As always, I will be lurking about and will jump in from time-to-time with my own observations. You didn't know that this was a philosophy class, did you?
All of the materials presented and discussed in the conferences are fair game for the midterm and the final. If there are other related items that are not part of the normal curriculum then we can discuss them in the cafe.
The Cybercafe conference is an open forum for the free exchange of ideas. You may use it anytime. Use this conference to make appointments for other members of the class to meet you in the cafe.