Date: 2009.5.7
Content: Advanced Programming in UNIX environment Chapter1: Unix System Overview
1. Books used: Advanced Programming in UNIX environment
(Advanced Programming in the Unix environment: Second Edition)
2. instance code: http://www.apuebook.com/src.tar.gz
Although the Code is provided, we strongly recommend that you input the code in vim or VI.
3. Prerequisites
(1) C/C ++ language knowledge: Before programming, you should at least know what functions, variables, struct, pointers, for/while/do... while and so on.
(2) Linux knowledge: at least how do you edit C/C ++ code under Vim/VI, and then use the CC command to compile your C/C ++ code.
(3) determine your Unix Version: I chose fedora core10 and the kernel version is 2.6. The Code provided in the book runs in the 2.4 Environment. Therefore, users with poor Linux infrastructure can directly install rh9.0.
4. Study Notes
I. UNIX System
1. The system calls system cballs as the kernel interface.
2. shell is a special application that provides interfaces for other applications.
3. Linux is the kernel of the GNU operating system, so some people call it gun/Linux. A simpler term is Linux (that is why gun/Linux is also called Linux)
Ii. Logon
1. Every time you log on to the system, the system will find the password file, which is generally in/etc/passwd. The password file consists of the following seven parts:
SAR: X: 205: 105: Stephen Rago:/home/SAR:/bin/KSh
Login Name: SAR
Key: x
ID: 205
User Group ID: 105
Note: Stephen Rago
Parent directory:/home/SAR
Shell Program:/bin/KSh
Iii. Files And Directories
1. The file name cannot contain two characters: slash '/' and null character: NULL
2. instance 1.3. list all the files in a directory
**************************************** ********************
Use the function opendir,Readdir, AndClosedir operation directory
Opendir returns the Dir struct and then transmits the returned value to readdir.
Readdir reads the instances of each Dir, which is usually operated cyclically.
Closedir disabled
**************************************** ********************
# Include "apue. H "// custom header files, including many common system header files <br/> # include <dirent. h> </P> <p> int <br/> main (INT argc, char * argv []) <br/>{< br/> dir * DP; <br/> struct dirent * dirp; </P> <p> If (argc! = 2) <br/> err_quit ("Usage: ls directory_name"); </P> <p> If (dp = opendir (argv [1]) = NULL) <br/> err_sys ("can't open % s", argv [1]); <br/> while (dirp = readdir (DP ))! = NULL) <br/> printf ("% s/n", dirp-> d_name); </P> <p> closedir (DP ); <br/> exit (0); <br/>}< br/>
Iv. Input and Output
1. file descriptor: generally a small non-negative integer number
2. Standard input, output, and error
3. instance 1.4: Non-buffered input/output
**************************************** ********************
Constant: stdin_fileno,Stdout_filenoIn the header file<Unistd. h> Definition
If you execute./A. Out <file1> file2, the content of file1 will be input to file2.
**************************************** ********************
# Include "apue. H "</P> <p> # define buffsize 4096 </P> <p> int <br/> main (void) <br/>{< br/> int N; <br/> char Buf [buffsize]; </P> <p> while (n = read (stdin_fileno, Buf, buffsize)> 0) <br/> If (write (stdout_fileno, Buf, n )! = N) <br/> err_sys ("write error"); <br/> If (n <0) <br/> err_sys ("read error "); </P> <p> exit (0); <br/>}</P> <p>
4. instance 1.5: Copy standard input to standard output
**************************************** ********************
The fget () function reads a complete row. The READ function reads a specified number of bytes.
The GETC function reads a character at a time, and putc writes the character to the standard output. when reading the last byte of the input
, GETC returns the constant EOF, which is defined in stdio. h.
**************************************** ********************
# Include "apue. H "</P> <p> int <br/> main (void) <br/>{< br/> int C; </P> <p> while (C = GETC (stdin ))! = EOF) <br/> If (putc (C, stdout) = EOF) <br/> err_sys ("output error "); </P> <p> If (ferror (stdin) <br/> err_sys ("input error"); </P> <p> exit (0 ); <br/>}< br/>
V. programs and processes
1. instance 1.6: print the current process ID
**************************************** ********************
Use the getpid () function to obtain the ID of the current process.
**************************************** ********************
# Include "apue. H "</P> <p> int <br/> main (void) <br/>{< br/> printf ("Hello world from process ID % d/N", getpid (); <br/> exit (0 ); <br/>}< br/>
2. instance 1.7: Read commands from standard input and execute
**************************************** ****************************
Fget reads a row from the standard input. When the end character of the input file is the first character of the row, fgets returns NULL.
Fork creates a new process. The new process is a copy of the called process. We call the process as the parent process and the new process as the child process.
Execlp is used to execute commands read from standard input
The child process calls execlp to execute the new program file, and the parent process wants to wait for the child process to terminate, which requires the waitpid implementation to be called.
**************************************** ****************************
# Include "apue. H "<br/> # include <sys/Wait. h> </P> <p> int <br/> main (void) <br/>{< br/> char Buf [maxline];/* From apue. H */<br/> pid_t PID; <br/> int status; </P> <p> printf ("% "); /* print prompt (printf requires % to print %) */<br/> while (fgets (BUF, maxline, stdin )! = NULL) {<br/> If (BUF [strlen (BUF)-1] = "/N") <br/> Buf [strlen (BUF) -1] = 0;/* replace newline with null */</P> <p> If (pid = fork () <0) {<br/> err_sys ("fork error"); <br/>} else if (pid = 0) {/* child */<br/> execlp (BUF, buf, (char *) 0); <br/> err_ret ("couldn't execute: % s", Buf); <br/> exit (127 ); <br/>}</P> <p>/* parent */<br/> If (pid = waitpid (PID, & status, 0) <0) <br/> err_sys ("waitpid error"); <br/> printf ("%"); <br/>}< br/> exit (0 ); <br/>}< br/>
6. handle errors
1. The C language provides two methods for users to print error messages
#include <string.h>char *strerror(int errnum); |
Returns: pointer to message string |
#include <stdio.h>void perror(const char *msg); |