Operating Systems (coe518) Lab 2

COE 518 (Operating Systems)
Lab 2 (2014)
Page 1 of 4
Operating Systems (coe518) Lab 2
September 15, 2014
Duration 1 week
Learn how to use command line arguments in a C program.
Learn how to give the same program different names and make it behave differently
according to the name it is invoked by.
Run multiple processes simultaneously and coordinate them using the mkdir
The lab can be done on any computer (Windows, Linux, Mac OSX, etc.)
You also need a Unix shell and a C compiler. Linux and Macs already come with these.
For Windows (XP, Vista, 7 or 8), you will also have to install cygwin available here.
Part A: Using command line arguments and exit codes
1. Create a C Project called 518Lab2.
2. Modify the main function to have the signature
int main(int argc, char * argv[])
3. Modify the body of the main function so that it has the following behavior:
It prints a string in the form “greeting person”.
The greeting is Hello (by default) or Bye if the program is invoked with a
command that ends in the string bye
The person is UNKNOWN if there are no other command line arguments.
Otherwise person is the first argument.
If there is exactly one argument to the command main should return 0 (zero) as
the exit code. Otherwise, it should return 1 if there are no arguments and 2 if
there is more than one argument.
5. Once compiled, the executable is placed in the project's Debug directory and is called
518Lab2. Make links (or “aliases”) of that command using the following shell commands:
Version 1.0 (September 23, 2014) (BUG FIXED: submit command)
COE 518 (Operating Systems)
Lab 2 (2014)
Page 2 of 4
ln 518Lab2 hello
ln 518Lab2 bye
6. A typical interactive session is shown below where user input is this font and the output is
in italic.
bye Alice
Bye Alice
hello bob
Hello bob
bye Cathy Ng
Bye Cathy
hello dave smith && bye al
Hello dave
hello "dave smith" && bye al
Hello dave smith
Bye al
Part B: Multiprocessing and synchronization
Consider the following program.
#include <stdio.h>
#include <stdlib.h>
#define N_REPS 100
#define DEFAULT_SLOWDOWN 1000000
int main(int argc, char * argv[]) {
int i, j;
int slow_down = DEFAULT_SLOWDOWN;
if (argc == 1) {
fprintf(stderr, "Usage: %s string [delay]\n", argv[0]);
return 1;
if (argc >= 3) {
slow_down = atoi(argv[2]);
for (i = 0; i < N_REPS; i++) {
Version 1.0 (September 23, 2014) (BUG FIXED: submit command)
COE 518 (Operating Systems)
Lab 2 (2014)
Page 3 of 4
char * cp = argv[1];
while (*cp) {
printf("%c", *cp++);
for (j = 0; j < slow_down; j++)
Suppose the compiled executable is called foo. The invoking foo abcdef will
result in the output: abcdefabcdefabcdefabcdef etc.
(Note: even if you are not familiar with all the coding conventions, you should examine
the code sufficiently to convince yourself that it does do something like this.)
If you now run two “foo” processes concurrently with, for example, the command: foo
abcd & foo WXYZ, you will see output something like:
One process prints lower case letters; the other uppercase letters. But they are all
intermixed. We would like the lower and upper case letters not to be jumbled together.
For example, we would like the output to be something like:
To achieve this, only one process at a time should be able to perform the
“while(*cp)” loop.
Your goal in Part B is to modify the code to achieve this.
To achieve this, you have to identify a “critical section” that only one process at a time
should be allowed to execute.
Use while(system("mkdir junk") != 0) ; and system("rmdir
junk"); to achieve this.
And Finally: Submit your lab
To submit your lab do:
1. Create a directory called 518Lab2
2. Copy your source code for Part A into that directory using the filename partA.c for
the copied file.
Version 1.0 (September 23, 2014) (BUG FIXED: submit command)
COE 518 (Operating Systems)
Lab 2 (2014)
Page 4 of 4
3. Similarly, copy your source code for Part B into the same directory calling the file
4. Zip the entire directory into a single zip file with the command:
zip -r Lab2.zip 518Lab2
5. Submit the zip file with the command: submit coe518 lab2 Lab2.zip
That's all folks....
Copyright © 2014 Ken Clowes. This work is licensed under the Creative Commons Attribution 3.0 Unported
License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a
letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
Version 1.0 (September 23, 2014) (BUG FIXED: submit command)