Professional Documents
Culture Documents
Basic Terms
• Multiprocessor, Uni-processor
• Asynchronous
– Two entities can proceed independently.
– Non blocking behavior
Multithreading in UNIX • Synchronous
– Two entities cannot proceed independently.
– Blocking behavior
• Parallelism
Harshad B. Prajapati – Concurrent sequences that proceed simultaneously.
– True parallelism can occur only on multiprocessor system, but concurrency can
Associate Professor occur on both uni-processor and multiprocessor systems
Information Technology Department, • Concurrency
– Things happening at the same time.
Dharmsinh Desai University, Nadiad – Appears to happen at the same time, could occur serially . (Illusion of
parallelism)
– Concurrent operations (of threads) may be interleaved.
1
7/21/2017
2
7/21/2017
3
7/21/2017
4
7/21/2017
Program Program
// File: threadDemo.c // File: threadDemo.c (continued)
#include <stdio.h>
#include <pthread.h> void* counter(void* param){
void* counter(void*); int* count= (int*)param;
int main(){
pthread_t tid1,tid2; int i=0;
int counter1=5; for(i=0;i<*count;i++)
int counter2=7;
pthread_create(&tid1,NULL,counter,(void*)&counter1); printf("Count=%d\n",i);
pthread_create(&tid2,NULL,counter,(void*)&counter2); pthread_exit(0);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL); }
}