System Interface n Interface that provides services from

  • Slides: 7
Download presentation
System Interface n Interface that provides services from the OS (Higher than BIOS) n

System Interface n Interface that provides services from the OS (Higher than BIOS) n n Memory Scheduler File/Storage System Inter-process Communication and Network, etc program libc Operating System BIOS

File System n A popular but also complex subsystem n n File descriptors (pseudo

File System n A popular but also complex subsystem n n File descriptors (pseudo files, like stdin and stdout) Low level IO File Management Examples file pointer /�� usr�� bin�� home�� file EOF

A little about File Attributes n File attributes (srwxrwxrwx) n n read, write and

A little about File Attributes n File attributes (srwxrwxrwx) n n read, write and execute an octal digit, rwx, desribes the permission to a set of users Three sets of differents users, owner, group and others Some unix commands: umask, chmod, etc to change the attributes

File Accesses n Read and Write n n n getc() and putc(); getchar() and

File Accesses n Read and Write n n n getc() and putc(); getchar() and putchar() int read(int fd, char *buf, int n); int write(int fd, char *buf, int n) #include “syscalls. h” /* Should be stdio. h */ int main () { char buf[1024]; int n; while ((n = read(0, buf, 1024)) > 0) write ( 1, buf, n); return 0; } #include “syscalls. h” /* Should be stdio. h */ int getchar () { char c; return (read(0, &c, 1) == 1 ) ? c : EOF; }

File Accesses -- continued n Open, creat, close and unlink n n n int

File Accesses -- continued n Open, creat, close and unlink n n n int open(char *name, int flags, int perms); Persm can be O_RONLY, O_RDWR, O_WRONLY and others int creat(char *name, int perms); /* within open, O_CREAT */ int close(int fd); int unlink(char *path); #include “stdio. h” /* A copy program*/ int main () { char *f 1 =“file 1”, *f 2=“file 2”; int fd 1, fd 2, n; if (fd 1 = open(f 1, O_RDONLY, 0) == -1) usage(“uanble to opening source file %sn”, f 1); if (fd 2 = creat(f 2, 0666) == -1) usage(“unable to creating new file %s n”, f 2); while ((n = read(fd 1, buf, 1024) > 0) write ( fd 2, buf, n); return 0; } #include <stdio. h> void usage(char *fmt, …) { va_list args; va_start(args, fmt); fprintf(stderr, “error: “); vfprintf(stderr, fmt, args); va_end(args); fprint(“usage: srcfile destfilen”); }

File seek -- access with fix location n lseek n n long lseek(int fd,

File seek -- access with fix location n lseek n n long lseek(int fd, long offset, itn origin); Origin: 0, 1, 2, determines where to start offset. n 0: beginning; 1: current location; 2: the end. #include “stdio. h” /* A copy program*/ int main () { char *f 1 =“file 1”, *f 2=“file 2”; int fd 1, fd 2, n; if (fd 1 = open(f 1, O_RDONLY, 0) == -1) usage(“uanble to opening source file %sn”, f 1); if (fd 2 = creat(f 2, 0666) == -1) usage(“unable to creating new file %s n”, f 2); else lseek(fd 2, 0 L, 2); while ((n = read(fd 1, buf, 1024) > 0) write ( fd 2, buf, n); return 0; }

Recap on lab 4 n File access alternatives n File * fp = fopen();

Recap on lab 4 n File access alternatives n File * fp = fopen(); n n fgets() Int fd = open(name, flags, perms); n read(fd, buff, n); #include <stdio. h> /* lab 4 */ int main () { char *maibox = argv[1]; int fd; File *fp; #if 1 fp = fopen(mailbox, “r”); #else fd = open (mailbox, O_RDONLY, 0); #endif for ( ) { /* keep reading every line and process the HEADER and BODIES */ } return 0; }