Lab Notes & FAQ's

Lab Notes & FAQ's

Lab notes & FAQ's

LABS

You may use only C, C++ or Java, but Java may not be feasible for specific assignments, so you need to know what your language can do. In real life, you will OFTEN find that no one knows in advance how many inputs you will get, what the file names will be or what sizes things will be (arrays, etc). In fact, you may not even know the max/min values of the numbers you may have to work with (e.g.; like 5*E**45 or 6.43).

Lab 1:

In the character mode version of the program, you must read ALL lines of the ENTIRE file. The problem is to read ONE character at a time, then write one character at a time. You are not to use any strings or vectors to holding them for writing.

In the line-at-a-time version, you read an entire line (into a string or vector) and write the entire line at once, then read another whole line. Then repeat for the ENTIRE file, same as the other version. So you need to do a "fixed-length read" equal to the length of the line (not counting the line-end, unless your I/O method requires it).

In other words, the 2 versions use DIFFERENT I/O functions to read and write.

Don't start your timings until you start reading the file. Your file must include spaces as part of the text, so it "looks" like words. You may use any tools available to you to create the input file. You should use another tool to verify that the output file is identical to the input. On *x, you can use "cmp", "diff" or any other tool. On Windows, you can use "comp" in a command prompt window to compare the 2 files.

FAQ’s

  1. Is there any special format or template that you want us to follow for h/w?
    A> Use the computer to print your answers. No need to re-state the question. Be sure to number the answers to match the questions. The number of the homework is the same as the Chapter number. (In other words, be SURE to put the chapter number on the page.) Otherwise, no special formatting requirements.
  1. How do I document my programs internally?
    A> The comments should explain WHY you are doing something.
    the following comment will cost you points
    I=I+1; // add one to I
    There should be a BRIEF description of each function's purpose, the allowable inputs and expected outputs. You should document both what kinds of INPUT errors are handled and which are not. By INPUT errors, I mean: input that is outside the specifications for the problem (i.e., user errors, data format).
  1. How do I start a compile in Linux?
    A> The basic command is either
    gcc myprog.c
    or
    g++ myprog.cpp
    depending on whether it is a C or C++ program. You may also need some library options. The options start with a -l (that's a "dash L") note that the L is lowercase (it's case sensitive, just like everything in Linux). Your executable file will be named "a.out". You run it just as in Windows, by typing its name at a prompt (yo have to ype the WHOLE name – the ".out" is NOT an extension as in windows, just part of the name.) Also, unless you modify your profile (see Misc documents), you need to precede the name with a "./" to tell the shell to look in your current directory to find it. You can rename the output or make the compiler give it another name if you wish.
  1. How do I determine the number of threads or processes for the matrix multiply problem?
    A> It is determined at runtime by the dimensions of the input matrices. That means you will have one thread per multiply or 1 thread per column/row pair.
  1. How do we supply input to our programs? And where should the output go
    A> All programs (except lab 1) must accept the name of a file for input as the first parameter. The output should be written to standard I/O (so it can be routed to a file by the TA as needed).
  1. What does the input look like for the matrix lab?
    A> The input file must look like something this:
    n11 n12 n13 n14
    n21 n22 n23 n24
    n31 n32 n33 n34
    ***************
    m11 m12 m13 m14
    m21 m22 m23 m24
    m31 m32 m33 m34
    where the **’s indicate the end of M1 and end-of-file indicates the end of M2. The "end of file" indicator tells you when there is no input left.

The newline character (\n) represents the end of a row, so you must detect it. Warning: this character is considered as “whitespace”, so the “cin” function ignores it. (i.e.; you cannot use > to get the data.)

  1. Why can’t I find JOIN on Solaris/Linux?
    A> Here is the information that was sent to us by Sun. According to them, there is no "join" function in Solaris or Linux. See the man page for fork:

man -s 2 fork
or
man fork1
or, on Linux,
man fork

According to the BU support specialist, you would use _exit to close one of the processes that was forked. Here is a sun doc that might be of more use (or, maybe not) =>

So when some one tells you they are running a UNIX system, take it with a grain of salt (a big one!).

  1. Why can’t I create a 2-D array in C++?
    A> C++ does not have arrays with variable dimensions determined at runtime. Your matrices can be stored as lists. There ARE other (nastier) ways of doing it, but they involve code like: (*x)[i][j] to reference the data. See: if you want to learn more. (PS. This is a REALLY old paper, but written by the inventor of C.)

