Hw1

advertisement
UNIX 教學
1
UNIX-LIKE SYSTEM
• Linux
• FreeBSD
• Solaris
• Mac OS X
• …
2
TOOLS
• Login tools
 Putty / pietty
• Editors
 ee(Easy Editor)
 vi
• FTP tools
 WinSCP
 FileZilla Client
3
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/
4
LOGIN
•
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
 …
5
UNIX-LIKE COMMAND - SHELL
• Command
 clear – clear the screen
 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/techdoc/shell/book1.html
http://larc.ee.nthu.edu.tw/~lmdenq/Unix.htm
6
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
 man formats and displays the on-line manual pages. If you
specify section, man only looks in that section of the
manual. name is normally the name of the manual page, which is
typically the name of a command, function, or file.
7
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)
8
UNIX-LIKE COMMAND - SHELL
 Command
 cd [directory] (change the working directory)
 pwd (show the working directory path)
 chmod [mode] [file] (change file modes)
 Mode : [user][group][guest]
-rw-r--r-- 1 kuanhua gcs
15 Oct 10 20:44 test.txt
Ex. chmod 777 test.txt
-rwxrwxrwx 1 kuanhua gcs 15 Oct 10 20:44 test.txt
#
Permission
rwx
7
full
111
6
read and
write
110
5
read and
execute
101
4
read only
100
3
write and
execute
011
2
write only
010
1
execute only
001
0
none
000
9
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/history/linux/linux.tnc.edu.tw/
techdoc/shell/book1.html
 http://linux.vbird.org/linux_basic/redhat6.1/linux_06command.php
10
EE/EDIT
•
BSD only
•
Start ee : % ee <input filename>
•
Usage
 edit mode like notepad
 ESC-ENTER : save/exit
11
VIM
•
Vim has six BASIC modes
•
But we often use the following three mode:
 Normal mode
Normal mode
 Insert mode
<:>
 Command mode
•
<Esc>
start vim:
In Normal mode you can
enter all the normal
editor commands.
<Esc>
<i>
%vim <filename>
Command mode
Reference:
http://www.study-area.org/tips/vim/
Insert mode
12
FTP - WINSCP
•
Add new account
 使用工作站帳號密碼
 Port 22
 SFTP
13
FTP - FILEZILLA
 開啓站台管理員
 新增站台
 選SFTP
 登入型式(一般)
14
FORK & THREAD 教學
15
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 -1 only when fork failed.
16
FORK
Parent
pid = (Child pid)
…
…
…
pid = fork();
…
…
…
Child
pid = fork();
…
…
…
…
…
pid = 0
17
FORK() - EXAMPLE1
#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 */ }
}
%g++ fork1.cpp -o fork1
% ./fork1 &
[1] 17356
my child is 17357
I am child!
% ps
PID TT STAT
15299 66 Ss
17356 66 R
17357 66 R
17379 66 R+
TIME COMMAND
0:00.04 /bin/tcsh -l
1:18.48 ./fork1
1:18.49 ./fork1
0:00.00 ps
% killall -v fork1
kill -TERM 17357
kill -TERM 17356
[1] + Terminated
./fork1
18
SHARED 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
Process A
Process B
Shared Memory
19
FORK() + SHARED MEMORY
% gcc fork2.c -o fork2
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
% ./fork2 &
#include <stdlib.h>
[1] 17671
#include <unistd.h>
int main()
% parent rand = 5
{
child get number = 5
int ShmID, *ShmPTR;
pid_t pid;
ShmID = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT|0666);
ShmPTR = (int *) shmat(ShmID, NULL, 0);
pid = fork();
Fork
if (pid == -1) printf("failure!\n");
failure
else if (pid == 0)
for(;;) {
if( ShmPTR[0] != 0 ) {
Child
printf("child get number = %d\n",ShmPTR[0]);
process
exit(0);
}
}
else {
srand(pid);
Parent
ShmPTR[0] = rand()%10+1;
printf("parent rand number = %d\n",ShmPTR[0]);
process }
exit(0);
}
20
THREAD
•
Light weight process
•
Share resources
•
Own private data
•
Synchronization
21
PTHREAD 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
22
HOW TO CREATE PTHREAD IN UNIX-LIKE OS?
•
Linux, BSD, Saloris…etc
 Include:
 #include <pthread.h>
 Command line:
 % g++ threads.cpp -lpthread -o threads
23
PTHREAD() – EXAMPLE1
#include <pthread.h>
#include <iostream>
#include <unistd.h>
using namespace std;
void *PrintHello(void *threadid)
{
int tid = *((int *)threadid);
printf("Hello World! thread #%d\n", tid);
pthread_exit(NULL);
}
#define NUM_THREADS 5
void *PrintHello(void *);
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);
}
}
}
24
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
25
PTHREAD() – EXAMPLE2
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
using namespace std;
void *doSomething(void * arg);
int main() {
void *doSomething(void *arg) {
for (;;) {
int tmp = *((int *)arg);
cout << tmp;
cout.flush();
sleep(tmp);
}
return NULL;
}
int tmp1=1, tmp2=2;
pthread_t t1;
if ( pthread_create(&t1, NULL, doSomething, (int *)&tmp1) != 0 ) {
cout << "pthread_create() error" << endl;
exit(-1);
% g++ threads2.cpp -o threads2 -lpthread
}
doSomething((int *)&tmp2);
}
% ./thread2
211211211211211…(loop)…
26
ASSIGNMENT
27
1-1. USE FORK() AND PTHREAD
• Create two global arrays with five elements
• Just “rand()” all the elements between 1~10, and find the minimum
• Print out all the elements and minimum of each array, and the minimum of the
two arrays
• Output sample:
 array1: 4 6 7 7 8, min1=4
 array2: 3 7 6 3 1, min2=1
 minimum=1
28
1-1. USE FORK()
• VER. Fork:
• Create a child process
• parent rand() the values of array1 and find the min1 of array1
• child rand() the values of array2 and find the min2 of array2
• Then child find the minimum between min1 and min2
29
1-1. USE PTHREAD
• VER. Thread:
• Create two threads
• thread1 rand() the values of array1 and find the min1 of array1, then
sleep(min1)
• thread2 rand() the values of array2 and find the min2 of array2, then
sleep(min2)
• Then main process find the minimum between min1 and min2.
30
1-2: PRODUCER AND CONSUMER
 First, build a GLOBAL BUFFER. It’s a queue(only need FIFO array, don’t need
to create 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 Consumer
(see textbook 7 th. Edition Ch3-4)
31
1-2 CONT.
 Buffer size=5
 Number of consumer and producer =12
 Output sample:
 producer(1)-producer put [208] in buffer[0]
 producer(2)-producer put [142] in buffer[1]
 consumer(1)-consumer get [208] in buffer[0]
 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]
……………..
32
1-2 CONT.
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>//**
#include <time.h>
#include <unistd.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);
}
33
REQUIREMENT
•
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
(30%)
• 1-1-thread.c
(30%)
• 1-2.c
(30%)
• STUDENT_ID.doc
(10%)
34
REQUIREMENT CONT.
•
Compress your file and named as STUDENT_ID_ver1.zip ex: 9917111_ver1.zip
•
Deadline: 11/4 23:59
•
Upload to the FTP:
• host : caig.cs.nctu.edu.tw
• port : 30021
• username : OS12
• password : OS12
35
Q&A
36
Download