Unix System Programming ChungTa King Department of Computer

  • Slides: 12
Download presentation
Unix System Programming Chung-Ta King Department of Computer Science National Tsing Hua University

Unix System Programming Chung-Ta King Department of Computer Science National Tsing Hua University

Layers of the Unix (system calls: entries to kernel; facilities provided by OS) 1

Layers of the Unix (system calls: entries to kernel; facilities provided by OS) 1

Processes z A process is a program in execution init csh Terminal getty ls

Processes z A process is a program in execution init csh Terminal getty ls ps inetd Ethernet swapper lpd Disk Printer 2

Low-level Process I/O z All communication of a process with outside is done by

Low-level Process I/O z All communication of a process with outside is done by reading or writing files => a single interface z File descriptor y. A non-negative integer for reference to a file y. Three descriptors are created at process creation: stdin (0), stdout (1), stderr (2) all are connected to the terminal by default y. More descriptors can be created with proper system calls: fd = open(“outfile”, O_WRONLY, 0644); y. Descriptor table: there is limit on # of open files (20) y. Related system calls: read, write, open, creat, close, unlink, lseek, dup 2 3

Low-level Process I/O: Example /* copy f 1 to f 2 */ int f

Low-level Process I/O: Example /* copy f 1 to f 2 */ int f 1, f 2, n; if ((f 1=open(arg[1], O_RDONLY)) == -1) /* error if non-exist */ error(“can’t open %s”, argv[1]); if ((f 2 = creat(argv[2], 0644)) == -1) error(“can’t create %s”, argv[2]); while ((n = read(f 1, buf, BUFSIZ)) > 0) /* return: 0 -> EOF; -1 -> error; n < BUFSIZ -> OK (read will return upto end of line) */ if (write(f 2, buf, n) != n) error(“write error”, (char *) 0); 4

Process Creation z Switch to another program: yexeclp("/usr/ucb/rsh", "rsh", ”cs 20", "date", 0); yreplaces

Process Creation z Switch to another program: yexeclp("/usr/ucb/rsh", "rsh", ”cs 20", "date", 0); yreplaces current process image with a new image z Split a process: fork() and wait() yfork() produces two identical processes; the child process returns 0 and parent returns child pid yif (fork() == 0) execlp ("sh", ”-c", commandline, (char *) 0); y. Shell operation: repeat get next command fork a child to run the command (fork() & execlp(); ) wait for the child to terminate (wait(); ) 5

Examine Process Status z ps -ajl F UID PID 100 0 1 40 0

Examine Process Status z ps -ajl F UID PID 100 0 1 40 0 2 40 0 3 40 0 4 40 0 5 140 0 12 140 0 13 100 0 67 100000 1000 28413 1000 28424 100 519 32452 100000 519 32459 PPID PRI SIZE RSS. . . STAT TTY 0 0 780 164 S ? 1 0 0 0 SW ? 1 -12 0 0 SW< ? 1 0 0 0 SW ? 1 0 756 100 S ? 1 0 768 132 S ? 1 0 772 112 S 2 28410 0 1992 696 S 1 28419 0 0 1232 S p 0 32451 9 0 1188 S p 1 32452 12 0 852 R p 1 TIME COMMAND 0: 20 init [3] 0: 00 (kflushd) 0: 00 (kswapd) 0: 00 (nfsiod) 0: 20 /sbin/update 0: 00 /sbin/kernel 0: 00 (agetty) 0: 00 /usr/lib/X 11 0: 00 -bin/tcsh 0: 00 -tcsh 0: 00 ps -ajl 6

Processes and Descriptors fd = open("outfile", 01002, 0644); dup 2(fd, 1); /* dup fd

Processes and Descriptors fd = open("outfile", 01002, 0644); dup 2(fd, 1); /* dup fd to 1 => 1 now link to file */ if (fork() == 0) /* the child */ execlp("/usr/ucb/rsh", "rsh", ”cs 20", "date", 0); else { /* the parent */ fprint(stderr, ”child working …n”); wait(&status); system("ps -ajl"); tty = open(“/dev/tty”, 2); write(tty, “done!”, 5); } 7

2 ex 1 0 1 open() 2 0 ex 1 3 1 dup 2()

2 ex 1 0 1 open() 2 0 ex 1 3 1 dup 2() fork() 2 0 ex 1 3 1 2 date 0 1 ex 1 2 0 | rsh 3 1 2 0 3 1 system() 2 0 csh 3 1 cs 21 fork() ps cs 20 outfile 8

Signals z When an external event of concern occurs, a signal is sent to

Signals z When an external event of concern occurs, a signal is sent to all processes that were started from the same terminal and terminates them by default z signal(): alters the default action on a signal ysignal(SIGINT, SIG_IGN); ysignal(SIGINT, handle_int); ysignal() returns previous value of the signal and resets to default action z setjmp() and longjmp(): 9

Signals: Example #include <signal. h> #include <setjmp. h> jmp_buf sjbuf; main() { if (signal(SIGINT,

Signals: Example #include <signal. h> #include <setjmp. h> jmp_buf sjbuf; main() { if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, onintr); setjmp(sjbuf); /* save current stack position */ /* main loop */ } onintr() { signal(SIGINT, onintr); /* reset for next interrupt */ longjmp(sjbuf, 0); /* jump to saved state */ } 10

Signals: Alarm z alarm(): causes SIGALRM sent to process n sec later /* “timeout

Signals: Alarm z alarm(): causes SIGALRM sent to process n sec later /* “timeout prog” run prog and abort it after 3600 sec */ main() { if ((pid=fork()) == 0) execvp(argv[1], &argv[1]); signal(SIGALRM, onalarm); alarm(3600); if (wait(&status) == -1 || status & 0177) != 0) error(“%s killed”, argv[1]); } onalarm() /* kill child when alarm arrives */ { kill(pid, SIGKILL); /* send pid the signal */ } 11