System Programming Chapter 1
  • Simple IO model
  • #include <stdio.h>
    
    int main()
    {
    	int c;
    
    	while((c = getchar()) != EOF)
    		putchar(c);
    
    	return 0;
    }
    		
  • Process ID
  • #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
    	printf("Process id: %d\n", getpid());
    
    	return 0;
    }
    		
  • privileged mode, only the operating system is allowed to execute privileged instructions
  • unprivileged mode, user processes
  • Environment Variables
  • Get environment variable
  • #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char* home = getenv("HOME");
    	printf("Home direcotry is %s\n", home);
    
    	return 0;
    }
    		
  • System call with wrapper, the wapper in C library has same name as the function in kernel
  • System call without wrapper, such as gettid
  • #define _GUNU_SOURCE
    #include <unistd.h>
    #include <sys/syscall.h>
    #include <sys/types.h>
    #include <stdio.h>
    
    int main()
    {
    	printf("Thread id %ld\n", syscall(SYS_gettid));
    
    	return 0;
    }
    		
    The UNIX File Hierarchy
  • bin, essential binary executables including those shell commands
  • boot, Static files of the boot loader
  • dev, essential device files
  • etc, host conguration files and system administration programs
  • home, user home directories
  • lib, essential shared libraries and kernel modules
  • mnt, mount point for mounting a le system temporarily
  • opt, add-on application software packages, used for the storage of larger program packages
  • sbin, essential system binaries for system start-up
  • tmp, temporary files
  • usr, the top of a hierarchy in which non-essential binaries, libraries, and sources
  • var, variable files
  • proc, pseudo file system containing process information
  • root, system administrator home directory
  • Files and Filenames
  • regular files
  • device files (character or block)
  • FIFOs
  • sockets
  • symbolic links
  • - (regular le), d (directory), b (buered special le), c (character special le), l (symbolic link), p (pipe), or s (socket)
  • more Program
  • more file1 file2 ... filen
  • ls -l | more
  • more < file1
  • #include <stdio.h>
    #include <stdlib.h>
    #define SCREEN_ROWS 23
    #define LINELEN 512
    #define SPACEBAR 1
    #define RETURN 2
    #define QUIT 3
    #define INVALID 4
    
    
    /** do_more_of ( )
     * Given a FILE* argument fp, display up to a page of the
     * file fp, and then display a prompt and wait for user input.
     * If user input is SPACEBAR, display next page.
     * If user input is RETURN, display one more line.
     * If user input is QUIT, terminate program.
    */
    void do_more_of (FILE *filep) ;
    
    /** get_user_input ( )
     * Displays more's status and prompt and waits for user response,
     * Require that user press return key to receive input
     * Returns one of SPACEBAR, RETURN or QUIT on valid key presses
     * and INVALID for invalide key presses.
    */
    int get_user_input();
    
    int main(int argc, char *argv[])
    {
    	FILE *fp;
    	int i = 0;
    	if(argc == 1)
    		do_more_of(stdin); // no args, read from standard input
    	else
    		while(++i < argc)
    		{
    			fp = fopen(argv[i], "r");
    			if (NULL != fp)
    			{
    				do_more_of(fp);
    				fclose(fp);
    			}
    			else
    				printf("Skipping %s \n", argv[i]);
    		}
    
    	return 0;
    }
    
    void do_more_of (FILE * fp)
    {
    	char line[LINELEN]; // buffer to store line of input
    	int num_of_lines = SCREEN_ROWS; // # of lines left on screen
    	int getmore = 1; // boolean to signal when to stop
    	int reply; // input from user
    
    	while(getmore && fgets(line, LINELEN, fp)){
    		//fgets() return pointr to string read or NULL
    		if(num_of_lines == 0)
    		{
    			reply = get_user_input();
    			switch(reply)
    			{
    				case SPACEBAR:
    					num_of_lines = SCREEN_ROWS; // all full screen
    					break;
    				case RETURN:
    					num_of_lines++; // allow one more line
    					break;
    				case QUIT:
    					getmore = 0;
    					break;
    				default:
    					break;
    			}
    		}
    		if(fputs(line, stdout) == EOF)
    			exit(1);
    		num_of_lines--;
    	}
    }
    
    int get_user_input()
    {
    	int c;
    	// Control Sequence Introducer
    	printf("\033[7m more? \033[m"); // reverse on a VT100
    
    	while((c = getchar()) != EOF)
    		switch(c)
    		{
    			case 'q':
    				return QUIT;
    			case ' ':
    				return SPACEBAR;
    			case '\n':
    				return RETURN;
    			default:
    				return INVALID;
    		}
    
    	return INVALID;
    }
    		
    Device Special Files
  • Every I/O device is associated with a device special file
  • /dev/tty is the name of the terminal that the process is using
  • /dev/mem is a special le that is a character interface to memory
  • /dev/null is a special le that acts like a black hole. All data sent to it is discarded
  • tty # /dev/pts/3
    echo 'Hello World!' > /dev/pts/3
    		
    Notation
  • ls [<option>] ... [<directory_name>] ...
  • Shells
  • Bourne shell (sh), the C shell (csh), the Korn shell (ksh), the Bourne-again shell (bash), the Z shell (zsh), and the TC shell (tcsh)
  • echo $0 or echo $SHELL, check the current shell
  • Reference
  • System Programming at CNUY