Thursday, February 21, 2008

CSE2031 lecture 12

SystemCalls

  • Check out these books
    • Stevens Advanced Programming in the unix Environment
    • Robins&Robins – unix systems programming

  • Processes
    • Fork
    • Exec….


     

  • Files
    • Open
    • Close
    • Read
    • Write
    • Lseek
    • Dup
    • Dup2


 

PerProcess file Descriptor table

System wide open file table

I-Nodes

File descriptors

Flags

Disk / device

0

Offset

 

1

To i-node

 

2

  

..

  
  • Write(fd,buf,50)
    • Fd
      • File descriptor
    • Buf
      • Void* char
    • 50
      • #bytes
  • Write(fd,"Hello",6)
    • If this was typed after the first line there would be an offset of 56
  • Lseek
    • Lseek(fd,offset,WHENCE)
      • Fd
        • File descriptor
      • Offset
      • WHENCE
        • SEEK_SET
        • SEEK_CUR
        • SEEK_END
  • Dup
    • Duplicates a file descriptor using another file descriptor
      • Int dup(int oldfd);
      • Int dup2(int oldfd, int newfd)
    • Why would you want to do that?
      • Because you would never want to do this
        • Dup2(fd,1);
          • First argument is one you want copied
          • Second is what you want it copied on
        • Close(fd);
        • Printf("hello\n";
        • This would write at a file temp

int a[15]


 


 

int (*a)[15] /** a is a pointer to an array of 15 ints**/


 

int fd

fd = open("temp", _|_|_, 0660)

/*for the last input '0660' there should always be a leading 0 because it is an octal*/

/*_| represents space for flags*/


 

#define    O_WRONLY    1<<4

    O_TRUNK    1<<2

---------------------------------------

fd = open("temp", ..flag..)

write(fd,...)

close(fd)x

---------------------------------------


 

always check the return value from a system acall


 

if((fd = open("temp", ..flags..))<0){perror("...."); exit(1)}


 

for every file there exists an 'I-Node"

along with an I-Node there is a system-wide openfiletable

and with that there is a perprocess file descriptor table

No comments: