Title: Unix ??
1Unix ??
2Unix-like System
- Linux
- FreeBSD
- Solaris
- Mac OS X
3Tools
- Login tools
- Putty / pietty
- Editors
- ee(Easy Editor)
- vi
- FTP tools
- WinSCP
- FileZilla Client
4How to use putty/pietty?
- Putty
- http//www.chiark.greenend.org.uk/sgtatham/putty/
download.html - Pietty
- http//www.csie.ntu.edu.tw/piaip/pietty/
5Log 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
6Unix-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/cou
rse/introcs/history/linux/linux.tnc.edu.tw/techdoc
/shell/book1.html
7Unix-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
8Unix-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)
9Unix-like command - Shell
- Command
- cd directory (change the working directory)
- pwd (return working directory name)
- chmod mode file (change file modes)
- Mode usergroupguest
- -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
10Unix-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
/history/linux/linux.tnc.edu.tw/techdoc/shell/book
1.html - http//linux.vbird.org/linux_basic/redhat6.1/linux
_06command.php
11ee/edit
- BSD only
- Start ee ee ltinput filenamegt
- Usage
- edit mode like notepad
- ESC-ENTER save/exit
12vi
- 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
13FTP - WinSCP
- Add new account
- ?????????
- Port 22
- SFTP
14FTP - FileZilla
- ???????
- ????
- ?SFTP
- ????(??)
-
15Fork thread ??
16fork
- 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 -1 only when fork failed.
17fork() - 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
- include ltstdlib.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 /
18Shared Memory
- Memory space is shared between processes
- int shmget(key_t key, size_t size, int
shmflg) - Request a shared memory, and the return value is
a shared memory ID - void shmat(int shmid, const void shmaddr,
int shmflg) - Attach an existing shared memory to an address
space, and the return value is void pointer to
the memory - int shmdt(const void shmaddr)
- Detach a shred memory, shmaddr is the value
return by shmat() - int shmctl(int shmid, int IPC_RMID, NULL)
- Remove a shared memory
19fork() shared memory
gcc fork2.c -o fork2 ./fork2 1 17671
parent rand 5 child get number 5
- include ltsys/ipc.hgt
- include ltsys/shm.hgt
- include ltstdio.hgt
- include ltstdlib.hgt
- include ltunistd.hgt
- int main()
-
- int ShmID, ShmPTR
- pid_t pid
- ShmID shmget(IPC_PRIVATE, sizeof(int),
IPC_CREAT0666) - ShmPTR (int ) shmat(ShmID, NULL, 0)
- pid fork()
- if (pid -1) printf("failure!\n")
- else if (pid 0)
- for()
- if( ShmPTR0 ! 0 )
- printf("child get number d\n",ShmPTR0)
- exit(0)
-
Fork failure
Child process
Parent process
20Thread
- Light weight process
- Share resources
- Own private data
- Synchronization
21Pthread API
- int pthread_create(pthread_t thread, const
pthread_attr_t attr, void (start_routine)(void
), void arg) - Create a new thread. If successful, the function
returns 0 - int pthread_join(pthread_t thread, void status)
- Suspend caller until thread argument ends. If
successful, the function returns 0 - void pthread_exit(void status)
- Terminate calling thread
- int pthread_cancel(pthread_t thread)
- Send a cancellation request to a thread
- int pthread_kill(pthread_t thread, int sig)
- Send the signal sig to thread
- pthread_t pthread_self(void)
- Return ID of calling thread
22How to create Pthread in unix-like OS?
- Linux, BSD, Salorisetc
- Include
- include ltpthread.hgt
- Command line
- g threads.cpp -lpthread -o threads
23pthread() example1
- include ltpthread.hgt
- include ltiostreamgt
- 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)
24pthread() 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
25pthread() 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)
g threads2.cpp -o threads2 -lpthread
./thread2 211211211211211(loop)
26Assignment
271-1. use fork() and pthread
- Create two global arrays with five elements
- Just rand() all the elements between 110, and
find the maximum - Print out all the elements and maximum of each
array, and the maximum of the two arrays - Output sample
- array1 4 6 7 7 8, max18
- array2 3 7 6 3 1, max27
- maximum8
281-1. use fork()
- VER. Fork
- Create a child process
- parent rand() the values of array1 and find the
max1 of array1 - child rand() the values of array2 and find the
max2 of array2 - Then child find the maximum between max1 and
max2(YES! communication between process!)
291-1. use Pthread
- VER. Thread
- Create two threads
- thread1 rand() the values of array1 and find the
max1 of array1, then sleep(max1) - thread2 rand() the values of array2 and find the
max2 of array2, then sleep(max2) - Then main process find the maximum between max1
and max2.
301-2 producer and consumer
- First, build a GLOBAL BUFFER. Its a queue(only
need FIFO array, dont need to create a queue) - Build a producer thread and consumer thread
- Producer put numbers by rand() into buffer. You
cant put more number when the buffer is full. - Consumer take out the numbers in the buffer. You
cant take more number out when the buffer is
empty. - Print out the number and its location in buffer
from Producer and Consumer (see textbook 7th.
Edition Ch3-4)
311-2 cont.
- Buffer size5
- Number of consumer and producer 12
- Output sample
- producer(1)-producer put 208 in buffer0
- producer(2)-producer put 142 in buffer1
- consumer(1)-consumer get 208 in buffer0
- 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
..
321-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
33Requirement
- You should submit three c/cpp files and one
report - The report should include output results and what
you have learned in this homework - File format
- 1-1-fork.c
- 1-1-thread.c
- 1-2.c
- STUDENT_ID.doc
34Requirement cont.
- Compress your file and named as STUDENT_ID.zip
- Upload to the FTP
- host caig.cs.nctu.edu.tw
- port 30021
- username OS11
- password OS11
35Q A