Unix ?? - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Unix ??

Description:

Unix Unix-like System Linux FreeBSD Solaris Mac OS X Tools Login in tools Putty / pietty Editor ee(Easy Editor) vi FTP tools WinSCP FileZilla Client How to ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 29
Provided by: abc88
Category:
Tags: unix

less

Transcript and Presenter's Notes

Title: Unix ??


1
Unix ??
2
Unix-like System
  • Linux
  • FreeBSD
  • Solaris
  • Mac OS X

3
Tools
  • Login in tools
  • Putty / pietty
  • Editor
  • ee(Easy Editor)
  • vi
  • FTP tools
  • WinSCP
  • FileZilla Client

4
How to use putty/pietty?
  • Putty
  • http//www.chiark.greenend.org.uk/sgtatham/putty/
    download.html
  • Pietty
  • http//www.csie.ntu.edu.tw/piaip/pietty/

5
Login in
  • the default for SSH service is port 22
  • bsd1.cs.nctu.edu.tw bsd5.cs.nctu.edu.tw
  • linux1.cs.nctu.edu.tw linux6.cs.nctu.edu.tw

6
Unix-like command - Shell
  • Command
  • ls - list directory contents
  • mkdir - make directories
  • rm - remove files or directories
  • cd - change directory
  • man - format and display the on-line manual pages

Reference http//www.csie.nctu.edu.tw/tsaiwn/cou
rse/introcs/history/linux/linux.tnc.edu.tw/techdoc
/shell/book1.html
7
ee/edit
  • BSD only
  • Start ee ee ltinput filenamegt
  • Usage
  • edit mode like notepad
  • ESC-ENTER save/exit

8
vi
  • Vi editor have two modes
  • Command mode
  • Edit mode
  • start vi
  • vi ltfilenamegt

Command mode
Edit mode
Insert Delete Replace Copy .....
Command mode
Exit Edit mode
Esc
Reference http//www.csie.nctu.edu.tw/tsaiwn/cou
rse/introcs/history/linux/linux.tnc.edu.tw/techdoc
/vi.htm
9
FTP - WinSCP
  • Add new account
  • ?????????
  • Port 22
  • SFTP

10
FTP - FileZilla
  • ???????
  • ????
  • ?SFTP
  • ????(??)

11
Fork thread ??
12
fork
  • fork - create a new process
  • The new process (child process) shall be an exact
    copy of the calling process (parent process)
  • The child process shall have a unique process
    ID(different parent process ID).
  • The return value in the child is 0,whereas the
    return value in the parent is the process ID of
    the new child.
  • It return only -1 when fork failed.

13
fork() - example1
gcc fork1.c -o fork1 ./fork1 1 16444
my child is 16445 I am child! ps PID TTY
TIME CMD 16212 pts/18 000000
tcsh 16444 pts/18 000005 fork1 16445 pts/18
000005 fork1 16446 pts/18 000000 ps
killall -v fork1 Killed fork1(16444) with signal
15 Killed fork1(16445) with signal 15 1
Terminated ./fork1
  • include ltstdio.hgt
  • includeltstdlib.hgt
  • include ltunistd.hgt
  • int main(void)
  • pid_t pid
  • pid fork()
  • switch (pid)
  • case -1 printf("failure!\n") break
  • case 0 printf("I am child!\n") break
  • default printf("my child is d\n",pid) break
  • for () / do something here /

14
fork() example2
  • include ltstdio.hgt
  • includeltstdlib.hgt
  • include ltunistd.hgt
  • int main(void)
  • pid_t pid
  • pid fork()
  • if (pidgt0)
  • printf("my child is d\n",pid)
  • printf("daemon on duty!\n")
  • / do something here /
  • exit(0)
  • else if (pidlt0)
  • printf("Can't fork!\n")
  • exit(-1)
  • for ()
  • printf("I am the daemon!\n")

gcc fork2.c -o fork2 ./fork2 1 16423 my
child is 36845 daemon on duty! I am the daemon! I
am the daemon! (loop)
Open a new window to kill it killall -v
fork2 Or Direct input in the window to kill
it Hint we can use copy-paste to do it
killall -v fork2
15
Thread
  • Light weight process
  • Share resources
  • Own private data
  • Synchronization