So the following code will NOT work:
int * mOne;
int * mTwo;
mOne = new int[M1row][M2col];
mTwo = new int[M1row][M2col];
But the following code WILL create a 2D array.
#include <stdio.h>
#include <stdlib.h>
// Pay close attention to the backslashes at the end of all
// but the last line in the macro-definition CHK_PTR.
//The backslashes tell the compiler that there is more to follow.
//------start of macro definition ------
#define CHK_PTR(p) {\
if((p) == (void *)0) {\
perror("Cannot allocate memory");\
exit(-1);// really terminate the parent\
}\
}
// ------end of macro definition
int main(int argc, char *argv[])
{
int **m, r=2, c=2, i, j;
m = (int **)malloc(r * sizeof(int *));
CHK_PTR(m);
for(i=0; i<r; ++i) // changed the c to an r
{
m[i] = (int *)malloc(c * sizeof(int));
CHK_PTR(m[i]);
}
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
m[i][j] = i+j;
printf("m[%d][%d] = %d\n", i, j, m[i][j]);
}
for(i=0; i<c; ++i)
free(m[i]);
free(m);
return 0;
}

Because of the difficulty of using arrays in C++, I would strongly recommend you use linked lists.

  1. When do I use FORK and when do I use Pthread_create?
    A> When you want to create processes, you use fork. When you want to create threads, you use Pthread_create. A single program may actually use BOTH! It all depends on what your problem is and how you intend to solve it.
  1. What is in a “Design Document”?
    A> Your design document should describe your METHODOLOGY. That is, what encapsulations you have (if any) which threads/processes will do what work, what options are available to the user and what command-line switches and/or data must be supplied and in what order (an actual example of a command-line would be best).
    If using a "development environment" such as Visual Studio, you must make a list of the libraries you included and excluded, and the options you used to compile and run the program. In Visual Studio, there are several windows that display this info and you can "select" all the info in the box, then copy and paste it into a simple file or an MS Word doc to send with the code.
  1. What do I actually have to submit?
    A> Your first required submission is a Design Document, as previously described. This is followed some time later, by the source code. You do NOT submit test data. We will make up data to test your program. (That is why the input must come from a file defined at runtime.)
  1. What is the STL? And how does it pertain to the matrix problem?
    A> STL is the Standard Template Library. There are MANY kinds of containers in the STL, some of which have dynamic bounds. Since you already know how to build a linked list (or you should), and given the time available to get your labs done, you'd be better off just implementing a simple linked list for each row and linking the rows together. You ARE permitted to use the STL.
    Consider this algorithm:
    Done=0;
    For (J=1;J<=2;J++)
    {Allocate a ptr to a list (list J) (this will be the headers of all the rows
    of a matrix).
    {Do(until done)
    Read a line.
    At the current position in list 1, allocate a ptr to ANOTHER list
    (this will be row # 'n')
    If the line contains the '***' done=1.
    Else
    Parse the numbers into elements of the list for row 'n'
    Repeat.
    }
    }
  1. How do my FORKed programs know what to work on?
    A> Remember what fork does! It creates a CLONE of your program, including ALL DATA! Therefore, if you set a pair of variables (containing the row and column numbers to be handled) BEFORE you fork, the child can use those variables to determine what to work on. Or you can use shared storage. For results, however, you will DEFINITELY have to use shared storage. The name of the termination function is "_exit", not "exit". Note the underscore!
  1. Where can I get Pthreads for my PC?
    A> this is a site to get the pthreads header (Pthreads.h) and dll (Pthreads.dll)
  1. How do I install Pthreads once I have the files?
    A> Install the files in a folder.
    Open Control Panel
    Open the System folder
    Click on the Environment button.
    Locate the "path" variable.
    Click "edit" and add the path to your library.
    Should be able to run without modifying the Windows directory contents.
  1. How do I tell the compiler about Pthreads?
    A>
  2. For g++ on Bingsuns orLinux, use: g++ -lpthread myprog.cpp
    The default output filename in Linux and Bingsuns (from gcc) will be called “a.out”. You can change this using the “-o myprog” option (the example here names it “myprog”).
  3. For MS Visual Studio, go to Project Settings and click on the tabs or windows labeled below in bold-face and make the specified changes. (Whether using .net or VC++ 6, you still need to update the settings to tell the compiler and linker where things are. The specific locations for this info should only be slightly different.)

1) Be sure you specified a multithreaded, Win32Console, non-DLL project. You can specify the debug version or not.

2) C/C++/Preprocessor

  1. Additional include libraries: Insert the path to the pthread .H files

3) Link

  1. Category: Input

a) Object library modules: insert pthread.lib

b) Additional library path: Insert the path to pthread.lib

