os course – lecture 1 amir averbuch and nezer j. zaidenberg
Post on 11-Jan-2016
225 Views
Preview:
TRANSCRIPT
OS course – OS course – lecture 1lecture 1
Amir Averbuch and Amir Averbuch and
Nezer J. ZaidenbergNezer J. Zaidenberg
Announcements 1Announcements 1
Lecturer : Prof. Amir AverbuchLecturer : Prof. Amir Averbuch
Tutoring assistant :Mr. Mr. Tomer MargalitTutoring assistant :Mr. Mr. Tomer Margalit
Ex. Grader :Ex. Grader :
The course form has changed since previous The course form has changed since previous years. It is recommended that you will years. It is recommended that you will attend lectures even if you taken this attend lectures even if you taken this course previouslycourse previously
Announcements 2Announcements 2
The same course is given in parallel by The same course is given in parallel by Nezer Nezer Zaidenberg.Zaidenberg.
The material is almost identical. Exercises The material is almost identical. Exercises are indentical.are indentical.
Nezer’s material is in Nezer’s material is in http://www.scipio.org/Courses/OS_Course.html
There is a forum administrated at There is a forum administrated at
http://oscourse.freeforums.org/
ScribeScribe
Following success last term…Following success last term…
1-2 Volunteers are needed as scribe1-2 Volunteers are needed as scribe
Scribe should attend every lecture and take notes.Scribe should attend every lecture and take notes.
Once a month the scribe will meet Nezer to check and review the Once a month the scribe will meet Nezer to check and review the class notes.class notes.
Notes will be published to the class in order to prepare for the test Notes will be published to the class in order to prepare for the test and homeworkand homework
Scribe duty well done will be considered as one homework exercise Scribe duty well done will be considered as one homework exercise submitted at 100 grade. (so scribe do gets something for effort) submitted at 100 grade. (so scribe do gets something for effort)
A second benefit is off course reviewing the material!A second benefit is off course reviewing the material!
People volunteering for scribe duty should contact Nezer via email People volunteering for scribe duty should contact Nezer via email nzaidenberg@mac.com. nzaidenberg@mac.com.
GradesGrades
50% exercises best 2 are 15% worst 2 are 10%50% exercises best 2 are 15% worst 2 are 10%
60% exam 60% exam
The exam will include multiple choice The exam will include multiple choice questions. questions.
You must pass both the exam and the You must pass both the exam and the homework to pass this course!homework to pass this course!
You will have to defend (orally) each exercise You will have to defend (orally) each exercise you submityou submit
ReferencesReferences The main books for this course is The main books for this course is
Operating Systems Concepts (6-8 Edition), Silberschatz, Galvin, Operating Systems Concepts (6-8 Edition), Silberschatz, Galvin, GagneGagne
Additional books (that will be noted on specific lectures)Additional books (that will be noted on specific lectures)
UNIX filesystems by S. PateUNIX filesystems by S. Pate
Beej Manual for network programming (web guide - networking)Beej Manual for network programming (web guide - networking)
Solaris internals by Jim MauroSolaris internals by Jim Mauro
Understand the Linux kernel 3Understand the Linux kernel 3rdrd edition (For kernel space) edition (For kernel space)
Advanced programming for the UNIX environment 3Advanced programming for the UNIX environment 3rdrd edition by edition by Richard W. Stevens (For user space)Richard W. Stevens (For user space)
Linux kernel development - Robert LoveLinux kernel development - Robert Love
Dranger guide for FFMPEG and SDL (web guide – multimedia)Dranger guide for FFMPEG and SDL (web guide – multimedia)
Course outlineCourse outline
The goal of this course is to gain theoretical The goal of this course is to gain theoretical and practical understanding of OSand practical understanding of OS
The course is very practical with several The course is very practical with several homework assignments and exam based on homework assignments and exam based on class and homeworkclass and homework
You will have to defend your workYou will have to defend your work
The course will focus on UNIX OS (in particular The course will focus on UNIX OS (in particular Linux), Windows and general principles of OS. Linux), Windows and general principles of OS.
The exercises will be mainly coding work or The exercises will be mainly coding work or reviewing kernel and low-level user code.reviewing kernel and low-level user code.
What this course is not What this course is not aboutabout
Coding (to the extent of coding standards, Coding (to the extent of coding standards, best coding practices or efficiency)best coding practices or efficiency)
MPP (beyond simple sync algorithms)MPP (beyond simple sync algorithms)
Network algorithms Network algorithms
EncryptionEncryption
Etc. Etc.
This code is on OS and OS related This code is on OS and OS related programming only programming only
What is OSWhat is OS
Once upon a time there was no operating systemsOnce upon a time there was no operating systems
Developers used to develop hardware and software, Developers used to develop hardware and software, together with everything needed to make the application together with everything needed to make the application runrun
As computers become more standard and tasks become As computers become more standard and tasks become common several tasks stood out as needed by all (or common several tasks stood out as needed by all (or practically all) software.practically all) software.
Those common tasks became “OS” and were supplied by Those common tasks became “OS” and were supplied by 33rdrd party vendors party vendors
Commercial software used to require specific OS. This Commercial software used to require specific OS. This became one of the first practices of “code reusability”became one of the first practices of “code reusability”
Common OS tasksCommon OS tasks
Memory managementMemory management
Hardware (driver) supportHardware (driver) support
Task schedulingTask scheduling
File managementFile management
User management (on multi-user systems)User management (on multi-user systems)
Software providers developed a single software for all Software providers developed a single software for all those components (and more) – the OSthose components (and more) – the OS
This software became the base for all further software This software became the base for all further software developmentdevelopment
What the OS includesWhat the OS includes
Core OS functionCore OS function Process managementProcess management User managementUser management File managementFile management Memory managementMemory management Hardware managementHardware management Network managementNetwork management
Additional functions are added and removed according to political and Additional functions are added and removed according to political and marketing scenariosmarketing scenarios Initial UNIX implementation had built in C compiler (sold separately with Initial UNIX implementation had built in C compiler (sold separately with
almost all modern OS)almost all modern OS) Most UNIX OS comes with wide range of servers such as ftp server, telnet Most UNIX OS comes with wide range of servers such as ftp server, telnet
server, daytime server, printing server and even HTTP server, J2EE server, daytime server, printing server and even HTTP server, J2EE platform, etc.platform, etc.
Many OS also come with GUI (windows, OSX aqua, X Windows) and other Many OS also come with GUI (windows, OSX aqua, X Windows) and other user tools (calculator, Text processor etc.)user tools (calculator, Text processor etc.)
Microsoft wished to add Internet Explorer, Windows media player etc. as Microsoft wished to add Internet Explorer, Windows media player etc. as part of the OS. However, the European union prevented the inclusion of part of the OS. However, the European union prevented the inclusion of media player in all windows sold in Europe… (anti trust reasons)media player in all windows sold in Europe… (anti trust reasons)
The course will deal with “CORE OS” functions only. These services are The course will deal with “CORE OS” functions only. These services are usually managed by the OS “Kernel”usually managed by the OS “Kernel”
We will focus on UNIX environment, Linux, Xubunto distributionWe will focus on UNIX environment, Linux, Xubunto distribution
What we will coverWhat we will cover
UNIX software developmentUNIX software development
ProcessProcess What is a processWhat is a process Memory managementMemory management Opening filesOpening files
Multi taskingMulti tasking Multi processMulti process Multi threadingMulti threading SyncSync
Memory managementMemory management
NetworkNetwork
Kernel developmentKernel development IO devices ( char device and block device )IO devices ( char device and block device ) File systemsFile systems
What is UNIXWhat is UNIX
UNIX is a general term for lots of thingsUNIX is a general term for lots of things
UNIX is the name of historical OS developed in 1970’s in Bell labsUNIX is the name of historical OS developed in 1970’s in Bell labs
It’s also a trademarkIt’s also a trademark
UNIX may refer to set of standards (POSIX) that defines OS interface UNIX may refer to set of standards (POSIX) that defines OS interface that many OS followthat many OS follow
UNIX is also general name for a family of many proprietary OS that UNIX is also general name for a family of many proprietary OS that were tested for standard completion. None of these OS is called UNIX were tested for standard completion. None of these OS is called UNIX but usually their name has an X that gives them away (AIX, OSX, HP-but usually their name has an X that gives them away (AIX, OSX, HP-UX, IRIX) with Solaris as the exceptionUX, IRIX) with Solaris as the exception
Commercial UNIX OS pay royalties to use the UNIX trademarkCommercial UNIX OS pay royalties to use the UNIX trademark
Since UNICES (plural for UNIX) share the same API and design principle Since UNICES (plural for UNIX) share the same API and design principle it is relatively easy (but not effortless) to port software from one UNIX it is relatively easy (but not effortless) to port software from one UNIX to the other.to the other.
UNIX-Like OS’sUNIX-Like OS’s
Open source OS (such as Linux, FreeBSD, OpenBSD) that Open source OS (such as Linux, FreeBSD, OpenBSD) that were not tested by standard committee (but tend to have were not tested by standard committee (but tend to have the same API) are often referred to as UNIX-like.the same API) are often referred to as UNIX-like.
Open Source OS’s also don’t pay royalties and cannot be Open Source OS’s also don’t pay royalties and cannot be officially named UNIX (due to trademark rules)officially named UNIX (due to trademark rules)
Open source UNIX-Like OS have become more and more Open source UNIX-Like OS have become more and more popular (especially Linux) in many fields more common then popular (especially Linux) in many fields more common then real “UNIX” boxes.real “UNIX” boxes.
Today, for most people, UNIX means an OS with specific set Today, for most people, UNIX means an OS with specific set of API and applications regardless of standards testing. (so of API and applications regardless of standards testing. (so many people will say Linux is as much of a UNIX as AIX, many people will say Linux is as much of a UNIX as AIX, Solaris and OSX are.) Solaris and OSX are.)
So it is common jargon for both UNIX and UNIX-like OS’s to So it is common jargon for both UNIX and UNIX-like OS’s to be considered UNIX. (at least for this course)be considered UNIX. (at least for this course)
UNIX OS internalsUNIX OS internals (IMPORTANT) (IMPORTANT)
UNIX OS and the UNIX API is built on top of two UNIX OS and the UNIX API is built on top of two basic principalsbasic principals Process Process FileFile
Almost all OS services are implemented as either Almost all OS services are implemented as either process or file. (for example the system logger is process or file. (for example the system logger is a “process” while a socket is a file.)a “process” while a socket is a file.)
This is one of the first implementations of the This is one of the first implementations of the interface design patterninterface design pattern
Throughout the course we will see how complex Throughout the course we will see how complex services are handled using these two basic services are handled using these two basic concepts.concepts.
UNIX history
UNIX (acr. for uniplexed information) was initially pun on multics (OS that is UNIX (acr. for uniplexed information) was initially pun on multics (OS that is virtually extinct that allowed for multi-user to run multiple tasks on multi virtually extinct that allowed for multi-user to run multiple tasks on multi process environment) and was the name of OS that was developed at bell process environment) and was the name of OS that was developed at bell labs for their PDP11 computers.labs for their PDP11 computers.
Original UNIX developers wanted to build a better multics.Original UNIX developers wanted to build a better multics.
The OS was initially distributed in source form so that anybody can modify The OS was initially distributed in source form so that anybody can modify and customize it.and customize it.
The source was later brunched into two main trunks SYSV and BSD code The source was later brunched into two main trunks SYSV and BSD code (with both brunches often borrowing code from each other)(with both brunches often borrowing code from each other)
Most modern Unices are usually based on either of those brunches.Most modern Unices are usually based on either of those brunches.
Commercial UNIX today mainly in high end server, workstation and desktop Commercial UNIX today mainly in high end server, workstation and desktop market. market.
Open source UNIX tends to rule several market segments (Linux and apache Open source UNIX tends to rule several market segments (Linux and apache rule the http serving and Linux practically runs on all Wireless routers.) and rule the http serving and Linux practically runs on all Wireless routers.) and is heavy favorite on several others (running database servers)is heavy favorite on several others (running database servers)
UNIX OS in the wild UNIX OS in the wild (partial list)(partial list)
SVR4 UnicesSVR4 Unices
Solaris and Open Solaris – Unix by SUN MicrosystemsSolaris and Open Solaris – Unix by SUN Microsystems
HP-UX – Unix by HPHP-UX – Unix by HP
Linux – The most popular open source OS (sponsored by Google, RedHat, IBM, Silicon Linux – The most popular open source OS (sponsored by Google, RedHat, IBM, Silicon graphics, Novell and more)graphics, Novell and more)
AIX – Unix by IBMAIX – Unix by IBM
BSD UnicesBSD Unices
OS X and Darwin iPhoneOS – Apple OS (OS X is the proprietary version and darwin is the OS X and Darwin iPhoneOS – Apple OS (OS X is the proprietary version and darwin is the open source version)open source version)
FreeBSD, NetBSD, OpenBSD – Open source BSD distributionsFreeBSD, NetBSD, OpenBSD – Open source BSD distributions
NOT UNIX systems (and how much they support POSIX)NOT UNIX systems (and how much they support POSIX)
Windows – not UNIX. But cygwin (open source) and microsoft POSIX subsystems each Windows – not UNIX. But cygwin (open source) and microsoft POSIX subsystems each provide compatibility layerprovide compatibility layer
Z/OS or MVS – IBM OS for mainframe – doesn’t follow UNIX at all but have a compatibility Z/OS or MVS – IBM OS for mainframe – doesn’t follow UNIX at all but have a compatibility layerlayer
UNIX API summaryUNIX API summary
All UNIX and UNIX like OS’s follow the same API All UNIX and UNIX like OS’s follow the same API
All OSs that follow the UNIX API share similar design All OSs that follow the UNIX API share similar design principles and conceptsprinciples and concepts
As long as we program using the UNIX API it is As long as we program using the UNIX API it is relatively easy to port from one OS to the other (and relatively easy to port from one OS to the other (and for a developer or administrator from one for a developer or administrator from one environment to move to another)environment to move to another)
Please note – some UNIX OS’s also have proprietary Please note – some UNIX OS’s also have proprietary “non POSIX” APIs to support more features (for “non POSIX” APIs to support more features (for example OSX COCOA) naturally such features cannot example OSX COCOA) naturally such features cannot be ported easily. be ported easily.
UNIX acceptanceUNIX acceptance Super computers – 88.6% of top 500 world super computers run Linux. 99% run Super computers – 88.6% of top 500 world super computers run Linux. 99% run
UNIXUNIX
Midrange servers (UNIX traditional role) – AIX is the most popular midrange Midrange servers (UNIX traditional role) – AIX is the most popular midrange servers in the financial industry while Solaris and HP-UX contend for the crown in servers in the financial industry while Solaris and HP-UX contend for the crown in Telco enterprisesTelco enterprises
Low end servers - About 70-75% of web servers (and most J2EE platforms) run on Low end servers - About 70-75% of web servers (and most J2EE platforms) run on Linux. Many network products (firewalls, switches, routers) are based on LinuxLinux. Many network products (firewalls, switches, routers) are based on Linux
High end Desktops and workstations – Over 65% of workstations and desktops High end Desktops and workstations – Over 65% of workstations and desktops costing above 1000 USD run OSXcosting above 1000 USD run OSX
Smart phones – both iPhoneOS (OSX) and Google Android (Linux) are unices Smart phones – both iPhoneOS (OSX) and Google Android (Linux) are unices
Databases – Sun was recently acquired by Oracle and Solaris will be the OS used Databases – Sun was recently acquired by Oracle and Solaris will be the OS used by Oracle’s Exadata. UNIX is also Tier-1 for most database vendorsby Oracle’s Exadata. UNIX is also Tier-1 for most database vendors
Embedded systems – VxWorks (arguably the most popular embedded OS) is POSIX Embedded systems – VxWorks (arguably the most popular embedded OS) is POSIX complaint. Linux dominates STB and IGD marketscomplaint. Linux dominates STB and IGD markets
Storage – most storage products run on top of UNIX (such as Linux or AIX)Storage – most storage products run on top of UNIX (such as Linux or AIX)
SAP environments – are typically managed by UNIX hostsSAP environments – are typically managed by UNIX hosts
Almost regardless of the Almost regardless of the industry or company you industry or company you will end up working for… will end up working for…
UNIX skills thought in UNIX skills thought in this course will be a vital this course will be a vital
tool in your toolboxtool in your toolbox
GNU source and the FSF
GNU – acr. For “GNU’s not UNIX” GNU – a public license granting permission to use,
modify and redistribute code provided it remains GNU
FSF – acr. For “free software foundation” FSF – distribute the source for most of the free UNIX
applications (The unix look and feel). Promote the GNU license
Linux kernel – the “main functionality” of the OS developed by volunteers (not the FSF, but the groups are connected) under GNU license
The many types of free
MIT license – allow you to do what ever you want with the code
BSD license – allow modification and redistribution (modification may not be free). You must keep credits to original author
GNU GPL – modifications (derived work) must be free GNU LGPL – modification must be free, but linking with GNU
source is not a modification (license used by GNU libraries) Other free licenses - check on FSF website Commercial Open source – you may view the source but not
use it commercially or you may edit the source, but your modification belong to specific company or modifications may not be redistributed. Are not considered “free” (such as Apple public license, Sun Public license, Netscape public license, MySQL)
Dual license – software available as GPL (if you modify you must open the source of the modification) same software is also available with closed source (you can modify and not open the source) for paying customers. This is the business model of many open source companies
History of LinuxHistory of Linux
In the early 1990’s there were commercial UNIX (SYS V) and In the early 1990’s there were commercial UNIX (SYS V) and free UNIX (BSD)free UNIX (BSD)
A T&T (copyright holders of SYS V) sued BSDi (BSD) for A T&T (copyright holders of SYS V) sued BSDi (BSD) for copyright infringement. BSD was crippled and many copyright infringement. BSD was crippled and many stopped using itstopped using it
During the legal vaccum a young Finnish student – Linus During the legal vaccum a young Finnish student – Linus Turvalds started implementing UNIX for intel 80386 from Turvalds started implementing UNIX for intel 80386 from scratch. The OS he developed was called Linuxscratch. The OS he developed was called Linux
Linus developed Linux “Just for fun”Linus developed Linux “Just for fun”
With BSD in legal struggle and Linux proving itself as With BSD in legal struggle and Linux proving itself as efficient and scalable alternative Linux started growing in efficient and scalable alternative Linux started growing in popularitypopularity
Linux was adopted by FSF for it’s GNU/Linux platformLinux was adopted by FSF for it’s GNU/Linux platform
Linux todayLinux today
Linux today is the most widely used UNIX OS Linux today is the most widely used UNIX OS (arguably the most widely used OS) found in (arguably the most widely used OS) found in cell phones, netbooks, embedded systems, cell phones, netbooks, embedded systems, notebooks, desktops, workstations, servers notebooks, desktops, workstations, servers and supercomputersand supercomputers
Linux runs on most architectures today (x86, Linux runs on most architectures today (x86, Itanium, ARM, Power, MIPS, s390 and a lot Itanium, ARM, Power, MIPS, s390 and a lot more)more)
Is distributed by commercial companies in Is distributed by commercial companies in what is known as a Linux distribution.what is known as a Linux distribution.
What is Linux What is Linux distributiondistribution
Linux distribution is a gathering of software Linux distribution is a gathering of software from several sources, compiling them and from several sources, compiling them and branding them by single companybranding them by single company
All Linux distribution include the Linux kernel All Linux distribution include the Linux kernel and most GNU sources (thereby Linux and most GNU sources (thereby Linux distribution….) distribution….)
Linux distributions by Red hat SuSe, Debian, Linux distributions by Red hat SuSe, Debian, Ubunto, slackware have been popularized.Ubunto, slackware have been popularized.
In this course we will focus on Xubunto 9.04 In this course we will focus on Xubunto 9.04
Differences between Differences between distributionsdistributions
Why and what is XubuntoWhy and what is Xubunto The popular Ubunto with X (i.e. not KDE or Gnome) The popular Ubunto with X (i.e. not KDE or Gnome)
desktopdesktop The lowest foot print and hardware requirements The lowest foot print and hardware requirements
of all modern Ubunto flavoursof all modern Ubunto flavours
The “founding fathers” of The “founding fathers” of Linux (partial list)Linux (partial list)
Linus Torvalds – Finnish – creator and Linus Torvalds – Finnish – creator and maintainer. About 2% of the current Linux maintainer. About 2% of the current Linux code (which is A LOT) is Linus’s code (which is A LOT) is Linus’s
Alan Cox – WalshAlan Cox – Walsh
Greg Kroah-Hartman - AmericanGreg Kroah-Hartman - American
Ingo Molnar – HungarianIngo Molnar – Hungarian
Robert Love – AmericanRobert Love – American
Shaped UNIX (partial Shaped UNIX (partial list)list)
Bill JoyBill Joy
Eric S. RaymondEric S. Raymond
Richard M. StallmanRichard M. Stallman
Ken ThompsonKen Thompson
Dennis RitchieDennis Ritchie
Marshall Kirk McKusickMarshall Kirk McKusick
Why do we need OS in the Why do we need OS in the first Placefirst Place
Well we don’t NEED OS. But we want to.Well we don’t NEED OS. But we want to.
Almost every piece of software require some set Almost every piece of software require some set of services such as managing files, allocating of services such as managing files, allocating memory and usually opening threads, syncing memory and usually opening threads, syncing etc.etc.
Initially people implemented it from scratch Initially people implemented it from scratch with any new software.with any new software.
OS is the basic set of services which allows us to OS is the basic set of services which allows us to write our software.write our software.
The strategy of rewriting the OS with a new The strategy of rewriting the OS with a new software is almost extinct since long ago.software is almost extinct since long ago.
OS code and User codeOS code and User code
OS code – usually invoked in “the kernel” is OS code – usually invoked in “the kernel” is the code that “makes the computer work”. the code that “makes the computer work”. Including drivers, memory management etc. Including drivers, memory management etc.
User code – most code we writeUser code – most code we write
In this code we will use the terms Userland In this code we will use the terms Userland and kernelspace to distinguish between and kernelspace to distinguish between them.them.
We will write both kinds of code in the We will write both kinds of code in the exercisesexercises
Where does the OS Where does the OS “ends”“ends”
The OS API is often called “system calls”The OS API is often called “system calls”
Often when we write code in C we call library Often when we write code in C we call library functions (such as printf, fopen, malloc) that functions (such as printf, fopen, malloc) that provide us with “OS” services (such as provide us with “OS” services (such as producing output, opening a file, allocating producing output, opening a file, allocating memory)memory)
Those are not system calls but C wrappers to Those are not system calls but C wrappers to system calls. (the C wrappers remain the system calls. (the C wrappers remain the same even on non-POSIX systems such as same even on non-POSIX systems such as windows)windows)
Help requestHelp request
In UNIX when we want brief help on something we can In UNIX when we want brief help on something we can type man (something) and get the help page. (try it. For type man (something) and get the help page. (try it. For example man man)example man man)
When the man refers to command line executable we will When the man refers to command line executable we will find the manual in section 1 of the manfind the manual in section 1 of the man
When the man refers to a system call we will find the When the man refers to a system call we will find the manual in section 2 of the manmanual in section 2 of the man
When the man refers to a library function we will find the When the man refers to a library function we will find the manual in manual 3 of the manmanual in manual 3 of the man
When a concept is found in two or more sections we can When a concept is found in two or more sections we can request the right manual by typing “man 1 write” or request the right manual by typing “man 1 write” or “man 2 write” (try it!)“man 2 write” (try it!)
Common conventionCommon convention
When discussing executable (such as ls) we When discussing executable (such as ls) we will use (1) after the executable name (for will use (1) after the executable name (for example ls(1))example ls(1))
Similarly when discussing system calls and Similarly when discussing system calls and library functions we will use 2 and 3 library functions we will use 2 and 3 respectfully. (open(2), printf(3))respectfully. (open(2), printf(3))
There may be some inconsistencies on There may be some inconsistencies on sections between UNIX flavours (functions sections between UNIX flavours (functions moving between section 2 and 3) but it’s rare. moving between section 2 and 3) but it’s rare.
Library code relation with Library code relation with OS codeOS code
Library functions invoke OS code to Library functions invoke OS code to complete their workcomplete their work
For example printf(3) (short for print For example printf(3) (short for print formatted) parses the input and format the formatted) parses the input and format the text. Then it calls for write(2).text. Then it calls for write(2).
Printf(3) is library function (and not OS Printf(3) is library function (and not OS function) while write(2) is a system call.function) while write(2) is a system call.
Some unix file related Some unix file related system calls you should system calls you should
already knowalready know Open(2)Open(2)
Close(2)Close(2)
Read(2)Read(2)
Write(2)Write(2)
To refresh your memory about these functions To refresh your memory about these functions type “man 2 open” to get the function C type “man 2 open” to get the function C info.info.
Example – man openExample – man open
OPEN(2) BSD System Calls Manual OPEN(2) BSD System Calls Manual OPEN(2)OPEN(2)
NAMENAME
open -- open or create a file for reading or writingopen -- open or create a file for reading or writing
SYNOPSISSYNOPSIS
#include <fcntl.h>#include <fcntl.h>
int open(const char *path, int oflag, ...);int open(const char *path, int oflag, ...);
DESCRIPTIONDESCRIPTION
The file name specified by path is opened for reading The file name specified by path is opened for reading and/or writing …and/or writing …
Note section 2 for system call
This is a man page from OSX which is BSD system
Open is the system call name
What the system call
does
Required header to use this function
C style function declaration (prototype)
Brief description
More from man More from man open(2)open(2)
… … (description snipped)…(description snipped)…
RETURN VALUESRETURN VALUES
If successful… (snipped)If successful… (snipped)
ERRORSERRORS
The named file is opened… (snipped)The named file is opened… (snipped)
SEE ALSOSEE ALSO
chmod(2), close(2), dup(2), getdtablesize(2), lseek(2), read(2), chmod(2), close(2), dup(2), getdtablesize(2), lseek(2), read(2), umask(2), write(2)umask(2), write(2)
HISTORYHISTORY
An open() function call appeared in Version 6 AT&T UNIXAn open() function call appeared in Version 6 AT&T UNIX
What the function returnsWhen
something interesting/ba
d happensRelated man pages
Standards etc.
The difference between system The difference between system and library functionand library function
OS functionLibrary function
Lowest level interfaceUsually calls OS function
Section 2 of man Section 3 of man
System dependent and may not exist on different systems (or used with different name or parameters)
Programming language dependent (but not system dependent)
Almost always bug freeAlmost always bugfree
Almost always efficientAlmost always efficient
Usually – kernel space codeUsually - user space code
fopen(3) fopen(3) implementationimplementation
FILE *fopen(char *name, char *mode)FILE *fopen(char *name, char *mode)
{{ int fd; int fd; FILE *fp; FILE *fp;
if (*mode != ‘r’ && *mode != ‘w’ && *mode != ‘a’)if (*mode != ‘r’ && *mode != ‘w’ && *mode != ‘a’) return NULL; return NULL; for (fp = _iob; fp < _iob + OPEN_MAX; fp++) for (fp = _iob; fp < _iob + OPEN_MAX; fp++) if ((fp->flag & (_READ | _WRITE)) == 0) if ((fp->flag & (_READ | _WRITE)) == 0) break; /* found free slot */ break; /* found free slot */ if (fp >= _iob + OPEN_MAX) /* no free slots */ if (fp >= _iob + OPEN_MAX) /* no free slots */ return NULL; return NULL;
if (*mode == ‘w’)if (*mode == ‘w’) fd = creat(name, PERMS); fd = creat(name, PERMS); else if (*mode == ‘a’) { else if (*mode == ‘a’) { if ((fd = open(name, O_WRONLY, 0)) == -1) if ((fd = open(name, O_WRONLY, 0)) == -1) fd = creat(name, PERMS); fd = creat(name, PERMS); lseek(fd, 0L, 2); lseek(fd, 0L, 2); } else } else fd = open(name, O_RDONLY, 0); fd = open(name, O_RDONLY, 0); if (fd == -1) /* couldn’t access name */ if (fd == -1) /* couldn’t access name */ return NULL; return NULL; fp->fd = fd; fp->fd = fd; fp->cnt = 0; fp->cnt = 0; fp->base = NULL; fp->base = NULL; fp->flag = (*mode == ‘r’) ? _READ : _WRITE; fp->flag = (*mode == ‘r’) ? _READ : _WRITE; return fp; return fp;
}}
System calls!
top related