16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Processes I<br />

}<br />

printf("%d %s\n", cntr, argv[cntr]);<br />

}<br />

cntr = 0;<br />

while(envp[cntr] [0] != 0)<br />

{<br />

printf( "%s\n", envp[cntr]);<br />

cntr++;<br />

}<br />

testenv output<br />

0 a.out<br />

-= ./a/out<br />

FCEDIT=/usr/bin/vi<br />

EXINIT=set dir=/tmp<br />

HOME=/staff/tech/greg<br />

PWD=/staff/tech/greg/itb443<br />

SHELL=/bin/ksh<br />

MAIL=/usr/mail/greg<br />

EDITOR=vi<br />

TERMCAP=/etc/termcap<br />

LOGNAME=greg<br />

TERM=vt100<br />

PATH=/usr/bin:/usr/local/bin:/bin:/usr/lib …><br />

TZ=est10<br />

exit system call<br />

void exit (status)/* does NOT */<br />

int status; /* return */<br />

convention: 0 normal termination<br />

!= 0 abnormal termination<br />

[a child process's 'parent-pid' changes to 1 (1 = init process) on parent termination]<br />

The exiting process's parent receives <strong>the</strong> status via 'wait'<br />

wait system call<br />

int wait(status)<br />

int *status;<br />

/* returns process-id of child or –1 on error(no children)<br />

and status-code into *status unless status = NULL */<br />

Zombie: exit by child before wait by parent; zombie retains only process (system) descriptor info till waited.<br />

Orphan: parent terminates before child does ... child gets new 'parent-process id' of 1.<br />

wait (status)<br />

if *status.lbyte = 0<br />

<strong>the</strong>n *status.rbyte is child's exiting status-code, i.e. as in "exit(n);"<br />

Pipes<br />

- accessed via std i/f (i.e. via file descriptor)<br />

- each pipe associated <strong>with</strong> an inode (in table)<br />

- size: 10 blocks = 5120 bytes (>4096)<br />

- non-blocking read, blocking write (full)<br />

Must check no. of bytes read in. If it is not blocking it will just return fewer bytes than requested.<br />

Pipe creation<br />

int fd[2]; pipe(fd);<br />

/* fd[0] for reading<br />

96

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!