Download presentation
Presentation is loading. Please wait.
1
Introduction to Linux (II) Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習
2
Linux 開機流程 BIOS 開機完成後根據設定的開機硬碟載入 Bootloader BootLoader 根據設定載入所指定的作業系統 PowerOn bootloader 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 Ethernet Printer Disk Terminal swapper lpd init inetd getty cshls ps
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 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
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
12
11 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
12 3 4 ex2 (a) 3 ex2 (b) 4 ex2 fork() 3 ex2 3 4 csh 0 1 ps (c)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.