Even though Linux is written primarily in C, Python allows us a simple way of making Posix system calls so we can concentrate on OS issues without worrying about arcane details of C pointers and strings. The python system calls page shows how some Posix system calls are done in Python. You can get information about system commands using the Linux man command. for example:
$ man forkwill tell you all about the fork system call. To see the Python documentation, you can invoke the pydoc command from the unix prompt:
$ pydoc os.forkOr use the help function inside an interactive Python session:
>>> import os >>> help(os.fork)
The first 4 programs constitute a lab warm-up exercise and should be completed in teams of 2 to 3 students. The final problem is homework to be done individually.
Write a program that comprises two processes. One process should write five thousand a's (lowercase "a") to the screen (with no new-line characters); the other process should write five thousand B's (uppercase "B"). Run the program and observe how the processes are scheduled. Note: if the a's and B's are not interleaved, you might have to increase the number that are written. Be sure to run your program several times before upping the counts, as scheduling may vary from one run to the next.
Here's a program, as1000.py that prints 1000 a's to the screen, this should help you get started. You can run the program by typing "python as1000.py" at the prompt. Note, this program makes use of sys.stdout to write it's output. This allows us to print a stream of a's one at a time without any spaces between.
Program 5: shell
For this program, you will implement a shell of your own. Your program will 1)write a prompt to the screen, 2)read an input line consisting of a string of characters optionally followed by an ampersand ("&"), 3)create a child process that executes the program whose name was entered, and 4)if the line did not end with an ampersand, wait for the completion of the child. These steps should be in a loop that continues executing until the user types "exit".
The code for the main loop is in the file shell-shell.py. You need to complete this code by providing the the crucial execute function. As it stands now, this function just prints out its arguments.
Test your shell using the programs: /bin/date, asbs2, and sleep10 a program(that you must write) that sleeps for 10 seconds and then prints a message.
Once your shell is working, see if you can enhance it in a couple ways. If the program cannot be found, print the message "Command not found" (like most Unix shells). If a program is run in the background (i.e. with an "&") print its process number when it is created (again, like typical shells). Another enhancement would be to retry the command with a "./" prepended if it is not found initially. This allows programs in the current directory to be run by just typing the name of the command.
Here is an example interaction:
[zelle@neon cs360]$ ./myshell.py john's shell% john's shell% fooey fooey command not found john's shell% /bin/date Wed Feb 11 19:42:16 CST 1998 john's shell% sleep10.py *** sleep 10 program terminating *** john's shell% john's shell% sleep10.py & [1] 1734 john's shell% sleep10.py & [2] 1735 john's shell% sleep10.py & [3] 1736 john's shell% *** sleep 10 program terminating *** *** sleep 10 program terminating *** *** sleep 10 program terminating *** john's shell% exit [zelle@neon cs360]$ exit
As you are working on this assignment, make sure to do a "ps" to see if you left any orphaned processes before you logout. You can kill them off with the kill command. Please make every effort to keep the system free of stray processes.
If you forgot to check before logging out, you can see all of the processes that you own (including those created in a previous session) by doing a "ps -x". If you're curious, "ps -aux" will show all of the process with information about ownership and status. Of course, you can't kill processes that you don't own.