Difference between revisions of "Operating Systems 2019F Lecture 4"

From Soma-notes
Jump to: navigation, search
(Created page with "==Video== Video from the lecture given on Sept. 11, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019f/lectures/comp3000-2019f-lec03-20190911.m4v is now available]. ==...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
==Video==
 
==Video==
  
Video from the lecture given on Sept. 11, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019f/lectures/comp3000-2019f-lec03-20190911.m4v is now available].
+
Video from the lecture given on Sept. 13, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019f/lectures/comp3000-2019f-lec04-20190913.m4v is now available].
  
 
==Topics==
 
==Topics==
  
* supervisor and user mode
 
* system calls
 
* process abstraction
 
 
==Notes==
 
 
<pre>
 
Lecture 4
 
---------
 
 
Topics
 
 
* files
 
* files
 
* open, read, write, close system calls
 
* open, read, write, close system calls
Line 22: Line 11:
 
* programmatic directory access
 
* programmatic directory access
  
 +
==Notes==
 +
 +
<pre>
 
What can you do with a file normally?
 
What can you do with a file normally?
 
* open it <--- why?
 
* open it <--- why?

Latest revision as of 17:54, 13 September 2019

Video

Video from the lecture given on Sept. 13, 2019 is now available.

Topics

  • files
  • open, read, write, close system calls
  • I/O redirection
  • command line arguments
  • programmatic directory access

Notes

What can you do with a file normally?
* open it <--- why?
* read from
* write to
* close it <--- why?

By having open and close, we tell the OS that
 - we're going to do multiple operations on this file, and
 - save the mapping of the filename to the underlying file data
 - do permission checks now, not when we access the file

When you open a file, you get a file descriptor (a small int)

Code

filetest.c

 1 #include <unistd.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 #include <stdio.h>
 6 
 7 int main(int argc, char *argv[], char *envp)
 8 {
 9         int fd;
10         char *msg = "Hello world!\n";
11         char *stdmsg = "Hello standard out!\n";
12 
13         write(1, stdmsg, 20);
14         
15         /* open a file */
16 
17         fd = open("test.txt", O_CREAT|O_APPEND|O_WRONLY,
18                   S_IRUSR|S_IWUSR);        
19 
20         printf("Got file descriptor %d\n", fd);
21         
22         /* write something to the file */
23         write(fd, msg, 13);
24 
25         /* close the file */
26         close(fd);
27 
28         return 0;
29 }

dirtest.c

 1 #include <sys/types.h>
 2 #include <dirent.h>
 3 #include <stdio.h>
 4 
 5 int main(int argc, char *argv[], char *envp[])
 6 {
 7         DIR *d;
 8         struct dirent *entry;
 9 
10         if (argc > 1) {
11                 d = opendir(argv[1]);
12         } else {
13                 d = opendir(".");
14         }
15         
16         entry = readdir(d);
17         while (entry) {
18                 printf("Entry: %s\n", entry->d_name);
19                 entry = readdir(d);                
20         }
21 
22         closedir(d);
23 
24         return 0;
25 }