Sistemas Operativos 3 MIEEC

Problem Set #1: Unix Shell Commands and C Programming


1. Objectives

2. Unix Shell Commands

1. Read the man pages of the following Unix shell commands: ls, pwd, mkdir, cd, mv, cat, more, ps, clear, and try them out.

2. Use some of the commands mentioned in the previous exercise to keep your work in this course organized. Start by creating a directory for the course. Under this directory, create a subdirectory for this set of problems. Put the solution to each of these problems under this subdirectory. The solution to each problem should be put in a file with a name that helps you locate it. E.g., pp2a.c for the solution of problem2a).

3. C Programming Problems

1. Write a program named fibonacci that computes and displays in the standard output the first 10 elements of the Fibonacci series.

Hint: The Fibonacci series is a series whose first 2 elements are 0 and 1, respectively, and the remainder are the sum of the two previous elements in the series.
IMP: Always use the -Wall, or even -Wextra, options, when compiling your programs. E.g.
gcc -Wextra fibonacci.c -o fibnacci
and fix your code until the compiler generates no warning. For more details about gcc's warning options check gcc's online documentation on warnings.
IMP. To run the executable compiled by the above command, assuming your current directory is the directory where you have done the compilation, just type at the shell's prompt:
./fibonacci

2. About the C operator sizeof.

  1. Write a program that displays the size of values of the following primitive types: char, short, int, long, long long.
  2. Write a program that displays the size of the following composite types:
    int a[10]; struct complex { double re, im; } z; char *hello = "Hello, World!";
    Try to explain the values.
  3. Assuming the declarations above, what should be the values returned by:
    sizeof(a[0]) sizeof(*hello)
    Check if your answer is correct by writing a program. Try to explain the values obtained by running your program.

3. About the use of pointers as function arguments.

  1. Write the function:
    void in_fibonacci(int *fib, int n)
    which fills the memory region whose address is passed in the first argument with the n first elements of the Fibonacci series.
  2. Write the main() function that should call in_fibonacci() and after its return should display those values on the standard output.
    IMP.- The memory region passed to in_fibonacci() should have enough space to store n. Two alternatives are: 1) static allocation, by declaring an array with at least n elements; 2) dynamic allocation, by using the function malloc() of the standard C library.

4. About command line arguments. Modify program of previous exercise so that it takes as command line argument the number of elements of the Fibonacci series to print, n.

  1. Use the function int atoi(const char *str) of the standard C library.
  2. Use instead the function long int strtoll(const char *nptr, char **endptr, int base). Compare the behavior of this version with the previous one, when the command line arguments are not integers.
Hint: Check the man pages of these functions

5. About pointers and strings.

  1. The function int strlen(char *str) of the standard C library, returns the length, i.e. the number of chars, excluding the end of string char, of the input string. Write int my_strlen(char *str), your version of this function.
  2. Write the function main() of a program that computes the size of the string "Hello, World!\n", by calling both strlen() and your own version, and displays the values returned by these functions on the stdout.
  3. Write yet another version of your program that computes the size of the string it receives as its command line argument, by calling both strlen() and your own version, and displays the values returned by these functions on the stdout.
    Hint: To pass as a single argument a string with spaces use quotes. E.g.:
    ./p5c "Hello, World!\n"

6. About pointer arithmetic. Using the array of strings:

char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  1. Write a program that displays all the months in a year, with one month per line. To traverse the array, you should use a pointer rather than the array index.
  2. Write function int no_months(char *mon1, char *mon2) to determine the number of months between the month specified in the first argument and the month specified in its second argument.
    Hint: Use the function strncasecmp() of the standard C library, to compare two strings ignoring their case.

7. Integers whose size is larger than one byte can be stored in memory in two ways: 1) the most significant byte (MSB) may be stored first, i.e. at the memory position with a lower address and then the remaining bytes in order, with the least significant byte (LSB) stored in the memory position with the highest address; or 2) the LSB may be stored first then the remaining in order, with the MSB in the last memory position. The former order is known as big endian whereas the latter is known as little endian.
The endianness is a characteristic of the instruction set architecture (ISA), although some processors support both.
Write a program with name endian that determines the endianness of the ISA of the processor in which it runs, by using pointers to chars to read the value of each bytes of a multi-byte integer.
Hint: To print the value of a byte in hexadecimal, you can use printf() by using the %#x conversion specifier.