Unix 教學 1. Unix-like System  Linux  FreeBSD  Solaris  Mac OS X  … 2.

Slides:



Advertisements
Similar presentations
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.
Advertisements

PTHREADS These notes are from LLNL Pthreads Tutorial
1 Introduction to UNIX Ke Liu
1 Introduction to UNIX 2 Ke Liu
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Working Environment - - Linux - -.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
CSE 451 Section 4 Project 2 Design Considerations.
CS1020: Intro Workshop. Topics CS1020Intro Workshop Login to UNIX operating system 2. …………………………………… 3. …………………………………… 4. …………………………………… 5. ……………………………………
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
OPERATIONS ON PROCESSES Process creation fork() exec() The argument vector Process deletion kill() signal()
Project 2 Data Communication Spring 2010, ICE Stephen Kim, Ph.D.
Help session: Unix basics Keith 9/9/2011. Login in Unix lab  User name: ug0xx Password: ece321 (initial)  The password will not be displayed on the.
Unix 教學. Unix-like System Linux FreeBSD Solaris Mac OS X …
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Essential Unix at ACEnet Joey Bernard, Computational Research Consultant.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
Thread Synchronization with Semaphores
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Module 2 Programming with Processes. Processes Process -- a program in execution –May share code segments –Typically do not share data, stack, heap OS.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
Introduction to UNIX Road Map: 1. UNIX Structure 2. Components of UNIX 3. Process Structure 4. Shell & Utility Programs 5. Using Files & Directories 6.
CS333 Intro to Operating Systems Jonathan Walpole.
Unix 教學. Unix-like System Linux FreeBSD Solaris Mac OS X …
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
Getting Started UNIX InKwan Yu Topics Unix Commands Unix System calls C function calls.
Process Management Azzam Mourad COEN 346.
Threads Dr. Yingwu Zhu. Threaded Applications Web browsers: display and data retrieval Web servers Many others.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
CS431-cotter1 Linux Programming, Processes, and Threads Man pages (fork, clone, execve, pthread_create) The Linux Programming Interface – Kerrisk, No Starch,
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
Dr. Sajib Datta Jan 16,  The website is up.  Course lectures will be uploaded there ◦ Check regularly for assignments and update.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Unix 教學.
UNIX To do work for the class, you will be using the Unix operating system. Once connected to the system, you will be presented with a login screen. Once.
CS1010: Intro Workshop.
Web Programming Essentials:
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
PTHREADS These notes are from LLNL Pthreads Tutorial
Linux Processes & Threads
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Shared Memory Dr. Yingwu Zhu.
Operating Systems Lecture 13.
Operating Systems Lecture 14.
Thread Programming.
CS510 Operating System Foundations
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Programming with Shared Memory
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Presentation transcript:

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  ml ml  Pietty  4

Log 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  … 5

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: chdoc/shell/book1.html 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 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 (return working directory name)  chmod [mode] [file] (change file modes) Mode : [user][group][guest] -rwxrwxrwx 1 user group 1 Sep test.txt Ex. chmod 644 test.txt -rw-r--r-- 1 user group 1 Sep test.txt 9

Unix-like command - Shell  Command  man man (format and display the on-line manual pages)  Other:  Reference: /history/linux/linux.tnc.edu.tw/techdoc/shell/book1. html /history/linux/linux.tnc.edu.tw/techdoc/shell/book1. html command.php command.php 10

ee/edit  BSD only  Start ee : % ee  Usage  edit mode like notepad  ESC-ENTER : save/exit 11

vi  Vi editor have two modes  Command mode  Edit mode  start vi : %vi Reference: chdoc/vi.htm Command mode Edit mode Exit Edit mode Insert Delete Replace Copy..... [Esc] 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() - example1 #include 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 */ } } % gcc fork1.c -o fork1 %./fork1 & [1] % my child is I am child! % ps PID TTY TIME CMD pts/18 00:00:00 tcsh pts/18 00:00:05 fork pts/18 00:00:05 fork pts/18 00:00:00 ps % killall -v fork1 Killed fork1(16444) with signal 15 Killed fork1(16445) with signal 15 [1] + Terminated./fork1 17

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 18 Process AProcess B Shared Memory

fork() + shared memory #include int main() { int ShmID, *ShmPTR; pid_t pid; ShmID = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT|0666); ShmPTR = (int *) shmat(ShmID, NULL, 0); pid = fork(); if (pid == -1) printf("failure!\n"); else if (pid == 0) for(;;) { if( ShmPTR[0] != 0 ) { printf("child get number = %d\n",ShmPTR[0]); exit(0); } else { srand(pid); ShmPTR[0] = rand()%10+1; printf("parent rand number = %d\n",ShmPTR[0]); } exit(0); } 19 % gcc fork2.c -o fork2 %./fork2 & [1] % parent rand = 5 child get number = 5 Fork failure Child process Parent process

Thread  Light weight process  Share resources  Own private data  Synchronization 20

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 21

How to create Pthread in unix-like OS?  Linux, BSD, Saloris…etc  Include: #include  Command line: % g++ threads.cpp -lpthread -o threads 22

pthread() – example1 #include #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); } void *PrintHello(void *threadid) { int tid = *((int *)threadid); printf("Hello World! thread #%d\n", tid); pthread_exit(NULL); } 23

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 24

pthread() – example2 #include using namespace std; void *doSomething(void * arg); 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); } void *doSomething(void *arg) { for (;;) { int tmp = *((int *)arg); cout << tmp; cout.flush(); sleep(tmp); } return NULL; } 25 % g++ threads2.cpp -o threads2 -lpthread %./thread …(loop)…

Assignment 26

1-1. use fork() and pthread  Create two global arrays with five elements  Just “rand()” all the elements between 1~10, and find the maximum  Print out all the elements and maximum of each array, and the maximum of the two arrays  Output sample:  array1: , max1=8  array2: , max2=7  maximum=8 27

1-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!) 28

1-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. 29

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) 30

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] …………….. 31

1-2 cont. #include #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); } 32

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  1-1-thread.c  1-2.c  STUDENT_ID.doc 33

Requirement cont.  Compress your file and named as STUDENT_ID.zip  Upload to the FTP:  host : caig.cs.nctu.edu.tw  port :  username : OS11  password : OS11 34

Q & A 35