16
Pthread API
  • int pthread_create(pthread_t thread, const
    pthread_attr_t attr, void (start_routine)(void
    ), void arg)
  • create a new thread with given attributes
  • int pthread_join(pthread_t thread, void
    status)
  • suspend caller until thread argument ends
  • void pthread_exit(void status)
  • terminate calling thread
  • int pthread_equal(pthread_t t1, pthread_t t2)
  • test if two thread IDs are to same thread
  • int pthread_cancel(pthread_t thread)
  • start cleanup and termination of given thread
  • int pthread_kill(pthread_t thread, int sig)
  • send given signal to specified thread
  • pthread_t pthread_self(void)
  • return ID of calling thread

17
Pthread API cont.
  • pthread_mutex_destroy()
  • destroy a mutex
  • pthread_mutex_init()
  • initialise a mutex
  • pthread_mutex_lock()
  • get mutex lock blocking while already locked
  • pthread_mutex_trylock()
  • try to get mutex lock, fail if already locked
  • pthread_mutex_unlock()
  • release lock on a mutex

18
How to create Pthread in unix-like OS?
  • Linux, BSD, Salorisetc
  • Include
  • include ltpthread.hgt
  • Command line
  • g threads.cpp -lpthread -o threads

19
pthread() example1
  • includeltpthread.hgt
  • includeltstdio.hgt
  • include ltunistd.hgt
  • define NUM_THREADS 5
  • void PrintHello(void )
  • int main (int argc, char argv)
  • pthread_t threadsNUM_THREADS
  • int rc , t
  • for(t0tltNUM_THREADSt)
  • printf("In main creating thread d\n", t)
  • rc pthread_create(threadst , NULL ,
    PrintHello , (void )t)
  • usleep(1000)
  • if(rc)

void PrintHello(void threadid) int tid
((int )threadid) printf("Hello World! thread
d\n", tid) pthread_exit(NULL)
20
pthread() example1 cont.
  • g threads1.cpp -o threads1 -lpthread
  • ./thread1
  • In main creating thread 0
  • Hello World! thread 0
  • In main creating thread 1
  • Hello World! thread 1
  • In main creating thread 2
  • Hello World! thread 2
  • In main creating thread 3
  • Hello World! thread 3
  • In main creating thread 4
  • Hello World! thread 4

21
pthread() example2
void doSomething(void arg) for ()
int tmp ((int )arg) cout ltlt tmp
cout.flush() sleep(tmp) return NULL
  • include ltiostreamgt
  • include ltpthread.hgt
  • using namespace std
  • void doSomething(void arg)
  • int main()
  • int tmp11, tmp22
  • pthread_t t1
  • if ( pthread_create(t1, NULL, doSomething,
    (int )tmp1) ! 0 )
  • cout ltlt "pthread_create() error" ltlt endl
  • exit(-1)
  • doSomething((int )tmp2)

22
pthread() example2 cont.
  • g threads2.cpp -o threads2 -lpthread
  • ./thread2
  • 211211211211211(loop)

23
Assignment
24
1-1 try to use fork() and Pthread
  • Just rand() two global integer between 110,
    then add them up
  • VER. Fork create a child process, then child
    rand() int1, parent rand() int2, child add up
    int1 and int2(YES! communication between
    process!)
  • VER. Thread create two threads, thread1 rand()
    int1,then sleep(int1), thread2 rand() int2, then
    sleep(int2) then main process add up int1 and
    int2.

25
1-2 producer and consumer
  • ?????GLOBAL?BUFFER,????queue(???FIFO???array,?????
    ?queue)
  • ??producer thread?consumer thread
  • Producer?????rand()???????buffer?,??buffer????????
    ?
  • Consumer?????buffer??????,??buffer???????
  • ??Producer????buffer??????,??consumer????????buffe
    r???
  • (??????7?3-4)

26
1-2 cont.
  • Buffer size5
  • Number of consumer and producer 12
  • Simple Output
  • producer(1)-producer put 208 in buffer0
  • producer(2)-producer put 142 in buffer1
  • consumer(1)-consumer get 208 in buffer0 is
  • producer(3)-producer put 66 in buffer2
  • producer(4)-producer put 241 in buffer3
  • producer(5)-producer put 164 in buffer4
  • consumer(2)-consumer get 142 in buffer1
  • producer(6)-producer put 7 in buffer0 ..

27
1-2 cont.
  • includeltstdio.hgt
  • includeltpthread.hgt
  • include lttime.hgt
  • define BUFFER_SIZE 5
  • int bufferBUFFER_SIZE
  • void consumer(void argv)
  • for (int num0numlt12num)
  • sleep(rand()10)
  • //write here
  • void producer(void argv)
  • for (int num0numlt12num)
  • sleep(rand()5)
  • //write here
  • int main()
  • int errno

28
Q A
Write a Comment
User Comments (0)
About PowerShow.com