Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.

Similar presentations


Presentation on theme: "Outline Unix Architecture Vi Fork Pthread. UNIX Architecture."— Presentation transcript:

1 Outline Unix Architecture Vi Fork Pthread

2 UNIX Architecture

3 System Calls and Library Functions Application code C library functions User process System calls kernel

4 vi

5 Vi editor have two modes –Command mode –Edit mode start vi : vi [filename] Command mode Edit mode Exit Edit mode Insert Delete Replace Copy..... [Esc]

6 Edit mode Insert –aInsert word after the cursor –iInsert word after the cursor –oCreate a new line below the cursor –OCreate a new line upon the cursor

7 Replace and Delete –x delete –ndwdelete n word –ndddelete n line –rreplace a word Undo –Uundo the changes in the same line –uundo the last action Edit –nY or nyycopy n line –P or ppaste –.redo the last action –/search

8 Command Mode –:wsave –:qexit –:q!exit and no save –:wqsave and exit

9 Fork

10 The main purpose is to duplicate the process 。 The new process created by fork is called child process 。 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 。

11 Pthread

12 Thread Light weight process Share resources Own private data Synchronization

13

14 The Pthread API Three major classes –Thread management –Mutex –Condition valuables All identifiers in the thread library begin with pthread_

15 Thread Management pthread_create(thread,attr,start_routine,arg)pthread_create pthread_exit (status)pthread_exit pthread_attr_init (attr)pthread_attr_init pthread_attr_destroy (attr)pthread_attr_destroy

16 Exit Condition The thread returns from its starting routine (the main routine for the initial thread). The thread makes a call to the pthread_exit subroutine. The thread is canceled by another thread via the pthread_cancel routine. The entire process is terminated due to a call to either the exec or exit subroutines.

17 #include int main() { pid_t pid; pid = fork(); if(pid<0) { printf("fork failed"); exit(1); } else if(pid==0) execlp("/bin/ls","ls","-al",NULL); else { wait(NULL); printf("Child Complete"); exit(0); }

18 #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { int tid; 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); if(rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); }


Download ppt "Outline Unix Architecture Vi Fork Pthread. UNIX Architecture."

Similar presentations


Ads by Google