1 / 12

Unix System Programming

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). Processes. A process is a program in execution. init. swapper. csh. ls. ps. Disk. getty. Terminal.

tadhg
Download Presentation

Unix System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  5. 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 < BUFSIZ -> OK (read will return upto end of line) */ if (write(f2,buf,n) != n) error(“write error”, (char *) 0);

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

  7. Examine Process Status • ps -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. 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. 2 0 2 0 ex1 ex1 1 3 1 open() dup2() 2 0 2 0 ex1 3 1 date 1 fork() ex1 | rsh 2 0 2 0 ex1 3 1 3 1 system() 2 0 2 0 cs20 csh ps 3 1 3 1 fork() outfile cs21

  10. Signals • 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 • signal(): alters the default action on a signal • signal(SIGINT, SIG_IGN); • signal(SIGINT, handle_int); • signal() returns previous value of the signal and resets to default action • setjmp() and longjmp():

  11. 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 */ }

  12. Signals: Alarm • 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 */ }

More Related