lecture 7: file i/o, more unixix.cs.uoregon.edu/~hank/330/lectures/cis330_s18_lec7.pdf · through...

49
Hank Childs, University of Oregon April 18 th , 2018 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 7: file I/O, more Unix

Upload: others

Post on 03-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Hank Childs, University of OregonApril 18th, 2018

CIS 330:_ _ _ _ ______ _ _____

/ / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __/ / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_/ /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/\____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/

Lecture 7: file I/O, more Unix

Page 2: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Announcements

• Projects– 4AdueINCLASSFriday(nolate)– 2Bassignedtoday,dueMonday– 3A,2Ctobeassignedsoon

• Nextweek:– LectureMondayandFriday– LabWednesday– Also,willbeaYouTubeshortlecture

• (Wearegettingaheadofschedule… tryinghardtopreventworkfromthisclasstobebackloaded)

Page 3: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Outline

• Review• FileI/O• Project2B• Redirection• Pipes

Page 4: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Outline

• Review• FileI/O• Project2B• Redirection• Pipes

Page 5: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Makefile example:multiplierlib

Page 6: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Unixcommand:tar

• tarcvf 330.tarfile1file2file3– puts3files(file1,file2,file3)intoanewfilecalled330.tar

• scp 330.tar@ix:~• ssh ix• tarxvf 330.tar• ls

file1file2file

Page 7: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

MemoryErrors

• Arrayboundsread

• Arrayboundswrite

Page 8: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

MemoryErrors

• Freememoryread/freememorywrite

Vocabulary:“danglingpointer”:pointerthatpointstomemorythathasalreadybeenfreed.

Page 9: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

MemoryErrors

• Freeingunallocatedmemory

Page 10: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

MemoryErrors

• Freeingnon-heapmemory

Page 11: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

MemoryErrors

• NULLpointerread/write

• NULLisneveravalidlocationtoreadfromorwriteto,andaccessingthemresultsina“segmentationfault”– ….rememberthosememorysegments?

Page 12: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

MemoryErrors

• Uninitializedmemoryread

Page 13: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

NEW:IPR,IPW

• int *X=0xDEADBEEF:• int Y=*X;

• whaterrorcodefits?

• Imissedtwo:– InvalidPointerRead(IPR)– InvalidPointerWriter(IPW)

Page 14: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Memoryerrorinaction

Page 15: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Project4A

• Postednow• Youwillpracticedebugging&usingadebugger– Thereare3programsyouneedtodebug

• Inthiscase,“debug”meansidentifythebug– Doesnotmeanfixthebug

• Canusegdb orlldb• Maywanttorunonix

• WorksheetdueinclassFriday• P3.c:4errors

Page 16: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Outline

• Review• FileI/O• Project2B• Redirection• Pipes

Page 17: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

FileI/O:streamsandfiledescriptors

• Twowaystoaccessfiles:– Filedescriptors:• Lowerlevelinterfacetofilesanddevices

– Providescontrolstospecificdevices

• Type:smallintegers(typically20total)

– Streams:• Higherlevelinterfacetofilesanddevices

– Providesuniforminterface;easytodealwith,butlesspowerful

• Type:FILE*

Streamsaremoreportable,andmoreaccessibletobeginningprogrammers.(Iteachstreamshere.)

Page 18: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

FileI/O

• Processforreadingorwriting– Openafile• TellsUnixyouintendtodofileI/O• Functionreturnsa“FILE*

– Usedtoidentifythefilefromthispointforward

• Checkstoseeifpermissionsarevalid

– Readfromthefile/writetothefile– Closethefile

Page 19: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Openingafile

• FILE*handle=fopen(filename,mode);

Example:FILE*h=fopen(“/tmp/330”,“wb”);

Note:#include<stdio.h>

Closewhenyouaredonewith“fclose”

Page 20: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Reading/Writing

Page 21: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Example

Page 22: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

FilePositionIndicator

• Filepositionindicator:thecurrentlocationinthefile

• IfIreadonebyte,theonebyteyougetiswherethefilepositionindicatorispointing.– Andthefilepositionindicatorupdatestopointatthenextbyte

– Butitcanbechanged…

Page 23: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

fseek

Page 24: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

ftell

Page 25: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Wehaveeverythingweneedtomakeacopycommand…

• fopen• fread• fwrite• fseek• ftell

Canwedothistogetherasaclass?

Page 26: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

argc &argv

• twoargumentstoeveryCprogram• argc:howmanycommandlinearguments• argv:anarraycontainingeachofthearguments

• “./a.out hankchilds”• à argc ==3• à argv[0]=“a.out”,argv[1]=“hank”,argv[2]=“childs”

Page 27: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,
Page 28: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Returnvaluesinshells

$?isthereturnvalueofthelastexecutedcommand

