1 / 18

Outline

Outline. Unix Architecture Vi Fork Pthread. UNIX Architecture. System Calls and Library Functions. Application code. User process. C library functions. System calls. kernel. vi. vi. Vi editor have two modes Command mode Edit mode start vi : vi [filename]. Command mode.

marcus
Download Presentation

Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Outline • Unix Architecture • Vi • Fork • Pthread

  2. UNIX Architecture

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

  4. vi

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

  6. Edit mode • Insert • a Insert word after the cursor • i Insert word after the cursor • o Create a new line below the cursor • O Create a new line upon the cursor

  7. Replace and Delete • x delete • ndw delete n word • ndd delete n line • r replace a word • Undo • U undo the changes in the same line • u undo the last action • Edit • nY or nyy copy n line • P or p paste • . redo the last action • / search

  8. Command Mode • :w save • :q exit • :q! exit and no save • :wq save 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. The Pthread API • Three major classes • Thread management • Mutex • Condition valuables All identifiers in the thread library begin with pthread_

  14. Thread Management • pthread_create(thread,attr,start_routine,arg) • pthread_exit (status) • pthread_attr_init (attr) • pthread_attr_destroy (attr)

  15. 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.

  16. #include<stdio.h> #include<unistd.h> 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); } }

  17. #include<pthread.h> #include<stdio.h> #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); } } }

More Related