Docsity

Concurrency - Operating Systems - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Concurrency - Operating Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Operating Systems

Lecture 09:

Concurrency

QOD-part 1

#include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ void *runner(void param); / the thread / main(int argc, char argv[]) { pthread_t tidl, tid; / the thread identifier / pthread_attr_t attr; / set of thread attributes / if (argc != 2) { fprintf(stderr, ’’usage: a.out \n’’); exit(); } if (atoi(argv[1] < 0) { fprintf(stderr, ’’%d must be >= 0\n’’, atoi(argv[1])); exit(); } pthread_attr_init(&attr); / create the threads/ pthread_create(&tid,&attr,runner,argv[1]); pthread_create(&tidl,&attr,runner,argv[1]); pthread_join(tid,NULL); pthread_join(tidl,NULL); printf(sum = %d\n,sum); }

/* The thread will begin control in this function */ void *runner(void *param) { int upper = atoi(param); int i; sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); }

Code in Shared Memory

void echo()

{

chin = getchar(); chout = chin; putchar(chout);

}

  • Input read one keystroke at a time, sent to the display
  • Used by each app in a multiprogramming environment
  • Thus, makes sense to be in shared memory

Troubles?

Process P1 Process P

chin = getchar();.

. chin = getchar();

chout = chin; chout = chin;

putchar(chout);.

. putchar(chout);

Multiple Processes

• Operating System design is concerned

with the management of processes and

threads:

• Multiprogramming

• Multiprocessing

• Distributed Processing

Concurrency

Arises in Three Different Contexts:

Multiple Applications

invented to allow processing time to be shared among active applications

Structured Applications

extension of modular design and structured programming

Operating System Structure

OS themselves implemented as a set of processes or threads

Resource Competition

 Concurrent processes come into conflict when they

are competing for use of the same resource

 for example: I/O devices, memory, processor time, clock

In the case of competing processes three control problems must be faced:

  • the need for mutual exclusion
  • deadlock
  • starvation Docsity.com

11

Some Definitions

  • Race Conditions: two or more processes are reading and writing on shared data and the final result depends on who runs precisely when
  • Mutual exclusion : making sure that if one process is accessing a shared memory, the other will be excluded from doing it
  • Critical region: the part of the program where shared variables are accessed
  • Atomic operation: A sequence of one or more statements that appears to be indivisible; that is, no other process can see an intermediate state or interrupt the operation.

Mutual Exclusion

Figure 5.1 Illustration of Mutual Exclusion

Requirements for Mutual

Exclusion

  • Must be enforced
  • A process that halts must do so without interfering with other processes
  • No deadlock or starvation
  • A process must not be denied access to a critical section when there is no other process using it
  • No assumptions are made about relative process speeds or number of processes
  • A process remains inside its critical section for a finite time only

Mutual Exclusion: Idea

Hardware Support

  • uniprocessor system
  • disabling interrupts guarantees mutual exclusion
    • the efficiency of execution could be noticeably degraded
    • this approach will not work in a multiprocessor architecture

Mutual Exclusion: Idea

Special Machine Instructions

Hardware support, as well

  • Compare&Swap Instruction
    • a compare is made between a memory value and a test value
    • if the values are the same a swap occurs
    • carried out atomically
  • Exchange Instruction
    • Exchanges atomically the contents of a register with that of a memory location Docsity.com

Exchange Instruction

Special Machine Instruction:

Advantages

• Applicable to any number of processes on

either a single processor or multiple

processors sharing main memory

  • Simple and easy to verify
  • It can be used to support multiple critical

sections; each critical section can be defined

by its own variable