CS360 Final -- December 10, 2002

Question 1

In the current directory, there exist the following files:

p1.c

wcat.c

#include < stdio.h >
#include < fcntl.h >

main(int argc, char **argv)
{
  int k, p[2];
  char *a1, *a2;
  a1 = NULL;
  a2 = NULL;

  open("f1", O_RDONLY);  
  open("f2", O_RDONLY);  
  open("fout-1", O_WRONLY 
       | O_CREAT | O_TRUNC, 0666);
  open("fout-2", O_WRONLY 
       | O_CREAT | O_TRUNC, 0666);

  pipe(p);

  for (k = 1; k < argc; k++) {
    if (strcmp(argv[k], "d1") == 0) {
      dup(atoi(argv[k+1]));
      k++;
    } else if (strcmp(argv[k], "d2") == 0) {
      dup2(atoi(argv[k+1]), atoi(argv[k+2]));
      k += 2;
    } else if (strcmp(argv[k], "f") == 0) {
      if (fork() == 0) {
        while(strcmp(argv[k], "e") != 0) k++;
      } 
    } else if (strcmp(argv[k], "e") == 0) {
      k = argc;
    } else if (strcmp(argv[k], "c") == 0) {
      close(atoi(argv[k+1]));
      k++;
    } else {
      a1 = argv[k];
      a2 = argv[k+1];
      k++;
    }
  }

  execlp("wcat", "wcat", a1, a2, NULL);
  printf("Issues\n");
}
#include < stdio.h >

main(int argc, char **argv)
{
  int fd1;
  int fd2;
  char c;
  int j;

  fd1 = atoi(argv[1]);
  fd2 = atoi(argv[2]);

  while (1) {
    j = read(fd1, &c, 1);
    if (j == 0) exit(0);
    if (j != 1) {
      fprintf(stderr, 
       "bad read from %d\n",  
        fd1);
      exit(1);
    }
    j = write(fd2, &c, 1);
    if (j != 1) {
      fprintf(stderr, 
       "bad write to %d\n", 
       fd2);
      exit(1);
    }
  }
}

Question 1, Continued

f1

f2

f3

Juice Em, Big Dog!!!
Oooooh-ah!
Jim Plank
Now, wcat.c and p1.c have been compiled into wcat and p1 respectively. Below are 17 commands typed in from the shell. For each command, circle the appropriate output for files fout-1, fout-2, fout-3, and the screen. Use the list of possible outputs below.

Possible Outputs

Commands

a.Empty file / No output
b.Juice Em, Big Dog!!!
c.Oooooh-ah!
d.Jim Plank
e.Issues
f.bad read from 0
g.bad read from 1
h.bad read from 2
i.bad read from 3
j.bad read from 4
k.bad read from 5
l.bad read from 6
m.bad read from 7
n.bad read from 8
o.bad read from 9
p.bad write to 0
q.bad write to 1
r.bad write to 2
s.bad write to 3
t.bad write to 4
u.bad write to 5
v.bad write to 6
w.bad write to 7
x.bad write to 8
y.bad write to 9
z.The program hangs
  • C1: wcat 0 1 < f3 > fout-3
  • C2: p1 < f3 > fout-3 3 5
  • C3: p1 < f3 > fout-3 4 1
  • C4: p1 < f3 > fout-3 0 2
  • C5: p1 < f3 > fout-3 3 8
  • C6: p1 < f3 > fout-3 4 7
  • C7: p1 < f3 > fout-3 7 8
  • C8: p1 < f3 > fout-3 d1 3 9 6
  • C9: p1 < f3 > fout-3 d2 1 3 3 5
  • C10: p1 < f3 > fout-3 d2 0 3 d2 4 0 3 5
  • C11: p1 < f3 > fout-3 d2 6 2 c 8 8 8
  • C12: p1 < f3 > fout-3 f 0 1 e 3 6
  • C13: p1 < f3 > fout-3 f c 8 7 5 e 4 8
  • C14: p1 < f3 > fout-3 c 3 f 3 6 e 0 5
  • C15: p1 < f3 > fout-3 c 3 d2 1 2 d2 5 1 3 5
  • C16: p1 < f3 > fout-3 c 3 d2 1 2 d2 5 1 4 1
  • C17: p1 < f3 > fout-3 c 2 d1 6 1 1

Assumptions

You should assume that fout-1 fout-2, and fout-3 are deleted before each of the above commands are executed. Also, the syntax for dup2() is:
int dup2(int orig, int dest)

Question 2

Suppose malloc() has been written so that:

Write a version of free() that works with the above version of malloc(). Your free() should:


Question 3

State the exact semantics of the following procedures: