CS 111 Scribe Notes

Lecture 6: OS organization revisited

Presented: Wednesday, January 25th, 2016

By Chunnan Yao,Yihai Dingzhou, Jiaqi Huo, Jiahao Zhang

This lecture picks up the concept of Modularity from last lecture and further divides it into two categories: soft modularity and hard modularity. From that, the lecture defines "Process" that utilizes hard modularity to enforce their independent share of computing resources.

processes & files: these are all "fake"

process file has(major resources we need):

ALU ALU
registers registers * (*when running)
memory(RAM)   physical memory ** (**when running, and "looking at"(access)
I/O emulated/simulated
(system calls) (kernels implementation)
(pretend) on real

So far, we have looked at OS organization via virtualization. Our machine must have a CPU, with an ALU and registers, RAM/primary memory, and a bus of devices. We must also consider the dimension of time, in which we allow our machine to run.

kernel memory

Each process has a process descriptor inside the RAM, which contains a copy of registers, memory information, file descriptor entries(0-1023). The fd entries sector of the process descriptor that is associated to each process records the process's access of certain files.

figure 1 Figure 1

compress process memory into memory info
access file indirectly by a number of file descriptor
in Linux/Unix, 'int' is a handle for an open file
other OSes, 'point': struct file descriptor *


int main(void){
    return open("/dev/tty", O_RDWR);
}

The design is bad, there is no sense to return a file descriptor in main() as above.

what can go wrong here?

1.write to a pipe, it's full, all the readers are busy doing something else.


cat /etc/passwd | less
-- make buffer bigger (exhaust memory)
-- discard new data (cheap, no need to do with memory.) | unreliable
-- discard old data | lose data
-- suspend writer

2.write to pipe, no readers


cat /etc/passwd | :
-- suspend will have a problem, maybe suspend forever
-- kill the writer (SIGPIPE)
(optionally, ignore SIGPIPE, writes fail with errno==ESPIPE)

if (printf("hello")<0)
goto exit_from_write_loop

3.read from empty pipe
The pipe will hang waiting for the writer, that never writes (wait forever)
read reutrns 0(EOF)


int fd[2];
pipe(fd);
read(fd[0], &c, 1);

4.named pipe
cat will hang until write end of pipe is closed. Only close the write end of pipe will send eof to read end of pipe.


$mkfifo /tmp/pipe
$cat /tmp/pipe > out &
$echo helllo > /tmp/pipe

also need to be careful of deadlock, which both way are hang.

orthogonality question


  rm bigfile
  grep intersting < bigfile

it will display the output, since it run fd level instruction first, then file name level.

Signals

why?

error in your code
/O, FP overflow, invalid insn
impatient user or infinite loop SIGINT
impending power outage SIGPWR
p = waitpid(-1, &status, WNOHANG) in a SIGCHILD
user went away SIGHUP
while (fork()) continue; SIGKILL(cannot be caught or ignored)
#kill -KILL -29316 pid negative process means kill all of its descendants too.
runaway program
alarm(20) SIGALRM

Uses for signals

--break out of a loop
--kill a process, or suspend it kill -STOP (29) kill -CONT (29)
--timeouts
important, unusual, unexpected events

Receiving signals

sighandler_t signal(int,	sighandler
^                    ^		new handler
|                    |		void (*sighandler_t) (int)
old handler        signal
)                    #

signal call will cause bug like following
signal(29, f)


x = y + 1;
         <---- f(29) and change the value of x
x = x + 1;

gzip:
fd = open("foo", O_RDONLY);
                          <- signal(SIGINT, cleanup)   1.race here(may not create the foo.gz before signal comes)
fo = open("foo.gz", O_WRONLY)
while (compress(fd, fo))
	continue;

to fix this problem, we need to use a signal handler to make the gzip action atomic.