Ignore libraries should be: libc.lib,msvcrt.lib,msvctrd.lib,libcd.lib,libcmtd.lib,libcimtd.lib

  1. How do I use Pthreads on Linux?
    A> This may help
  1. Are there any good books on Pthreads?
    A> YES!
  2. "Multithreaded Programming with Pthreads", Lewis & Berg, Prentice Hall
  3. "Programming with Pthreads", Butenhof
  1. Why can’t I use condition variables instead of semaphores?
    A>Condition variables are not semaphores, even if the underlying implementation uses them. Remember that you aren't supposed to know how the function calls do their job. So pthread_mutex_t is, by definition, a NEW datatype. It is a non-counting semaphore. Semaphores protect the critical section, condition variables only indicate it is now OK to TEST the semaphore. Condition variables are only valid to use inside a monitor.
  1. Why won't my program run on Linux after it compiled OK?
    A> The current path (see it via "echo $PATH") does not include your current directory. To run your program with out changing the path, try this:
    /home/joeuser/mypgm where "mypgm" is your compiled program, using -o mypgm
    Our system administrator may have already added your personal directory to your path as a default. It can also be done using Linux commands.
  1. How can I get a PID from inside Java?
    A> Since the kernel keeps the PID, there has to be an API to the kernel. There isn't one built into the Java language, nor is there a function in any Java library. You will have to use a C++ module linked to your Java module. (We discussed this in class.) See this website for the code:
  1. How do I handle strings in C++?
    A> in c++ a string is just a character array terminated by '\0'?
    Try declaring it that way. Something like....
    char str[80];
    cin > str; //enter a string from key board
    cout < str //display string
    Note:
    In "namespace std", there is a new datatype called a "string". It is not just a character-array. It is a library-managed object. It is not terminated by a NULL. All assignments to a "string" are actually implemented by methods of the string object, which guarantee proper operations.
    "using namespace std" is a signal to the compiler that you want to use the "Standard Template Library", which includes a large number of new objects to simplify programming (e.g.; it includes stacks, queues, etc.).
    When using namespace std, you do NOT put the ".h" inside your includes. So you should have this:
    #include <string>
    #include <iostream>
    And so forth.
  1. What is time-sharing?
    A> See:

  1. I am trying to use _ftime() to get the real time in milliseconds. Is it supported?
    A> Yes, it is supported, but you may also need:
    #include <sys/types.h>
    which doesn't show up in the examples, but IS listed at the top of the help file. Your struct "timebuffer" needs to have specific fields defined in it.
    Also, I think it'll work if you take out the "_" in the name “_ftime”. I know the following works:
    #include <stdio.h>
    #include <sys/timeb.h>
    #include <cstdlib>
    #include <iostream>
    #include <time.h>
    #include <sys/types.h>
    using namespace std;
    int main()
    {
    struct timeb timebuffer;
    char *timeline;
    ftime(&timebuffer);
    timeline = ctime(&(timebuffer.time));
    printf( "The time is %.19s.%hu %s", timeline,
    timebuffer.millitm,&timeline[20] );
    }
  1. How do I get CPU time in Java?
    Java does not have a built-in CPU-time interface. See:

  1. What causes “bus error” and “segmentation fault”?
    A> Both bus errors and segmentation errors are addressing errors, such as trying to address memory outside of your legal address space. A segmentation error is detected by the kernel, while a bus error is detected by the hardware. Incorrect setting of pointers causes it.
  1. How do I create a thread in Java? You create a thread by deriving from Thread. A method of a Thread derivative runs on a thread only if called directly or indirectly from that thread's run() method. Objects cannot be threads; only methods can be threads. See the notes and: .
  1. What kinds of threads run under Java?
    The "Green threads" implementation option (in Solaris) causes all Java threads to be run under a single kernel thread, which means no thread can directly issue a blocking call to the O/S. The JVM must trap them all, so other threads can run. The Java "native thread" implementation option runs each Java thread as an independent kernel thread, just like in C and C++.
  1. Do I have to do my labs on campus?
    No. For Windows, download the Developer's Studio from MSDNAA (free). It contains C++ and Java libraries. You can use the standard V6 studio or the .net studio. For Linux, use Putty, Cygwin or SSH to get to the TJW Debian Linux machines.
  1. How do we work with shared memory?
    See my document on using shared memory..

Also see the man pages for shmget and some decent examples of how to program using shared memory at this website .

Although the examples don't show it you need to convert the output of shmat() to the appropriate pointer type.

  1. How do I handle syntax differences across platforms (Windows vs. Linux). You should have only ONE set of source-code files used for allsystems. IF there are any differences in source code, you can handle them easily by the following method:

#ifdef WIN32

... Windows version of code

#else

... Linux version of code

#endif

... Common code

because MS Visual Studio automatically pre-defines WIN32 for you.

This is the usual method and avoids multiple source files.

  1. Q. How do I get to Java on the TJW machines? Java is available on all your Linux machines. It is located in /usr/bin/java. You can change your logon profile to add it to your path IF and ONLY IF, you know how. Otherwise, you will have to always use the explicit path.