include

advertisement
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 use putty/pietty?
• Putty
– http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
• Pietty
– http://www.csie.ntu.edu.tw/~piaip/pietty/
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
–…
Unix-like command - Shell
• Command
–
–
–
–
–
–
–
–
ls - list directory contents
mv - move files or directories
mkdir - make directories
rm - remove files or directories
cd - change directory
man - format and display the on-line manual pages
chmod - change file system modes of files and directories.
…
Reference:
http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/history/linux/linux.tnc.edu.tw/te
chdoc/shell/book1.html
Unix-like command - Shell
• Command
– ls -a
• Include directory entries whose names begin with a
dot (`.').
– ls -l
• (The lowercase letter ``ell''.) List files in the long
format, as described in the The Long Format subsection
below.
– man ls
Unix-like command - Shell
• Command
– mkdir [folder_name] (create folder)
– rmdir [folder_name] (delete folder)
– rm [file_name] (delete file)
– mv [source] [target] (move files or folder)
Unix-like command - Shell
• Command
– cd [directory] (change the working directory)
– pwd (return working directory name)
– chmod [mode] [file] (change file modes)
• Mode : [user][group][guest]
-rwxrwxrwx 1 user group 1 Sep 28 2010 test.txt
Ex. chmod 644 test.txt
-rw-r--r--
1 user group 1 Sep 28 2010 test.txt
Unix-like command - Shell
• Command
– man man
• (format and display the on-line manual pages)
• Other:
– Reference:
• http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/hi
story/linux/linux.tnc.edu.tw/techdoc/shell/book1.html
• http://linux.vbird.org/linux_basic/redhat6.1/linux_06co
mmand.php
ee/edit
• BSD only
• Start ee : % ee <input filename>
• Usage
– edit mode like notepad
– ESC-ENTER : save/exit
vi
• Vi editor have two modes
– Command mode
– Edit mode
• start vi:
%vi <filename>
Command mode
Insert
Delete
Replace
Copy
.....
Command mode
Edit mode
[Esc]
Exit Edit mode
Reference:
http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/history/linux/linux.tnc.edu.tw/te
chdoc/vi.htm
FTP - WinSCP
• Add new account
– 使用工作站帳號密碼
– Port 22
– SFTP
FTP - FileZilla
•
•
•
•
開啓站台管理員
新增站台
選SFTP
登入型式(一般)
Fork & thread 教學
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.
fork() - example1
% gcc fork1.c -o fork1
#include <stdio.h>
#include<stdlib.h>
#include <unistd.h>
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 */ }
}
% ./fork1 &
[1] 16444
% my child is 16445
I am child!
% ps
PID TTY
16212 pts/18
16444 pts/18
16445 pts/18
16446 pts/18
TIME
00:00:00
00:00:05
00:00:05
00:00:00
CMD
tcsh
fork1
fork1
ps
% killall -v fork1
Killed fork1(16444) with signal 15
Killed fork1(16445) with signal 15
[1] + Terminated
./fork1
#include <stdio.h>
#include<stdlib.h>
#include <unistd.h>
fork() – example2
int main(void)
{
pid_t pid;
pid = fork();
if (pid>0) {
printf("my child is %d\n",pid);
printf("daemon on duty!\n");
/* do something here */
exit(0);
} else if (pid<0) {
printf("Can't fork!\n");
exit(-1);
}
for (;;) {
printf("I am the daemon!\n");
usleep(300000); //sleep 0.3 seconds
/* do something here */
}
}
% 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
Thread
•
•
•
•
Light weight process
Share resources
Own private data
Synchronization
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
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
How to create Pthread in unix-like OS?
• Linux, BSD, Saloris…etc
– Include:
• #include <pthread.h>
– Command line:
• % g++ threads.cpp -lpthread -o threads
pthread() – example1
#include<pthread.h>
#include<stdio.h>
#include <unistd.h>
#define NUM_THREADS 5
void *PrintHello(void *);
void *PrintHello(void *threadid)
{
int tid = *((int *)threadid);
printf("Hello World! thread #%d\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc , t;
for(t=0;t<NUM_THREADS;t++)
{
printf("In main: creating thread %d\n", t);
rc = pthread_create(&threads[t] , NULL , PrintHello , (void *)&t);
usleep(1000);
if(rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
}
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
pthread() – example2
#include <iostream>
#include <pthread.h>
using namespace std;
void *doSomething(void * arg);
void *doSomething(void *arg) {
for (;;) {
int tmp = *((int *)arg);
cout << tmp;
cout.flush();
sleep(tmp);
}
return NULL;
}
int main() {
int tmp1=1, tmp2=2;
pthread_t t1;
if ( pthread_create(&t1, NULL, doSomething, (int *)&tmp1) != 0 ) {
cout << "pthread_create() error" << endl;
exit(-1);
}
doSomething((int *)&tmp2);
}
pthread() – example2 cont.
% g++ threads2.cpp -o threads2 -lpthread
% ./thread2
211211211211211…(loop)…
Assignment
1-1: try to use fork() and Pthread
• Just “rand()” two global integer between
1~10, 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.
1-2: producer and consumer
• First,build a GLOBAL BUFFER,it’s a queue(only
need FIFO array,don’t need to creat a queue)
• Build a producer thread and consumer thread
• Producer: put numbers by rand() into buffer,you
can’t put more number when the buffer is full.
• Consumer: Take out the numbers in the buffer,
you can’t take more number out when the buffer
is empty.
• Print out the number and its location in buffer
from Producer,and Print out the number and its
location in buffer from Consumer (see textbook
7th. Edition Ch3-4)
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印出取出的數字跟buffer的號碼
• (詳見恐龍本第7版3-4)
1-2 cont.
• Buffer size=5
• Number of consumer and producer =12
• Simple Output:
–
–
–
–
–
–
–
–
producer(1)-producer put [208] in buffer[0]
producer(2)-producer put [142] in buffer[1]
consumer(1)-consumer get [208] in buffer[0] is
producer(3)-producer put [66] in buffer[2]
producer(4)-producer put [241] in buffer[3]
producer(5)-producer put [164] in buffer[4]
consumer(2)-consumer get [142] in buffer[1]
producer(6)-producer put [7] in buffer[0] ……………..
1-2 cont.
#include<stdio.h>
#include<pthread.h>
#include <time.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
void * consumer(void *argv){
for (int num=0;num<12;num++){
sleep(rand()%10);
//write here
}
}
void * producer(void *argv){
for (int num=0;num<12;num++){
sleep(rand()%5);
//write here
}
}
int main(){
int errno;
srand((int)time(0));
pthread_t p_tid, c_tid;
pthread_create(&c_tid, NULL, consumer, NULL);
pthread_create(&p_tid, NULL, producer, NULL);
pthread_join(c_tid ,NULL);
pthread_join(p_tid ,NULL);
}
Q&A
Download