Lecture 1 - Introduction To Concurrency: CS3211 Parallel and Concurrent Programming
Lecture 1 - Introduction To Concurrency: CS3211 Parallel and Concurrent Programming
Concurrency Parallelism
• Two or more tasks can start, run, and • Two or more tasks can run (execute)
complete in overlapping time periods simultaneously, at the exact same
• They might not be running (executing time
on CPU) at the same instant • Tasks do not only make progress, but
• Two or more execution flows make they also actually execute
progress at the same time by simultaneously
interleaving their executions or by
executing instructions (on CPU) at
exactly the same time
Exceptions Interrupts
• Executing a machine level instruction • External events can interrupt the
can cause exception execution of a program
• For example: Overflow, Underflow, • Usually hardware related: Timer,
Division by Zero, Illegal memory Mouse Movement, Keyboard Pressed
address, Mis-aligned memory access etc.
Synchronous Asynchronous
• Occur due to program execution • Occur independently of program
execution
• Have to execute an exception handler
• Have to execute an interrupt handler
• Taken from Operating System Concepts (7th Edition) by Silberschatz, Galvin & Gagne, published by Wiley
• 2 types of threads
• User-level threads
• Kernel threads
13 CS3211 L1 - Intro to Concurrency
POSIX Threads
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
• Threading overhead
• Stack
• Context switching
Library
OS
Hardware