CS360 Midterm -- October 15, 2002

There are 6 questions. Answer all of them.


Question 1

Convert each of the following C procedures to assembler. Do not optimize.

Procedure 1

int a()
{
  int k;

  k = 5;
  return k+1;
}

Procedure 2

int tmp;
void s(int *i, int *j)
{
  tmp = *i;
  *i = *j;
  *j = tmp;
}

Procedure 3

int s(int **i)
{
  return *i[3];
}

Procedure 4

int s(int **i, int j, int k, int m)
{
  return *(i[3*j+5*k] + (m-3));
}


Question 2

Write the program lslt. This program lists all files in the current directory, with the exception of those beginning with a dot, in the following order:
  • Newer files should be printed before older files.
  • Files with the same age should be printed in alphabetical order.
  • Use file modification time to determine age.
In your answer, don't bother with the include files -- just assume that all of them listed on the cheat sheet are included.

Question 3

  • Part 1: Describe what the setuid bit is, and what it means when a file has the setuid bit set.

  • Part 2: Suppose you want to enable users to read only the first line of a file that you own. Describe how the setuid bit can be used to achieve this goal, and why you can't achieve it without the setuid bit (in other words, what would the alternatives be, and how do they fail). Additionally, if there are any pitfalls with this approach, state them and how to combat them.

Question 4

In jtar, how do you make sure that two files that are hard links to each other do not have both of their contents stored in the jtar file?

For Questions 5 & 6

Suppose:

Look at the following two programs:

Program 1

main()
{
  int i, j;

  i = 0;

  while (fread(&j, 4, 1, stdin) > 0) i++;
}

Program 2

main()
{
  int i, j;

  i = 0;

  while (read(0, &j, 4) > 0) i++;
}

Question 5

Convert Program 1 into assembly code. Obviously, do not optimize. Stdin is a global variable.

Question 6

Suppose standard input is x bytes, and suppose that x is a multiple of 4. Give exact expressions for how long Program 1 and Program 2 will take to run. You do not need to simplify these expressions. However, show how you get them.

Random Extra Credit

I drove through Charles Town, West Virginia two days ago.

What is the connection between that town and the eclectic rock group King Crimson?