What is OS?
What does OS do?
How does OS do that?
Software organization
applications: emacs, gcc, firefox, bash, mdb-lookup-cs3157
-----------------------------------------------------------------
library functions: printf(), strcpy(), malloc(), fopen(), fread()
-----------------------------------------------------------------
system calls: open(), read(), fork(), signal()
-----------------------------------------------------------------
OS kernel
-----------------------------------------------------------------
hardware: processor, memory, disk, video card, keyboard, printer
1945-1970:
1970: Ken Thompson & Dennis Ritchie invent UNIX and C
Since then, many UNIX variants come and go including:
Currently, four main competitors remain:
OS for personal computers
example:
jae@tbilisi:~/cs3157-pub/bin$ ls -al
total 84
drwxr-xr-x 2 jae phd 4096 2011-10-26 00:06 .
drwxr-xr-x 7 jae phd 4096 2011-10-25 23:35 ..
-rwxr-xr-x 1 jae phd 16740 2011-10-25 23:47 mdb-add
-rwsr-xr-x 1 jae phd 16755 2011-10-25 23:58 mdb-add-cs3157
-rw-r--r-- 1 jae phd 5480 2011-10-29 16:58 mdb-cs3157
-rwxr-xr-x 1 jae phd 20905 2011-10-25 23:47 mdb-lookup
-rwxr-xr-x 1 jae phd 83 2011-10-26 00:06 mdb-lookup-cs3157
jae@tbilisi:~/cs3157-pub/bin$
stdin
, stdout
, stderr
on descriptors 0, 1, 2file descriptors are used for sockets too
example:
int fd = open("myfile", O_RDONLY, 0);
if (fd == -1) {
// open error
}
int n;
char buf[BUF_SIZE];
while ((n = read(fd, buf, BUF_SIZE)) > 0)
if (write(1, buf, n) != n) {
// write error
}
if (n < 0) {
// read error
process ID: getpid()
example:
// NOTE: this is pseudo-code
......
if ((pid = fork()) < 0) { // "called once, returns twice"
die("fork err");
} else if (pid == 0) {
// comes here in child process
exec("ls");
die("exec err");
} else {
// comes here in parent process
......
getty
, xdm
, sshd
, etc.ps auxfww
you can generate a signal: kill
command or kill()
function
example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
static void sig_int(int signo)
{
printf("stop pressing ctrl-c!\n");
}
int main()
{
if (signal(SIGINT, &sig_int) == SIG_ERR) {
perror("signal() failed");
exit(1);
}
int i = 0;
for (;;) {
printf("%d\n", i++);
sleep(1);
}
}