introduction to linux (ii)
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/1.jpg)
Introduction to Linux (II)
Prof. Chung-Ta KingDepartment of Computer ScienceNational Tsing Hua University
CS1103 電機資訊工程實習
![Page 2: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/2.jpg)
Linux 開機流程
BIOS 開機完成後根據設定的開機硬碟載入 Bootloader BootLoader 根據設定載入所指定的作業系統
PowerOn bootloaderBIOS
Hardware 開機
Linux kernel init System ready
![Page 3: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/3.jpg)
Layers of Unix
Users
Shells and commands
System calls
Signals IPC
Networking
File system I/O system
Device drivers
CPU scheduling Demand paging
Process managing
Machine hardware
(system calls: entries to kernel from user-space application)
![Page 4: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/4.jpg)
Processes
A process is a program in execution
Ethernet
Printer
Disk
Terminal
swapper
lpd
init
inetdgetty
csh ls ps
![Page 5: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/5.jpg)
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 fileThree 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 fileshttp://linux.die.net/man/2/syscalls
![Page 6: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/6.jpg)
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);}
![Page 7: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/7.jpg)
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 processesChild process returns 0 and parent returns
child’s pidif (fork() == 0)execve(“/bin/sh”, “sh”, “-c”, commandline,(char *) 0,0);
![Page 8: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/8.jpg)
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())
![Page 9: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/9.jpg)
Examine Process Status (ps al)
![Page 10: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/10.jpg)
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”);}
![Page 11: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/11.jpg)
012
ex1 open()
012
ex1 3
012
ex1 3
012ex1
rsh 3012
ex1 3system()
012
csh 3012
ps 3outfile
dup2()
fork()
fork()
012
date
cs20
cs21
![Page 12: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/12.jpg)
12
Pipes for Inter-process Comm.
Pipe: a unidirectional byte streame.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); }
![Page 13: Introduction to Linux (II)](https://reader035.vdocuments.site/reader035/viewer/2022062500/56814fbb550346895dbd7422/html5/thumbnails/13.jpg)
13
34ex2
(a)
3ex2
(b)
4ex2
fork()
3ex2
34ex2
csh
01ps
(c)