1 / 13

Introduction to Linux (II)

CS1103 電機資訊工程實習. Introduction to Linux (II). Prof. Chung-Ta King Department of Computer Science National Tsing Hua University. Linux 開機流程. BIOS 開機完成後根據設定的開機硬碟載入 Bootloader BootLoader 根據設定載入所指定的作業系統. bootloader. PowerOn. BIOS. Hardware 開機. Linux kernel. init. System ready.

tracen
Télécharger la présentation

Introduction to Linux (II)

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. CS1103 電機資訊工程實習 Introduction to Linux (II) Prof. Chung-Ta King Department of Computer Science National Tsing Hua University

  2. Linux 開機流程 • BIOS開機完成後根據設定的開機硬碟載入Bootloader • BootLoader根據設定載入所指定的作業系統 bootloader PowerOn BIOS Hardware 開機 Linux kernel init System ready

  3. Layers of Unix (system calls: entries to kernel from user-space application)

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

  5. 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/keyboard by default • More descriptors can be created:fd = open(“outfile”, O_WRONLY, 0644); • Descriptor table: with a limit on # of open files http://linux.die.net/man/2/syscalls

  6. Low-level Process I/O: Example main(int argc, char *argv[]) { /* copy f1 to f2 */ int f1,f2,n; char buf[BUFSIZ]; if ((f1=open(argv[1],O_RDONLY)) == -1) /* error if non-exist */ error(“can’t open %s\n”, 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 up to end of line */ if (write(f2,buf,n) != n) error(“write error”, (char *) 0); }

  7. Unix Process Creation • Switch to another program: execve(“/usr/bin/rsh”,“rsh”,”cs20”, “date”,0,0); • Replaces current process image • Split a process: fork() and wait() • fork() produces two identical processes • Child process returns 0 and parent returns child’s pid if (fork() == 0)execve(“/bin/sh”, “sh”, “-c”, commandline,(char *) 0,0);

  8. Unix Process Creation • Given fork(), execve(), and wait(), it is easy to understand how shell operates: repeat get next command fork a child to run command (fork() & execve()) wait for the child to terminate (wait())

  9. Examine Process Status (ps al)

  10. Processes and Descriptors char *argu[] = {"rsh","cs20",”date”,0}; fd = open(“outfile”,O_RDWR,0644); dup2(fd,1); /* dup fd to 1 */ /* 1 now link to outfile */ if (fork() == 0) /* the child */ execve(“/usr/bin/rsh”, argu,0); else { /* the parent */ fprint(stderr, “child working …\n”); wait(&status); system(“ps al”); }

  11. 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

  12. Pipes for Inter-process Comm. • Pipe: a unidirectional byte stream e.g., ls | pr -2 | lpr int sk[2]; /* sk[0]: read-end; sk[1]: write-end */ pipe(sk); /* create a pipe */ if (fork()) { /* the parent */ close(sk[1]); while(read(sk[0],buf,SIZE)>0) printf("%s",buf); } else { /* the child */ close(sk[0]); fd=popen("ps -l","r"); while((s=read(fd,buf,SIZE))>0) write(sk[1],buf,s); }

  13. 3 ex2 3 ex2 4 3 (a) ex2 4 3 ex2 csh fork() 0 ex2 4 ps 1 (b) (c)

More Related