ProcessesandPipes CS217 Fall2001 1 UnixProcess - - PDF document

processes and pipes
SMART_READER_LITE
LIVE PREVIEW

ProcessesandPipes CS217 Fall2001 1 UnixProcess - - PDF document

ProcessesandPipes CS217 Fall2001 1 UnixProcess Memory(addressspace) text,heap,stack,globaldata Processorstate PC , PSR ,general-purposeregisters Otherkerneldatastructures


slide-1
SLIDE 1

1

Fall2001 1

ProcessesandPipes

CS217

Fall2001 2

UnixProcess

  • Memory(addressspace)

text,heap,stack,globaldata

  • Processorstate

PC,PSR,general-purposeregisters

  • Otherkerneldatastructures

filetable

  • Howarethesestructures/fieldsinitialized?

Fall2001 3

Fork

  • Createanewprocess(systemcall)

child processinherits itsstatefromparent process parentandchildhaveseparatecopiesofthatstate parentandchildshareaccesstoanyopenfiles pid =fork(); if(pid!=0){ /*inparent*/ ... } /*inchild*/ ...

slide-2
SLIDE 2

2

Fall2001 4

Exec

  • Overlaycurrentprocessimagewitha

specifiedimagefile(systemcall)

affectsprocessmemoryandregisters hasnoaffectonfiletable

  • Example

execlp(“ls”,“ls”,“-l”,NULL); fprintf(stderr,“execfailed\n”); exit(1);

Fall2001 5

Exec(cont)

  • Manyvariationsofexec

intexeclp(constchar*file, constchar*arg,...) intexecl(constchar*path, constchar*arg,...) intexecv(constchar*path, char*const argv[]) intexecle(constchar*path, constchar*arg,..., char*constenvp[])

Alsoexecve andexecvp

Fall2001 6

Fork/Exec

  • Commonlyusedtogetherbytheshell

...parsecommandline ... if((pid =fork())==-1) fprintf(stderr,“forkfailed\n”); elseif(pid ==0){ /*inchild*/ execvp(file,argv); fprintf(stderr,“execfailed\n”); } else /*inparent*/ ...returntotopofloop ...

slide-3
SLIDE 3

3

Fall2001 7

Dup

  • Duplicateafiledescriptor(systemcall)

int dup(intfd);

duplicatesfd asthelowestunallocateddescriptor

  • Commonlyusedtoredirectstdin/stdout

intfd; fd =open(“foo”,O_RDONLY,0); close(0); dup(fd); close(fd);

Fall2001 8

Dup(cont)

  • Forconvenience…

dup2(intfd1,intfd2);

usefd2 toduplicatefd1 closesfd2 ifitwasinuse

fd =open(“foo”,O_RDONLY,0); dup2(fd,0); close(fd);

Fall2001 9

Wait

  • Parentwaitsforachild(systemcall)

pid_twait(int*status);

blocksuntilstatusofachildchanges returnspid ofthechildprocess returns–1ifnochildrenexist(alreadyexited)

if(fork()==0){ ... dup2(fd,0); ... execlp(“ls”,“ls”,“-l”,NULL); } pid =wait(&status);

slide-4
SLIDE 4

4

Fall2001 10

Pipes

  • Providesaninterprocesscommunication

channel

  • Afilterisaprocessthatreadsfromstdin

andwritestostdout

ProcessA ProcessB

  • utput

input

Filter

stdin stdout

Fall2001 11

Pipes(cont)

  • ManyUnixtoolsarewrittenasfilters

grep,sort,sed,cat,wc,awk ...

  • Shellssupportpipes

ls –l|more who|grepmary|wc ls *.[ch]|sort cat<foo|grepbar|sort>save

Fall2001 12

CreatingaPipe

  • Systemcall

int pipe(intfd[2]); return0 uponsuccessand–1 uponfailure fd[0] isopenforreading fd[1] isopenforwriting

  • Twocoordinatedprocessescreatedbyfork

canpassdatatoeachotherusingapipe.

slide-5
SLIDE 5

5

Fall2001 13

PipeExample

intpid,p[2]; ... pipe(p); if((pid=fork())==0){ close(p[1]); ...readusingp[0]asfd... } close(p[0]); ...writeusingp[1]asfd... close(p[1]);/*sendEOFtoreader*/ wait(&status);

Fall2001 14

PipesandStandardI/O

intpid,p[2]; pipe(p); if((pid=fork())==0){ close(p[1]); dup2(p[0],0); close(p[0]); ...readfromstdin ... } close(p[0]); dup2(p[1],1); close(p[1]); ...writetostdout ... wait(&status);