180 likes | 333 Views
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.
E N D
Outline • Unix Architecture • Vi • Fork • Pthread
System Calls and Library Functions Application code User process C library functions System calls kernel
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]
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
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
Command Mode • :w save • :q exit • :q! exit and no save • :wq save and exit
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。
Thread • Light weight process • Share resources • Own private data • Synchronization
The Pthread API • Three major classes • Thread management • Mutex • Condition valuables All identifiers in the thread library begin with pthread_
Thread Management • pthread_create(thread,attr,start_routine,arg) • pthread_exit (status) • pthread_attr_init (attr) • pthread_attr_destroy (attr)
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.
#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); } }
#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); } } }