introduction to linux (ii)

Post on 21-Jan-2016

29 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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 Presentation

TRANSCRIPT

Introduction to Linux (II)

Prof. Chung-Ta KingDepartment of Computer ScienceNational Tsing Hua University

CS1103 電機資訊工程實習

Linux 開機流程

BIOS 開機完成後根據設定的開機硬碟載入 Bootloader BootLoader 根據設定載入所指定的作業系統

PowerOn bootloaderBIOS

Hardware 開機

Linux kernel init System ready

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)

Processes

A process is a program in execution

Ethernet

Printer

Disk

Terminal

swapper

lpd

init

inetdgetty

csh ls ps

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

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);}

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);

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())

Examine Process Status (ps al)

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”);}

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

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); }

13

34ex2

(a)

3ex2

(b)

4ex2

fork()

3ex2

34ex2

csh

01ps

(c)

top related