CSCE 451/851 Operating Systems Principles the UNIX Operating System Processes &
Total Page:16
File Type:pdf, Size:1020Kb
Page 1 The UNIX Operating System History ◆ The original UNIX 1 » An experimental operating system developed by Ken Thompson & Dennis Ritchie in the late ‘60s ◆ Main variants (“Standards”) »System V »POSIX ❖developed by AT&T, now ❖IEEE/ISO owned by UNIX International »FreeBSD » 4.4 BSD ❖ Open Software Foundation »Linux ◆ Commercial products » Ultrix, DEC UNIX — DEC » AIX — IBM » SunOS, Solaris — Sun » Xenix — Microsoft » HP/UX — Hewlett Packard » ... 2 Lecture 4 Steve Goddard [email protected] Processes & IPC The UNIX OperatingThe System http://www.cse.unl.edu/~goddard/Courses/CSCE451 CSCE 451/851 Operating Systems Principles CSCE 451/851 Steve Goddard Lecture 4 Page 2 CSCE 451/851 CSCE Steve Goddard The UNIX Operating System The UNIX Operating System Processes Processes ◆ A process is created by the fork() system call ◆ Alternatively, processes can be “created” by an exec() » creates a new address space that is a duplicate of the callers » replaces the memory image of the caller with a new program Parent address space “parent” “child” main (argc, argv) address space address space int childpid; main() prog() childpid = 1 { { { switch (childpid = fork()) { exec( prog) case 0: /* child */ } } child_func(); exit(0); default: /* parent */ ◆ This is how the shell executes commands parent_func(); while(wait((int *) 0) != childpid); »a fork() followed by an exec() exit(0); Child address space case -1: /* oops */ csh wait() error("fork:%s\n",sys_errlist[errno]); childpid = 0 fork() } } csh ls exec() 3 4 CSCE 451/851 CSCE 451/851 Steve Goddard Lecture 4 Page 3 Steve Goddard Lecture 4 Page 4 Processes in UNIX Processes in UNIX Example: How users logs in (Simple) Interprocess communication init ◆ wait() Like message passing except more general fork() init getty login csh ◆ Pipes — a shared, in-memory file exec() » a queue of 4K bytes csh ls » buffered, asynchronous message passing ❖ blocks reader when queue is empty ◆ There exists a “master process” in UNIX: init ❖ blocks writer when queue is full ◆ init forks a process for each terminal port ◆ init getty write Pipe read each copy execs which prints the login (fd,buf,len) (fd,buf,len) prompt and then reads the login and password ◆ getty then execs login which verifys the login ◆ login then execs csh which forks new processes for each command 5 6 CSCE 451/851 CSCE 451/851 Steve Goddard Lecture 4 Page 5 Steve Goddard Lecture 4 Page 6 Processes in UNIX Processes in UNIX (Simple) Interprocess communication Interprocess communication via Sockets socket() write() Pipe1 read() Server Client read() Pipe2 write() bind() main() { listen() int pipe1[2], pipe2[2]; socket() if (pipe(pipe1) == -1 || pipe(pipe2) == -1) error(... accept() switch (childpid = fork()) { case 0: /* child */ Blocks until connection close(pipe1[1]); /* write descriptor for pipe1 */ from client connect() close(pipe2[0]); /* read descriptor for pipe2 */ connection establishment client(pipe1[0],pipe2[1]); /* client program */ default : /* parent */ read() write() close(pipe1[0]); /* read descriptor for pipe1 */ data (request) close(pipe2[1]); /* write descriptor for pipe2 */ server(pipe2[0],pipe1[1]); /* server program */ write() data (reply) read() while (wait((int *) 0) != childpid); /* wait for child */ } } Server Client 7 8 CSCE 451/851 CSCE 451/851 Steve Goddard Lecture 4 Page 7 Steve Goddard Lecture 4 Page 8 The UNIX Operating System The UNIX File System File system Open file table examples ◆ ◆ UNIX maintains an open file table for each process I/O redirection — ls > foo which lists each file in use by the process » just change a pointer in the OFT » the OFT is copied when processes are forked system system per process OFT file structure table per process OFT file structure table system-wide 0: stdin 0: stdin per process OFT file structure table in-core i-node list 1: stdout /dev/tty03 1: stdout /dev/tty03 0: stdin list of i-nodes 2: stderr 2: stderr 1: stdout file block & a count of the : : /u3/kj/foo 2: stderr & # of file structure : : offset pointers table entries that fork();<edit OFT>;exec(ls) point to them sync’ed with 63: disk i-node list ◆ File sharing 9 10 CSCE 451/851 CSCE 451/851 Steve Goddard Lecture 4 Page 9 Steve Goddard Lecture 4 Page 10.