UNIVERSITY OF ATHENS DEPARTMENT OF INFORMATICS & TELECOMMUNICATIONS
WWoorrkkiinngg wwiitthh tthhee UUnniixx OOSS
Maria Fragouli Dimitris Leventis Argyris Petropoulos Alex Delis
AN INTRODUCTION TO UNIX
CONTENTS 1. INTRODUCTION TO UNIX ...... 1 2. UNIX SHELLS...... 18 3. C PROGRAMMING ...... 25 4. UNIX TOOLS ...... 38 5. DEVELOPMENT TOOLS...... 51 6. C LIBRARIES...... 64 7. INTRODUCTION TO KERNEL ...... 80 8. PROCESSES (I) ...... 93 9. PROCESSES (II) ...... 122 10. I/O SUBSYSTEM ...... 143 11. INTERPROCESS COMMUNICATION ...... 153 12. PROCESS SCHEDULING...... 169 13. BUFFER CACHE ...... 180 14. UNIX ADMINISTRATION ...... 187 15. UNIX SECURITY ...... 200
TABLE OF FIGURES Figure 1. Data Structures for Processes...... 85 Figure 2. Process States and Transitions ...... 85 Figure 3. Sample Code Creating Doubly linked List ...... 85 Figure 4. Incorrect Linked List because of Context Switch ...... 86 Figure 5.Multiple Processes Sleeping on a Lock ...... 86 Figure 6. Process State ...... 99 Figure 7. Processes and Regions...... 100 Figure 8. Mapping Virtual Addresses ...... 101 Figure 9. Changing Mode from User ...... 101 Figure 10. Memory Map of u area in the Kernel ...... 101 Figure 11. Components of Context of a Process...... 102 Figure 12. Sample Interrupt Vector...... 102 Figure 13. Handling Interrupts...... 102 Figure 14. Example of Interrupts ...... 103 Figure 15. Algorithm for System Calls Invocations...... 103 Figure 16. Stack configuration for creat system call...... 103 Figure 17. Steps for a Context Switch...... 103 Figure 18. Pseudo-Code for Context Switch ...... 103 Figure 19. Process system calls...... 104 Figure 20. Algorithm for fork ...... 104 Figure 21. Fork Creating New Process Context...... 105 Figure 22. Example of Parent and Child Share File A ...... 105 Figure 23. Use of Pipe, Dup and Fork...... 106 Figure 24. Checking and Handling Signals ...... 107 Figure 25. Recognizing Signals ...... 107 Figure 26. Algorithm for Handling Signals ...... 108
Figure 27. Driver Entry Points...... 143 Figure 28. Block and Character Device Switch Tables...... 143 Figure 29. Opening a Device ...... 144 Figure 30. Closing a Device ...... 144 Figure 31. Reading Disk Data - block & raw interface...... 145 Figure 32. Data Sequence and Data Flow through Line Dicsipline...... 146 Figure 33. Removing characters from a Clist ...... 147 Figure 34. Placing characters on a Clist ...... 147 Figure 35. Writing Data to a Terminal ...... 148 Figure 36. Flooding Standard Output with Data...... 148 Figure 37. Algorithm for Reading a Terminal ...... 1 Figure 38. Contending for Terminal Input Data ...... 149 Figure 39. Raw Mode - Reading 5 character Bursts ...... 150 Figure 40. Polling a Terminal ...... 150 Figure 41. Loggin in...... 151 Figure 42. A Stream after Open...... 151 Figure 43. Pushing a Module onto a Stream...... 152 Figure 44. Windowing VT...... 152 Figure 45. Pseudo-code for Multiplexing Windows ...... 152 Figure 46. Process Scheduling...... 170 Figure 47. Range of Process Priorities ...... 171 Figure 48. Process Scheduling Example ...... 171 Figure 49. Tie breaker rule ...... 173 Figure 50. Fair Share Scheduler...... 173 Figure 51. Program Using Timer ...... 174 Figure 52. Alarm Call...... 174 Figure 53. Clock Handler ...... 175 Figure 54. Invoking Profil system call ...... 175 Figure 55. Output for Profil Program...... 175 Figure 56. Algorithm for Allocating Space from Maps ...... 176 Figure 57. Mapping Process Space ...... 177 Figure 58. Swapping a Process into Memory ...... 177 Figure 59. Adjusting Memory Map for Expansion Swap...... 177 Figure 60. Algorithm for Swapper ...... 178 Figure 61. Thrashing due to Swapping...... 179 Figure 62. Sequence of Swapping Operations...... 179 Figure 63. Free List of Buffers ...... 180 Figure 64. Buffers on the Hash Queues...... 181 Figure 65. Algorithm for Buffer Allocation ...... 181 Figure 66. First Scenario in Finding a Buffer: Buffer on Hash Queue...... 182 Figure 67. Second Scenario for Buffer Allocation...... 183 Figure 68. Third Scenario for Buffer Allocation...... 183 Figure 69. Forth Scenario for Buffer Allocation...... 184 Figure 70. Race for Free Buffer ...... 184 Figure 71. Fifth Scenario for Buffer Allocation...... 184 Figure 72. Race for a Locked Buffer...... 185 Figure 73. Reading a Disk Block Bach, "bread"...... 185 Figure 74. Algorithm for Block Read Ahead "breada"...... 186 Figure 75. Writing a Disk Block Bach, "bwrite"...... 186
Introduction to Unix
1. INTRODUCTION TO UNIX
Brief History The Unix system was developed by Brian Kernighan and Dennis Ritchie at AT&T. It is written in the C language and it entails many simple powerful concepts such as - simple homogeneous file system - devices & files treated the same - pipes - turn programs into building blocks - powerful shell command language - on-line manuals The system is designed as a program development environment, not a commercial data processing operation system
Earlier Versions of the system include: - Version 6 Unix -early 70s -very cheap for Universities - Version 7 Unix -dominated the research world - Berkeley Unix 4.1, 4.2, 4.3, 4.4 -fast file system, sockets, symbolic links - AT&T System V 4.2 -file locking for data bases - Networking & Remote mounted file systems
Unix Survival commands Login & Logout login user_name password enter with noecho exit logout
passwd to change password