Lab 2 (5 points)
CS550, Operating Systems
Introduction to UNIX and C
Name: _____________________________________________
This assignment is based upon the UNIX labs from the CS2351
course at Oklahoma State University, available at
cs.okstate.edu/newuser/index.html. The purpose of this lesson is
to learn to use the various UNIX commands, to learn about command
line compilers, editors, and to establish an account with the
Extreme Science and Engineering Discovery Environment (XSEDE). It
is recommended that you complete this assignment with the Littlefe
cluster, however, you may use Cygwin, your own copy of BCCD, or
another Linux variant to complete it.
Command Line Compilers
First, you will experiment with the C command line compiler called gcc.
Create a file called hello_loop.c with nano. Type the following code into the text editor:
#include <stdio.h>
int main(int argc, char ** argv)
{
int i;
for(i = 0; i < 100; i++)
printf("Hello %d\n", i)
}
}
Compile the code with the following command:
gcc hello_loop.c -o hello_loop.exe
What happened? ______________________________________________________
Fix any errors in the code and recompile.
When running the code, you will use a pipe to send the output of the program to another UNIX command called more. Pipes are a one way form of interprocess communication. Just like water, data flows through a pipe in one direction. Execute your code as follows:
./hello_loop.exe | more
What happened? ________________________________________________________
Use the spacebar and the "b" character to page up and page down through your output. The "q" character will get you out of more.
Many UNIX and Linux systems support less. Try piping your program to less. What happens? ________________________________________________________
Next, you will experiment with another program using the C command line compiler called gcc.
Create a file called test_args.c with nano. Type the following code into the text editor:
#include <stdio.h>
int main(int argc, char ** argv)
{
int i;
if(argc > 0)
{
for(i = 0; i < argc; i++)
printf("%s\n", argv[i])
}
return 0;
}
Compile the code with the following command:
gcc test_args.c -o test_args.exe
What happened? ______________________________________________________
Fix any errors in the code and recompile.
When running the code, you will pass parameters into the code via the command line. These parameters are called command line arguments. Note that you can also use command line arguments in Java. Execute your code as follows:
./test_args.exe arg1 arg2 arg3
What happened? ________________________________________________________
Now execute your code as follows:
./test_args.exe
What happened? ________________________________________________________
Complete the next program, and name it lab2.c:
//lab2.c
//<your name goes here>
//This program determines if a number is prime.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char ** argv)
{
//Declare and initialize a counter i and an input
//variable num.
int num, i = 2;
//Read in num using scanf
//Use a count controlled loop to check if num is prime.
//Hint: use the sqrt function from the math library to
//find the square root of num.
while(...)
{
//Replace the ... in the if statement with
the condition to
//determine if num is divisible by i
if(...)
{
//Print a message stating num is
not prime.
exit(0);
}
//increment i
}
//Print a message stating that num is prime.
return 0;
}
Hand in a printout of your code file (lab2.c), and a printout of
a sample output file you created.
To compile your program use the following command:
gcc lab2.c -lm -o lab2.exe
Introducing MPI
Finally, you will experiment with the Message Passing Interface
(MPI) enhanced C command line compiler called mpicc. Note
that the MPI library allows for C programs to be executed as
multiple independent processes.
Create a file called do_nothing.c with nano. Type the following code into the text editor:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char ** argv)
{
int number_of_processes;
int my_rank;
int mpi_error_code;
int i, j = 0;
mpi_error_code = MPI_Init(&argc, &argv);
mpi_error_code =
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
mpi_error_code =
MPI_Comm_size(MPI_COMM_WORLD, &number_of_processes);
for(i = 0; i < 100000000; i++)
j = i + 1;
printf("%d o%d: Hello, world!\n", my_rank,
number_of_processes);
mpi_error_code = MPI_Finalize();
return 0;
}
Note that all the code inside of MPI_Init through MPI_Finalize will run as number_of_processes processes. Compile the code with the following command:
mpicc
do_nothing.c -o do_nothing.exe
What happened? ______________________________________________________
Fix any errors in the code and recompile.
Remove the line containing MPI_Init and recompile.
What happened?
______________________________________________________
Replace the line containing MPI_Init, and remove the line
containing MPI_Finalize and recompile.
What happened? ______________________________________________________
Replace the missing line in your code, fix all errors and recompile. Open a second terminal (or PuTTY) window. If you are opening a second window to a remote system such as Littlefe, you will need to login again. Run the command called top. This command shows the current system statistics, including CPU usage by each process, and it updates on a regular basis. Execute your code as follows in the first terminal window:
mpirun -n 8 do_nothing.exe
On LittleFe use:
mpirun -n 8 -machinefile
machines-openmpi do_nothing.exe
Note that the -n 8 tells the operating system to create 8 processes.
What happened in the first terminal window? ________________________________________________________
What happened in the second terminal window? ________________________________________________________
Replace the -n 8 with -n 16 to tell the operating
system to create 16 processes. Hint: you can use the up and
down arrows at the command line to scroll through your history of
commands.
What happened in the first terminal window? ________________________________________________________
What happened in the second terminal window? ________________________________________________________
Now press the character "q" in the second terminal window and run the command:
mpirun -n 16 do_nothing.exe
On LittleFe use:
mpirun -n 16 -machinefile
machines-openmpi do_nothing.exe
While the program is running, run the following command in the second terminal window:
ps -fu username
Where username is your username. The ps command outputs a list of the current running processes.
What happened in the first terminal window? ________________________________________________________
What happened in the second terminal window? ________________________________________________________
Using Other UNIX Commands
Using nano write a short java program with an infinite loop. Compile and run this program at the command line. While this program is running, use ps to find its process id number. Then use the kill command to terminate the process. Submit a copy of your code and the output from ps with this homework.
The grep command allows you to find patterns in files. Run the command:
grep -n MPI do_nothing.c
What does the "-n" option do?__________________________________________
Attach the output of the grep command to this homework.
Investigate the sort,
diff,
and uniq
commands. The man
pages would also be a good place to look. Explain each in one or
two short sentences.
Run the command:
man sort
What does the sort
command
do?_____________________________________________________________________
__________________________________________________________________________________________________
Run the command:
man diff
What does the diff
command
do?_____________________________________________________________________
__________________________________________________________________________________________________
Run the command:
man uniq
What does the uniq
command
do?_____________________________________________________________________
__________________________________________________________________________________________________