Race Conditions - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Race Conditions

Description:

Race conditions caused by programmer assuming that one thread will complete ... Consists of five elements: Protocol (must be the same for both client and server) ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 30
Provided by: phi140
Category:

less

Transcript and Presenter's Notes

Title: Race Conditions


1
Race Conditions
  • Errors that occur on a very infrequent basis.
  • Happen at random times so very difficult to
    isolate and fix.
  • Race conditions caused by programmer assuming
    that one thread will complete operation on data
    before another thread will access data.
  • That is, programmer makes assumptions as to the
    order in which threads are executed.

2
Assume global cxClient, cyClient LRESULT
CALLBACK WndProc(..) .. case
WM_SIZE cxClient LOWORD(lParam) cyClient
HIWORD(lParam) ..
3
In Thread Void thread1(PVOID pvoid) .
TextOut(buf, cxClient, cyClient/4 ..)
4
Possible Schedule
  • Main thread cxClient LOWORD(lParam)
  • Interrupted
  • Other thread TextOut(buf, cxClient, cyClient/4,
    )
  • Interrupted in middle of function call.
  • Main thread cyClient HIWORD(lParam)
  • Thread TextOut statement used new cx_Client and
    old value of cyClient. Inconsistent.

5
Solutions
  • Synchronization mechanisms been active area of
    research for last 30 years.
  • Structure your use of threads so that race
    conditions cannot develop.
  • Structure so that primary thread creates all
    windows, all window procedures, and processes all
    messages to window.
  • Thread then becomes background cruncher that
    does not handle user input.

6
Race Conditions
  • Threads share data and can be preempted at any
    point in their execution.
  • Race conditions caused by programmer assuming
    that one thread will complete operation on data
    before another thread will access data.
  • That is, programmer makes assumptions as to the
    order in which threads are executed.
  • Errors that occur on a very infrequent basis.
  • Happen at random times so very difficult to
    isolate and fix.

7
Race Conditions
  • //Global Variables
  • Int MyBalance 6000
  • VOID GetMoney(PVOID pvoid)
  • int widthdraw
  • widthdraw pvoid-gtamount
  • MyBalance - widthdraw
  • // load B widthdraw
  • // load A MyBalance
  • // sub A, B
  • // store AC MyBalance

8
LRESULT CALLBACK WndProc(..)
  • int Amount 1000
  • switch(msg)
  • case WM_CREATE
  • _beginthread(GetMoney, 0, Amount)
  • _beginthread(GetMoney, 0, Amount)

9
Possible Schedule
GetMoney Load B widthdraw //1000 Load A
MyBalance //6000 (interrupted) -------- --------
GetMoney
-------- --------

Load B widthdraw //1000 Load A MyBalance
//6000 (interrupted)
10
sub A, B // 5000 store AC MyBalance // 5000
Thread Exits
sub A, B // 5000 store AC MyBalance // 5000
Final Value of MyBalance is 5000
11
Solution Critical Sections

Protect sections where multiple threads access
and modify shared data.
widthdraw pvoid-gtamount MyBalance -
widthdraw
Do not want both threads executing in critical
section.
12
Critical Section Data Structure
  • CRITICAL_SECTION cs
  • EnterCriticalSection (cs)
  • ltcritical codegt
  • LeaveCriticalSection (cs)
  • Only one thread at a time will progress past
    EnterCriticalSection call.
  • Other threads will be blocked from entering until
    LeaveCriticalSection called

13
int MyBalance CRITICAL_SECTION cs
VOID GetMoney(PVOID pvoid) int widthdraw
widthdraw pvoid-gtamount
EnterCriticalSection(cs) MyBalance -
widthdraw LeaveCriticalSection(cs)
VOID GetMoney(PVOID pvoid) int widthdraw
widthdraw pvoid-gtamount
EnterCriticalSection(cs) MyBalance -
widthdraw LeaveCriticalSection(cs)
14
Questions
  • What two factors cause race conditions?
  • Why do threads share data?
  • Why are threads useful?
  • What data do threads share?
  • What data is private to a thread?

15
Networking in Windows
  • Each machine on Internet has unique IP Address
  • Most convenient to think of dotted notation
  • e.g. 216.47.84.122
  • Hosts communicate using a given communication
    protocol
  • Communication protocol is set of rules that
    determine how communication takes place between
    two network hosts.
  • Most common is TCP/IP protocol suite

16
Transmission Control Protocol
  • TCP (Transmission Control Protocol) provides
    reliable communication service
  • All data sent will arrive eventually (with
    certain caveats).
  • Data provided to application in strict sending
    order.
  • No duplicate data.
  • Referred to as connection-oriented protocol.

17
User Datagram Protocol
  • UDP is stateless or connectionless protocol.
  • Data sent may not be received.
  • Data may be duplicated.
  • Data can arrive in any order.

18
Internet Protocol
  • TCP and UDP execute on top of IP.
  • Internet Protocol
  • Addressing
  • Routing
  • Fragmentation and reassembly of packets.

19
Client/Server Model
  • Most often used paradigm
  • One host is the server to which clients
    connect.
  • Once connection established, both able to send
    and receive.
  • Application accesses network through socket.

20
Client Server Model
Subsequent Communications
Network
Client
Server
21
Sockets
  • Socket is an endpoint of communication.
  • Software construct that allows applications to
    access network.
  • In general, computer has one hardware network
    interface card (NIC).
  • May be shared by many sockets.
  • Socket Name consists of IP address and Port
    number.

22
Winsock
  • Windows networking library called winsock.
  • Windows Sockets
  • When client socket connects to a server socket
    form an association.
  • Consists of five elements
  • Protocol (must be the same for both client and
    server).
  • Client IP address.
  • Client Port number.
  • Server IP address.
  • Server Port number.

23
Network Program Sketch
  • Open a socket.
  • Name the socket.
  • Associate with another socket.
  • Send and receive messages between sockets.
  • Very similar to message passing between windows.
  • Close the socket.

24
Sockets
  • SOCKET sock
  • sock socket (AF_INET, SOCK_STREAM, 0)
  • SOCK_DGRAM for UDP.
  • AF_INET Internet family
  • SOCK_STREAM Indicates TCP protocol
  • Third value ignored for TCP and UDP.

25
Naming a Socket
  • Name consists of three attributes
  • Protocol
  • IP Address
  • Port Number
  • Generic Socket Address Structure
  • struct sockaddr
  • u_short as_family
  • char sa_data14 //Value depends on address
    family

26
Socket Address
  • struct sock_addr is generic. For TCP/IP use
  • struct sockaddr_in data structure.
  • struct sockaddr_in
  • short sin_family // always AF_INET
  • u_short sin_port // in network order
  • struct sin_addr // IP Address.

27
Port Number
  • IP Address not sufficient since may want multiple
    servers per host.
  • Differentiated with port number.
  • Port 0 1023 reserved for well known services
  • HTTP 80
  • FTP 23
  • Users can assign numbers 1025 5000.

28
IP Address
  • struct sin_addr component holds 32 bit IP Address
  • Access s_addr field of sin_addr.
  • Example (Almost correct)
  • struct sockaddr_in sa
  • sa.sin_family AF_INET
  • sa.sin_port 3000
  • sa.sin_addr.s_addr 32.33.14.55

29
Host/Network Order
  • sa.sin_port htons(3000)
  • htons converts short to network byte order.
  • sa.sin_addr.s_addr inet_addr( 32.33.14.55)
  • inet_addr takes string (dotted notation) and
    converts is to a long in network order.
Write a Comment
User Comments (0)
About PowerShow.com