NU EECS 340 - Unix Systems Programming In a Nutshell

Unformatted text preview:

CS 340 Unix Systems Programming In A Nutshell Dinda, Winter 2002 Page 1 of 8 Unix Systems Programming In a Nutshell Unix presents a huge set of interfaces to the systems programmer. However, much of this complexity can be tamed by understanding several fundamental abstractions and models, as well as by knowing where to look for more detail. It is important to note, however, that Unix does not always conform to these abstractions and models. Furthermore, although the Unix interfaces have the appearance of orthogonality, orthogonality is not always maintained. This document attempts to describe the fundamental concepts and to point out commonly used interfaces. Examples are geared to Linux. The Different Varieties Of Unix The evolution of Unix has resulted in a number of different contemporary implementations: BSD, System V, Linux, Mach, … Different implementations have slightly different semantics for their basic interfaces, and often provide additional interfaces. For example, Mach-based Unices have always offered kernel threads, while BSD-based Unices generally have not. If you look at code that is intended to be portable across different Unices, you’ll see many #ifdefs that specialize what is done to the specific Unix. Documentation Man pages are the basic on-line reference documentation. No matter how primitive your terminal is, you can always get man pages. To get a man page for, for example, the open call, type “man open”. If there is both a command and an interface by a given name, man will tell you the command. To have it print the interface you need to specify the appropriate “section” of the manual. For example, “man chmod” will tell you about the chmod command, while “man 2 chmod” will tell you about the chmod interface, on which, not surprisingly, the chmod command is based. The problem with man is that if you don’t know the name of what you’re looking for, you’re in trouble. If you don’t know that the interface to delete files is called “unlink”, you could try running “man –k delete” or “apropos delete” and then sifting through the many man pages, but that can be somewhat painful. Code can sometimes provide examples from which you can generalize. For example, if you look at the source of rm, you will soon discover that it uses unlink. Often, code examples, as well as other documentation can be found on the web. Of course, it helps if you use a powerful search engine such as www.google.com. Rick Stevens’s “Advanced Programming In the Unix Environment” is an excellent. Everything Tries To Be A File Unix tries very hard to make all sorts of objects look like files. This means two things: that these objects are named as files in the file system, and that they can be accessedCS 340 Unix Systems Programming In A Nutshell Dinda, Winter 2002 Page 2 of 8 using the same interface that is used to access files. For example, /dev/dsp is the name of the sound card. /dev/dsp can be opened and read (recording sound samples from line-in) and written (playing sound samples to line-out) in the same way as any ordinary file. Sometimes the file abstraction breaks down, however. For example, it is meaningless to lseek on /dev/dsp since the sound card can’t go forward or backward in time. Naming can also break down. For example, pipes are anonymous, being implicitly named by the processes at their endpoints. Another example is sockets, the abstraction for using the network. We cannot very well name a remote socket within the local file system. Files Try To Be Streams of Bytes Unix treats all files as streams of bytes. It has no clue about what they mean. For example, it is a convention that ASCII text files use the linefeed to denote the end of a line, but Unix does not treat the linefeed character in any special way. There are only a few exceptions to this rule, mostly involving terminals, a topic that won’t be discussed here. (“Terminals”, despite the archaic name, are important, however, because all keyboard sessions go through the terminal system. The xterms that you have probably used enumerable times are based on “pseudoterminals.”) Errors in Unix Generally, Unix interfaces have integer return values. A negative return value indicates an exceptional condition. The global integer variable errno provides more information on the error and the perror function can be used to print a meaningful description of the error. For example, int fd =open(“foo”,O_RDWR);if (fd<0) {perror(“Can’t open foo”);exit(-1);}attempts to open the file “foo” for both reading and writing. If the open fails, open will return a negative number and set errno to the appropriate error code. The perror will then print “Can’t open foo: [description of error code in errno]”. File I/O in Unix versus Standard I/O in C versus … This document will next discuss how to do I/O with plain files with the raw Unix interfaces. It important to note that the run-time libraries of most programming languages add a layer above this to simplify application-level programming or to increase performance by sophisticated buffering. As you will see, the raw Unix interface is quite primitive. Probably the most familiar run-time library is C’s Standard I/O library, which provides buffered I/O, simple parsing tools, and other tools. For example, consider how much work you might have to go through to write the following with the raw Unix interface we’ll describe below. double x,y;FILE *in=fopen(“input.txt”,”r”), *out=fopen(“output.txt”, “w+”);fscanf(in,”%lf %lf”, &x, &y) ;fprintf(out,”%lf”, x+y);CS 340 Unix Systems Programming In A Nutshell Dinda, Winter 2002 Page 3 of 8 fclose(in); fclose(out);C++ I/O streams provide even a higher level of abstraction, as well as static type checking to reduce the chance of errors. File I/O In Unix, the basic calls for file I/O are open(), read(), write(), lseek(), and close(). Before a file can be accessed, it must be opened. For example, int fd=open(“foo”,O_RDWR | O_CREAT);opens the file foo for reading and writing. If foo doesn’t exist, it is first created and then opened. Note that “foo” could name a plain file, a fifo, a device, or many other kinds of objects. The same open() call (and the subsequent calls we discuss in this section) can be used. Open() returns a file descriptor, which identifies the open


View Full Document

NU EECS 340 - Unix Systems Programming In a Nutshell

Download Unix Systems Programming In a Nutshell
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Unix Systems Programming In a Nutshell and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Unix Systems Programming In a Nutshell 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?