CSS 430
FAQ on Program 1: System Calls and Shell


Q1: With what editor should I use to program processes.cpp on a Linux machine?

A: Use emacs or pico

The emacs manual page is here:
http://www.delorie.com/gnu/docs/emacs/emacs_toc.html

Q2: How can I understand the details of each system call?

A: Use man

If you want to peek the manual page for a given command or system call, type as follows from the shell promplt:
   man fork
   man close
   man dup2
   man 2 wait
Since come commands and system calls have an identical name, you sometimes have to specify which section of manual you would like to see. For instance, if you type man wait, you will get the manual page for the wait command. Type man 2 wait to see the manual page for the waitsystem call. In general, the section 2 gives explanations about system calls.

Q3: How can I find a system call or a command which I would like to use?

A: Use apropos

If you want to see a list of commands and system calls that are associated with "process", type
   apropos process

Q4: How can I compile my processes.cpp

A: Use g++

To compile your processes.cpp into an executable module named "processes", type:
   g++ processes.cpp -o processes

Q5: I can't execute my program located in the current working directory! I can't use javac nor java!

A: Edit your ~/.bash_profile

   PATH=$PATH:/usr/java/j2sdk1.4.1_02/bin/:.
   export PATH

Q6: When I compile my processes.cpp, the g++ compiler complained "fork, execlp, ... not found".

A: You have to include some system header files

Check the usage of each system call by man. For instance, if you type "man fork", you will see the following manual page:
NAME
       fork - create a child process
 
SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>
 
       pid_t fork(void);
This means that you have to include "sys/type.h" and "unistd.h" in order to use the fork system call.

Q7: How can I debug my processes.cpp?

Insert "cout" as many as possible in your code

Use "cout" to see which portion of your program has been executed. To check which process has executed a given portion, you should use the getpid system call that prints out the executing process ID.
   cout << getpid( ) << ": executed this portion" << endl;
You might also want to try to use gdb, but I wonder if it is useful in our situation where we handle multiple processes.

Q8: How can I print out my source code to a printer?

A: Use a2ps command.

   a2ps -Puw1-320-p1 processes.cpp
   a2ps -Puw1-320-p1 Shell.java
If you want to print out a postscript file, you should use lpr:
   lpr -Puw1-320-p1 report.ps

Q9: How can we make a copy of object in Java?

A: Use clone method

For more detail, see http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html

Also, please be reminded:
In Java, the assignment statement for objects means that the left hand side object reference just receives the reference of the right hand side object, and thus both left and right hand side references point to the same object. There occurs no object duplication. This also applies to the array.

If you want to make a copy of array, use

   System.arraycopy( Object src, int src_position,
                     Object dst, int dst_position,
                     int length );
Some system-provided classes such as String has a copy constructor. Use them:
   String a;
   a = "abc";
   String b( a ); 

Q10: When I tried to download all the files of ThreadOS, I got the following errors. What's wrong?

goodall02% cp ~css430/ThreadOS/* ~/ThreadOS
cp: /rc44/d03/css430/ThreadOS/Cache.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Directory.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/FileDescriptor.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/FileSystem.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/FileTable.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Inode.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Kernel_org.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/QueueNode.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Scheduler.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Scheduler_mul.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Scheduler_pri.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Scheduler_sus.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/Shell.java: Permission denied
cp: /rc44/d03/css430/ThreadOS/SyncQueue.java: Permission denied

A: Some java files are prohibited to be downloaded.

Those files are answers. Ignore errors. Just use the file you have downloaded. Also, if cp ~css430/ThreadOS/* ~yourAccount/ThreadOS does not work out, try cp ~css430/ThreadOS ~yourAccount/ThreadOS

Q11: I coded and compiled a Java thread program successfully, but cannot execute it.

goodall03 javac Shell.java
goodall03 java Shell
Exception in thread "main" java.lang.NoSuchMethodError: main

A: Your thread program must be executed on ThreadOS.

mead% javac Shell.java
mead% java Boot
threadOS ver 1.0:
Type ? for help
threadOS: a new thread (thread=Thread[Thread-3,2,main] tid=0 pid=-1)
--> l Shell
l Shell
threadOS: a new thread (thread=Thread[Thread-6,2,main] tid=1 pid=0)
Shell[1]%

Q12: I tried to run a PingPong thread from the ThreadOS loader but got the following error.

-->l PingPong abc
l PingPong
java.lang.InstantiationException: PingPong
PingPong failed in loading
-->

A: If you don't give a correct number of arguments to your thread, the ThreadOS loader mumbles some complaints.

-->l PingPong abc 10000000
l PingPong
threadOS: a new thread (thread=Thread[Thread-6,2,main] tid=1 pid=0)
abc abc abc abc abc abc ......
...
-->

Q13: When I run my Shell, I got the following null pointer exception.

java.lang.NullPointerException
        at Kernel.interrupt(Kernel.java:141)
        at SysLib.cin(SysLib.java:35)
        at Shell.run(Shell.java:13)
        at java.lang.Thread.run(Thread.java:498)

A: In Java, a variable declaration is simply a reference declaration.

You have to instantiate an object by new. Otherwise, a variable has just a null pointer.

Q14: When I run some test programs on my Shell.java, I got the no such method exception message.

shell[0]% TestProg1 & TestProg2 &
java.lang.NoSuchMethodException: 
java.lang.NoSuchMethodException: 
java.lang.NoSuchMethodException: 
java.lang.NoSuchMethodException: 

A: Threads didn't get the correct number of arguments.

Your shell still has a bug that does not pass the correct number of arguments to a new thread like TestProg1 and TestProg2.

Q15: I don't know how to use SysLib.cin

A: Here is a big hit.

StringBuffer sb = new StringBuffer( );
SysLib.cin( sb );
String[] args = SysLib.stringToArgs( sb.toString( ) );

Q16: A conditional statement in my Java program does not work.

   if (args[0] == "exit")
This always returns false.

A: Use the String compareTo() or equal() function.

   if (args[0].equal( "exit" ) )