Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercise ● Exercise 4.15: Write a version of mkdir which supports the - p option. Due by class 2/23....

Similar presentations


Presentation on theme: "Exercise ● Exercise 4.15: Write a version of mkdir which supports the - p option. Due by class 2/23...."— Presentation transcript:

1 Exercise ● Exercise 4.15: Write a version of mkdir which supports the - p option. Due by class 2/23....

2 Unions ● mytime.simpleDate is an integer of 4 bytes. ● mytime.preciseDate is a double. ● sizeof(mytime)=8. ● Tags can be used to indicate which field is active. ● struct conditions { float temp; union feels_like { float wind_chill; float heat_index; } } today;

3 Unions ● mytime.simpleDate is an integer of 4 bytes. ● mytime.preciseDate is a double. ● sizeof(mytime)=8. ● Tags can be used to indicate which field is active. ● struct conditions { float temp; union feels_like { float wind_chill; float heat_index; } } today;

4 Devices and Files ● Device I/O is like File I/O ● Every device (terminal, printer, mouse, etc) is attached to the file system by a file name. ● Devices are usually stored in /dev ● You communicate with devices with read, write, open, close, lseek ● example: who > /dev/pts/4 ● cp /etc/passwd /dev/pts/4

5 Devices and Files ● There are some special files: – /dev/null – a data sink – /dev/zeros – all the zeros you could ever want – /dev/mem – kernel interface to memory ● Open, read, write,close do what you expect. lseek is not defined for all devices. ● File permission, mod time, owner, types are stored in inode. ● Use stat to examine ● stty

6 Devices and Files ● Connections to device are not hollow tubes; they sometimes contain a complex sequences of steps ● A fd has a buffering mechanism ● A terminal has baud rates, parity, stop bits, echo-mode, buffering, cr translation

7 Devices and Files ● The features of a device connections can be controlled by fcntl ● #include int v; v=fcntl(fd,F_GETFL) //get flags v |= O_SYNC //set sync bit fcntl(fd,F_SETFL,v) //set flags now write()s to disk will be synced to the disk

8 Devices and Files ● auto-append mode is another fd attribute ● Consider logfiles like wtmp

9 Devices and Files ● Solution: set O_APPEND #include int v; v=fcntl(fd,F_GETFL) //get flags v |= O_APPEND//set sync bit fcntl(fd,F_SETFL,v) //set flags ● This makes lseek-write ATOMIC ● See man 2 open

10 Terminal ● Terminals have – buffering – autoappend ● They behave much differently than files – all connects to a tty (pty) have attributes of regular files – all connections to ttys have additional attributes appropriate for human interfaces

11 Terminals naïve view

12 Terminals less naïve view

13 Terminals ● Consider stty. ● To write user programs like an editor we have to be able to read and change the properties of a terminal. ● tcgetattr and tcsetattr ● struc termios ● termios_p : pointer to struct termios

14 Terminals ● #include struct termios settings; tcgettattr(fd,&settings); //test or clear bits tcsetattr(fd, TCSANOW, &settings);

15 Terminals TCSANOW the change occurs immediately. TCSADRAIN the change occurs after all output written to fd has been transmitted. This function should be used when changing param- eters that affect output. TCSAFLUSH the change occurs after all output written to the object referred by fd has been transmitted, and all input that has been received but not read will be discarded before the change is made.

16 Terminals

17 #include /** **showtty **displays some of current tty settings**/ main() {structtermios ttyinfo;/* this struct holds tty info */ if ( tcgetattr( 0, &ttyinfo ) == -1 ){ /* get info */ perror( "cannot get params about stdin"); exit(1);}/* show info */ showbaud ( cfgetospeed( &ttyinfo ) );/* get + show baud rate*/ printf("The erase character is ascii %d, Ctrl-%c\n", ttyinfo.c_cc[VERASE], ttyinfo.c_cc[VERASE]-1+'A'); printf("The line kill character is ascii %d, Ctrl-%c\n", ttyinfo.c_cc[VKILL], ttyinfo.c_cc[VKILL]-1+'A'); show_some_flags( &ttyinfo );/* show misc. flags*/ }

18 Terminals showbaud( thespeed ) /*prints the speed in english*/ { printf("the baud rate is "); switch ( thespeed ){ case B300:printf("300\n");break; case B600:printf("600\n"); break; case B1200:printf("1200\n"); break; case B1800:printf("1800\n"); break; case B2400:printf("2400\n"); break; case B4800:printf("4800\n"); break; case B9600:printf("9600\n"); break; default:printf("Fast\n");break; }

19 Terminals struct flaginfo { intfl_value; char*fl_name; }; struct flaginfo input_flags[] = { IGNBRK,"Ignore the break condition", BRKINT,"Signal interrupt on break", IGNPAR,"Ignore chars with parity errors", PARMRK,"Mark parity errors", INPCK,"Enable input parity check", ISTRIP,"Strip character", INLCR,"Map NL to CR on input", IGNCR,"Ignore CR", ICRNL,"Map CR to NL on input", IXON,"Enable start/stop output control", /* _IXANY,"enable any char to restart output",*/ IXOFF,"Enable start/stop input control", 0,NULL };

20 Terminals s truct flaginfo local_flags[] = { ISIG,"Enable signals", ICANON,"Canonical input (erase and kill)", /* _XCASE,"Canonical upper/lower appearance", */ ECHO,"Enable echo", ECHOE,"Echo ERASE as BS-SPACE-BS", ECHOK,"Echo KILL by starting new line", 0,NULL }; show_some_flags( struct termios *ttyp ) /* *show the values of two of the flag sets_: c_iflag and c_lflag *adding c_oflag and c_cflag is pretty routine - just add new *tables above and a bit more code below. */ {show_flagset( ttyp->c_iflag, input_flags ); show_flagset( ttyp->c_lflag, local_flags );}

21 Terminals show_flagset( int thevalue, struct flaginfo thebitnames[] ) /* * check each bit pattern and display descriptive title */ { inti; for ( i=0; thebitnames[i].fl_value ; i++ ) { printf( " %s is ", thebitnames[i].fl_name); if ( thevalue & thebitnames[i].fl_value ) printf("ON\n"); else printf("OFF\n"); }


Download ppt "Exercise ● Exercise 4.15: Write a version of mkdir which supports the - p option. Due by class 2/23...."

Similar presentations


Ads by Google