Page 29: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Printingtoterminalandreadingfromterminal

• InUnix,printingtoterminalandreadingfromterminalisdonewithfileI/O

• Keyboardandscreenarefilesinthefilesystem!– (atleasttheywere…)

Page 30: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

StandardStreams

• Wikipedia:“preconnected inputandoutputchannelsbetweenacomputerprogramanditsenvironment(typicallyatextterminal)whenitbeginsexecution”

• Threestandardstreams:– stdin (standardinput)– stdout (standardoutput)– stderr (standarderror)

WhatmechanismsinCallowyoutoaccessstandardstreams?

Page 31: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

printf

• Printtostdout– printf(“helloworld\n”);– printf(“Integersarelikethis%d\n”,6);– printf(“Twofloats:%f,%f”,3.5,7.0);

Page 32: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

fprintf

• Justlikeprintf,buttostreams• fprintf(stdout,“helloworld\n”);–à sameasprintf

• fprintf(stderr,“helloworld\n”);– printsto“standarderror”

• fprintf(f_out,“helloworld\n”);– printstothefilepointedtobyFILE*f_out.

Page 33: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

bufferingandprintf

• Important:printf isbuffered• So:– printf putsstringinbuffer– otherthingshappen– bufferiseventuallyprinted

• Butwhataboutacrash?– printf putsstringinbuffer– otherthingshappen…includingacrash– bufferisneverprinted!Solutions:(1)fflush,(2)fprintf(stderr)alwaysflushed

Page 34: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Outline

• Review• FileI/O• Project2B• Redirection• Pipes

Page 35: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Project2B

0 1

Page 36: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Outline

• Review• FileI/O• Project2B• Redirection• Pipes

Page 37: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Unixshellsallowsyoutomanipulatestandardstreams.

• “>”redirectoutputofprogramtoafile• Example:– ls >output– echo“thisisafile”>output2– catfile1file2>file3

Page 38: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Unixshellsallowsyoutomanipulatestandardstreams.

• “<”redirectfiletoinputofprogram• Example:– python<myscript.py• Note:pythonquitswhenitreadsaspecialcharactercalledEOF(EndofFile)• YoucantypethischaracterbytypingCtrl-D• ThisiswhyPythonquitswhenyoutypeCtrl-D

– (manyotherprogramstoo)

Page 39: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Unixshellsallowsyoutomanipulatestandardstreams.

• “>>”concatenateoutputofprogramtoendofexistingfile– (orcreatefileifitdoesn’texist)

• Example:– echo“Iamstartingthefile”>file1– echo“Iamaddingtothefile”>>file1– catfile1

IamstartingthefileIamaddingtothefile

Page 40: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

What’shappeninghere?

ls isoutputtingitserrormessagestostderr

Page 41: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Redirectingstderr inashell

Page 42: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Redirectingstderr tostdout

Convenientwhenyouwantbothtogotothesamestream

Page 43: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Outline

• Review• FileI/O• Project2B• Redirection• Pipes

Page 44: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

cfunctions:forkandpipe• fork:duplicatescurrentprogramintoaseparateinstance– Tworunningprograms!– Onlydifferentiatedbyreturnvalueoffork(whichisoriginalandwhichisnew)

• pipe:mechanismforconnectingfiledescriptorsbetweentwoforkedprograms

Throughforkandpipe,youcanconnecttworunningprograms.Onewritestoafiledescriptor,andthe

otherreadstheoutputfromitsfiledescript

Onlyusedonspecialoccasions.(Andoneofthoseoccasionsiswiththeshell.)

Page 45: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

pipesinUnixshells

• representedwith“|”• outputofoneprogrambecomesinputtoanotherprogram

Page 46: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Veryusefulprograms

• grep:keeplinesthatmatchpattern,discardlinesthatdon’tmatchpattern

Page 47: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Veryusefulprograms

• sed:replacepattern1withpattern2– sed s/pattern1/pattern2/g• smeanssubstitute• gmeans“global”…everyinstanceontheline

sed isalsoavailablein“vi”:%s/pattern1/pattern2/g(%meansalllines)

:103,133s/p1/p2/g(lines103-133)

Page 48: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Wildcards

• ‘*’isawildcardwithunix shells

‘?’isawildcardthatmatchesexactlyonecharacter

Page 49: Lecture 7: file I/O, more Unixix.cs.uoregon.edu/~hank/330/lectures/CIS330_S18_Lec7.pdf · Through fork and pipe, you can connect two running programs. One writes to a file descriptor,

Otherusefulshellthings

• ‘tab’:auto-complete• esc=:showoptionsforauto-complete• Ctrl-A:gotobeginningofline• Ctrl-E:gotoendofline• Ctrl-R:searchthroughhistoryforcommand