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 II<br />

/* background.c */<br />

#include <br />

main(int argc, char *argv[])<br />

{<br />

if(fork()== 0){ /* child */<br />

execvp(argv[1], &argv[1]);}<br />

fprintf(stderr, "could not execute %s\n", argv[1]);<br />

}<br />

background cc wait.c<br />

ps<br />

PID TT STAT TIME COMMAND<br />

664 p1 S 0:00 -ksh (ksh)<br />

710 p1 R 0:00 ps<br />

715 p1 D 0:00 cc wait.c<br />

/* redirect.c */<br />

#include <br />

#include <br />

main(int argc, char *argv[])<br />

{<br />

int fd;<br />

fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0600);<br />

dup2(fd, 1); /* duplicate standard output */<br />

close (fd) ; /* close original descriptor */<br />

execvp(argv[2], fprintf(stderr, &argv[2]); "main - should never execute\n");<br />

}<br />

redirect ls.out ls -1<br />

cat ls.out<br />

SIGNALS<br />

- terminates process and generates core file (dump)<br />

- terminates process <strong>with</strong>out core (quit)<br />

- ignores and discard signal (ignore)<br />

- suspends process (suspend)<br />

- resumes process<br />

/* alarm.c */<br />

#include <br />

main()<br />

{<br />

alarm(3); /* schedule an alarm in 3 secs */<br />

printf("looping forever ...\n");<br />

while (1);<br />

fprintf(stderr, "should never execute\n");<br />

}<br />

/* handler.c */<br />

#include <br />

#include <br />

int alarmflag = 0;<br />

void alarmhandler();<br />

main()<br />

{<br />

signal (SIGALRM, alarmhandler); /* signal handler */<br />

alarm(3); /* schedule an alarm in 3 secs */<br />

printf ("looping. . . \n") ;<br />

124

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

Saved successfully!

Ooh no, something went wrong!