CSCE 451/851 Operating Systems Principles

The Processes & IPC

Steve Goddard [email protected]

http://www.cse.unl.edu/~goddard/Courses/CSCE451

1 Steve Goddard CSCE 451/851 History System The Operating UNIX ◆ ◆ ◆ Commercial products Commercial variants (“Standards”) Main The original UNIX » HP/UX — HP/UX Hewlett Packard » Sun — Solaris SunOS, » Ultrix, DEC UNIX — DEC » BSD 4.4 » »System V Thompson by Ken developed system operating experimental An » & Dennis Ritchie in the late& Dennis ‘60s ❖ ❖ Open Software Open Foundation owned by UNIX International now developed by AT&T, Lecture 4 Lecture »POSIX ... » Xenix — Microsoft » AIX — IBM » »Linux»FreeBSD ❖ IEEE/ISO 2

CSCE 451/851 Page 2 Page Steve Goddard Lecture 4 Page 1 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

◆ 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();;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