Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Unix System Programming Chung-Ta King Department of Computer Science National Tsing Hua University."— Presentation transcript:

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

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

3 2 Processes zA process is a program in execution Ethernet Printer Disk Terminal swapper lpd init inetd getty cshls ps

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

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

6 5 Process Creation zSwitch to another program: yexeclp("/usr/ucb/rsh","rsh",”cs20","date",0); yreplaces current process image with a new image zSplit 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","sh",”-c",commandline,(char *) 0); yShell operation: repeat get next command fork a child to run the command (fork() & execlp();) wait for the child to terminate (wait();)

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

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

9 0 1 2 ex1 open() 0 1 2 ex1 3 0 1 2 3 0 1 2 | rsh 3 0 1 2 ex1 3 system() 0 1 2 csh 3 0 1 2 ps 3 outfile dup2() fork() 0 1 2 date cs20 cs21

10 9 Signals zWhen 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 zsignal(): 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 zsetjmp() and longjmp():

11 10 Signals: Example #include 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 */ }

12 11 Signals: Alarm zalarm(): 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 */ }


Download ppt "Unix System Programming Chung-Ta King Department of Computer Science National Tsing Hua University."

Similar presentations


Ads by Google