Operating Systems File access ≥ Sequential access H read all bytes/records from the beginning H cannot jump around, could rewind or back up E convenient when medium was mag tape g Random access a bytes/records read in any order H essential for data base systems Gao Haichang, Software School, Xidian University 13
Operating Systems Gao Haichang , Software School, Xidian University 13 File Access Sequential access read all bytes/records from the beginning cannot jump around, could rewind or back up convenient when medium was mag tape Random access bytes/records read in any order essential for data base systems
Operating Systems File attributes Attribute Meaning Protection Who can access the file and in what w Password Password needed to access the file Creator id of the person who created the file Owner Current owner Read-only flag 0 for read/write; 1 for read only Hidden flag 0 for normal; 1 for do not display in listings System flag 0 for normal files, 1 for system file Archive flag 0 for has been backed up; 1 for needs to be backed up ASCIbinary flag o for ASCll file; 1 for binary file Random access fla Temporary flag Lock flags 0 for unlocked: nonzero for locked Record length Number of bytes in a record Key position I Offset of the key within each record Key length Number of bytes in the key field Creation time Date and time the file was created Possible file Time of last access Date and time the file was last accessed Time of last change Date and time the file has last changed attributes current size Number of bytes in the file Maximum size Number of bytes the file may grow to Gao Haichang, Sofhvare School, Xidian University 14
Operating Systems Gao Haichang , Software School, Xidian University 14 File Attributes Possible file attributes
Operating Systems File operations 1. Create 7. Append 2. Delete 8 Seek Op pen 9. Get attributes 4. Close 10. Set attributes 5. Read 1. Rename 6. rite Gao Haichang, Software School, Xidian University 15
Operating Systems Gao Haichang , Software School, Xidian University 15 File Operations 1. Create 2. Delete 3. Open 4. Close 5. Read 6. Write 7. Append 8. Seek 9. Get attributes 10. Set Attributes 11. Rename
Operating Systems An Example Program Using File System Calls(1/2) copyfile abc xyz #include <sys/types. h> / include necessary header files * #include <fcntl. h> #include <stdlib. h> #include <unistd. h> int main(int argc, char *argv) / ANSI prototype * #define BUF SIZE 4096 /* use a buffer size of 4096 bytes #define OUTPUT MODE 0700 / protection bits for output file * int main(int argc, char *argil) int in fd out fd, rd count wt count char buffer[BUF_SIZE] if (argc I= 3)exit(1): / syntax error if argc is not 3*/ argc=3,argv[o]=‘ copyfile”,argv[们=“abc,argv2]=“xyz” Gao Haichang, Software School, Xidian University 16
Operating Systems Gao Haichang , Software School, Xidian University 16 An Example Program Using File System Calls (1/2) copyfile abc xyz argc=3, argv[0]=“copyfile”, argv[1]=“abc”, argv[2]=“xyz
Operating Systems G> An Example Program Using File System Calls(2/2) /* Open the input file and create the output file * in_fd= open(argv[1],o_RDONLY); *open the source file * if (in fd O)exit(2) /* if it cannot be opened, exit * out_fd= creat(argv[2, oUTPUT_MODE);/* create the destination file * if (out_fd O)exit( 3); if it cannot be created. exit * / Copy loop * while (TRUE) rd_ count= read(in_fd, buffer, BUF SIZE); / read a block of data * if(rd__count < 0)break / if end of file or error, exit loop * wt_count= write(out_fd, buffer, rd_count); /*write data * if (wt_count < O)exit(4) / wt count <=0 is an error * / Close the files * (in fd lose(in_ fd) close(out_ fd) if(rd_count = 0) / no error on last read * else exit(5) / error on last read * Gao Haichang, Software School, Xidian University 17
Operating Systems Gao Haichang , Software School, Xidian University 17 An Example Program Using File System Calls (2/2)