1 / 8

Processes and threads

Processes and threads. תהליכים ותתי תהליכים. תהליכים כל תוכנית שרצה היא תהליך ( process ) ניתן להתפצל מתהליך אחד למספר תהליכים, בעזרת הפקודה fork() . הרבה יותר יעיל מבחינת זיכרון מהרצת אותה תוכנית מספר פעמים. לתהליכים אין זיכרון משותף. תתי תהליכים (threads) :

amish
Download Presentation

Processes and threads

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. Processes and threads

  2. תהליכים ותתי תהליכים • תהליכים • כל תוכנית שרצה היא תהליך (process) • ניתן להתפצל מתהליך אחד למספר תהליכים, בעזרת הפקודה fork(). • הרבה יותר יעיל מבחינת זיכרון מהרצת אותה תוכנית מספר פעמים. • לתהליכים אין זיכרון משותף. • תתי תהליכים (threads): • ניתן לפתוח תתי תהליכים מתוך תהליך בעזרת ספריית pthread.h • עשרות פונקציות לתיאום וסינכרון בין תתי תהליכים. • לתתי תהליכים יש גישה לזיכרון משותף :כולם מעדכנים משתנים משותפים.

  3. שכפול תהליך בעזרת fork #include <stdio.h> int main() { int i, j=0, pid; pid = fork(); printf("pid is: %d\n", getpid()); j++; if (pid==0) printf("child: %d\n", j); else printf("parent: %d\n",j); }

  4. output pid is: 20453 child: 1 pid is: 20452 parent: 1

  5. יצירת תתי תהליכים #include <stdio.h> #include <pthread.h> int g; void *threadFunc(void *arg){ /* The thread function. Like main(), but for a thread */ char *str; str=(char*) arg; for (int i = 0; i < 6; i++) printf("%s (g = %d)\n",str, g++); return NULL; } void main(void) { pthread_t pth; pthread_create(&pth,NULL,threadFunc,“I’m a thread..."); for (int i = 0; i < 10 ; ++I;} printf("main() is running...(g = %d)\n", g++); } NULL = default attributes for thread Thread identifier Thread function Argument to function

  6. output I'm a thread... (g = 0) I'm a thread... (g = 1) main() is running...(g = 2) I'm a thread... (g = 3) I'm a thread... (g = 4) I'm a thread... (g = 5) main() is running...(g = 6) I'm a thread... (g = 7) main() is running...(g = 8) main() is running...(g = 9) main() is running...(g = 10) main() is running...(g = 11)

  7. יצירת תתי תהליכים - סנכרון #include <stdio.h> #include <pthread.h> int g = 0; void *threadFunc(void *arg){ /* The thread function. Like main(), but for a thread */ char *str; str=(char*) arg; for (int i = 0; i < 6; i++) printf("%s (g = %d)\n",str, g++); return NULL; } void main(void) { pthread_t pth; // the thread identifier pthread_create(&pth,NULL,threadFunc,“I’m a thread..."); pthread_join(pth, NULL);/* wait for our thread to finish before continuing */ for (int i = 0; i < 10 ; ++I;} printf("main() is running...(g = %d)\n", g++);

  8. output I'm a thread... (g = 0) I'm a thread... (g = 1) I'm a thread... (g = 2) I'm a thread... (g = 3) I'm a thread... (g = 4) I'm a thread... (g = 5) main() is running...(g = 6) main() is running...(g = 7) main() is running...(g = 8) main() is running...(g = 9) main() is running...(g = 10) main() is running...(g = 11)

More Related