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

Slides:



Advertisements
Similar presentations
#include void main() { float x = 1.66, y = 1.75; printf(%f%f,ceil(x), floor(y)); }
Advertisements

Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Pointers הרצאה קריטית. השאלות הפתוחות מה זה ה- & שמופיע ב scanf מדוע כשמעבירים מחרוזת ל scanf אין צורך ב & האם ניתן להכריז על מערך שגדלו אינו ידוע בתחילת.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
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.
תכנות תרגול 6 שבוע : תרגיל שורש של מספר מחושב לפי הסדרה הבאה : root 0 = 1 root n = root n-1 + a / root n-1 2 כאשר האיבר ה n של הסדרה הוא קירוב.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Some Example C Programs. These programs show how to use the exec function.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Thread Synchronization with Semaphores
Creating and Executing Processes
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Pipe-Related System Calls COS 431 University of Maine.
Pthreads.
Operating Systems Process Creation
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,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
Pipes Pipes are an inter-process communication mechanism that allow two or more processes to send information to each other.
Process Management Azzam Mourad COEN 346.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
CSCI 330 UNIX and Network Programming
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
CS431-cotter1 Linux Programming, Processes, and Threads Man pages (fork, clone, execve, pthread_create) The Linux Programming Interface – Kerrisk, No Starch,
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
OPERATING SYSTEMS 3 - PROCESSES PIETER HARTEL 1. Principle of concurrency - giving a process the illusion that it owns the whole machine  A process has:
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
שיתוף PDT בין חוטים PDT Thread A Process Descriptor File Object 1 File Object 2 File 1 File 2 pthread_create Thread B Process Descriptor ה PDT משותף לכל.
Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
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.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
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.
מספרים אקראיים ניתן לייצר מספרים אקראיים ע"י הפונקציה int rand(void);
Lesson One – Creating a thread
Fork VS. Threads Lab 05.
Thread Programming.
Chapter 4: Threads.
Fork and Exec Unix Model
Thread Programming.
Tutorial 3 Tutorial 3.
Unix System Calls and Posix Threads
Programming Assignment # 2 – Supplementary Discussion
Process Programming Interface
Default Arguments.
Tutorial: The Programming Interface
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 -
System Programming: Process Management
Presentation transcript:

Processes and threads

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

שכפול תהליך בעזרת fork #include 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); }

output pid is: child: 1 pid is: parent: 1

יצירת תתי תהליכים #include 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

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)

#include 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++); יצירת תתי תהליכים - סנכרון

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)