Introduction to Linux (II) - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Linux (II)

Description:

CS1103 Introduction to Linux (II) Prof. Chung-Ta King Department of Computer Science National Tsing Hua University – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 14
Provided by: Preferr404
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Linux (II)


1
Introduction to Linux (II)
CS1103 ????????
  • 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 bufBUFSIZ
  • if ((f1open(argv1,O_RDONLY)) -1)
  • / error if non-exist /
  • error(cant open s\n, argv1)
  • if ((f2 creat(argv2,0644)) -1)
  • error(cant create s,argv2)
  • while ((n read(f1,buf,BUFSIZ)) gt 0)
  • / return 0-gtEOF -1-gterror nltBUFSIZ-gtOK /
  • / 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
    childs 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
2
0
2
ex1
ex1
1
1
3
open()
dup2()
0
2
0
2
ex1
1
3
date
1
fork()
ex1 rsh
0
2
0
2
ex1
1
3
1
3
system()
0
2
0
2
cs20
csh
ps
1
3
1
3
fork()
outfile
cs21
12
Pipes for Inter-process Comm.
  • Pipe a unidirectional byte stream
  • e.g., ls pr -2 lpr
  • int sk2 / sk0 read-end sk1 write-end
    /
  • pipe(sk) / create a pipe /
  • if (fork()) / the parent /
  • close(sk1)
  • while(read(sk0,buf,SIZE)gt0) printf("s",buf)
  • else / the child /
  • close(sk0) fdpopen("ps -l","r")
  • while((sread(fd,buf,SIZE))gt0)
    write(sk1,buf,s)

13
3
ex2
3
ex2
4
3
(a)
ex2
4
3
ex2
csh
fork()
0
ex2
4
ps
1
(b)
(c)
Write a Comment
User Comments (0)
About PowerShow.com