Fork VS. Threads Lab 05.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Threads Programming Thread creation Synchronization.
1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Chap. 23 Threads (1) Threads v.s. Fork (2) Basic Thread Functions #include int pthread_create ( pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void.
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.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
Thread Synchronization with Semaphores
Pthreads: A shared memory programming model
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Pthreads.
Operating Systems Process Creation
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Process Management Azzam Mourad COEN 346.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
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.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
Shell Execution Basic: fork, child execs, parent waits code of program in box –RC == return value from fork() Call fork RC=0 Call exec Subsequent instructions.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
回到第一頁 What are threads n Threads are often called "lightweight processes” n In the UNIX environment a thread: u Exists within a process and uses the process.
Tutorial 4. In this tutorial session we’ll see Threads.
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.
Lecture 4 Process, Thread and Task
סמפורים.
Threads in C Caryl Rahn.
Threads Threads.
Netprog: Threads Programming
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Pthreads void *sleeping(void *); /* thread routine */
Thread Programming.
Threads.
Linux Processes & Threads
Using Processes.
CSC 382: Computer Security
Concurrency I: Threads April 10, 2001
POSIX Threads 1. Background 2. Threads vs. Processes
Lecture 5: Process Creation
Realizing Concurrency using Posix Threads (pthreads)
Fork and Exec Unix Model
Realizing Concurrency using the thread model
Screen output // Definition and use of variables
Pointers & Functions.
Thread Programming.
Tutorial 3 Tutorial 3.
Pthread Prof. Ikjun Yeom TA – Mugyo
Simple Shell Due date: March 27, 2002.
The Environment of Unix Process
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 9: POSIX Threads Cont.
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Pointers & Functions.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

Fork VS. Threads Lab 05

اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً

Lab Objective To understand the deference between thread and fork.

The fork Function In computing, when a process forks, it creates a copy of itself, which is called a "child process." The original process is then called the "parent process“. pid_t fork(void); If successful, the fork function returns twice: On failure, the fork function returns once:

Thread When a program is started, a single thread is created, called the initial thread or main thread. Additional threads are created by: Returns 0 if OK, positive Exxx value on error int pthread_create (pthread_t * tid, const pthread_attr_t *attr, void *(*func) (void*), void *arg);

Thread pthread_t pthread_self(); void pthread_exit(); int pthread_join(pthread_t tid, void **status);

Practice In the following C++ program, the main process creates one thread of the function doit and forks one child. Both the doit function and the child code increment and display the global variable counter. Write, compile, and run the program. #include <iostream> #include "pthread.h“ using std::cout; using std::dec; using std::endl; #define NLOOP 10 int counter = 0; void * doit(void *) int main() { pthread_t tidA, tidB; pthread_create(&tidA, NULL, doit, NULL); pthread_create(&tidB, NULL, doit, NULL); //wait for both treads to terminate pthread_join(tidA, NULL); pthread_join(tidB, NULL); exit(0); }//end main void * doit(void *vprt) int i, val; for( i = 0; i<NLOOP; i++) val = counter; cout<<"Thread = "<<pthread_self(); cout<<" Counter = "<<dec<<counter<<endl; counter = val+1; } return (NULL);

#include <iostream> #include <stdlib.h> /* exit() */ #include <unistd.h> /* fork() */ #include <sys/types.h> /* pid_t */ #include <sys/wait.h> /* wait() */ #include "pthread.h" using namespace std; //Output a new line int counter = 0; //Incremented by the threads and child void * doit(void *); int main() { pthread_t tid; pid_t pid, cpid; int status; // Start the thread pthread_create(&tid, NULL, doit, NULL); //Delay between starting the thread and forking the child sleep(2); pid = fork(); //Fork the Child

if (pid < 0){ cout<<“Fork Failed\n”; exit(-1); } else if (pid == 0) { // child process sleep(2); counter++; cout << "Child Counter = " << counter << endl; else // parent process // parent will wait for the child to complete cpid = wait(&status); // wait for the thread to terminate pthread_join(tid, NULL); exit(0); } // End main void * doit(void *vptr) { sleep(1); cout << "Thread First Counter = " << ++counter << endl; sleep(5); cout << "Thread Second Counter = " << ++counter << endl; return(NULL);

Check Off What are the printed values of counter? Explain why counter gets these values from the child and the thread. Remove the sleep(2) line that delays between starting the thread and forking the child. Recompile the program and run it. What are the new printed values of counter? Explain why counter gets these values from the child and the thread. Thread 1st counter = 1 Child counter = 2 Thread 2nd counter =2 Child counter = 1

??? Any questions ??? 