Python stop and wait protocol using _thread module














































Python stop and wait protocol using _thread module



Description

Threads are sometimes called light-weight processes and they do not require much memory overhead; they are cheaper than processes.
A thread has a beginning, an execution sequence, and a conclusion. It has an instruction pointer that keeps track of where within its context is it currently running.

To learn about stop and wait protocol visit: https://www.javatpoint.com/stop-and-wait-protocol

In this article, I will show how to implement the stop and wait protocol using
_thread library.

Function definitions:
    sender: This function is meant to carry out operations for a sender thread.
    receiver: This function is responsible for carrying out operations for a receiver thread.
    file_create: This function is responsible for creating a text file with a random string
    which will be considered as the string which the sender will send to the receiver.

The code explanation is available after the code.

Source Code: https://github.com/Ishikashah2510/cppsecrets_articles

Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import _thread
import random
import string

flag = 1
data = ''
i = 0


def sender(a):
    global flag
    global data
    global i
    file_create()
    while(True):
        if data[i] == '1':
            break
        if flag == 1:
            print("Sending  : ", data[i])
            i += 1
            flag = 0


def receiver(a):
    global flag
    global i

    while(True):
        if flag == 0:
            print("Recieved : ", data[i-1])
            flag = 1
            if data[i] == '1':
                break


def file_create():
    file = open('prac4_input.txt', 'w')
    n = random.randint(10, 20)
    res = ''.join(random.choices(string.ascii_uppercase, k=n))
    res = res + '1'
    file.write(res)
    # print("hi")


if __name__ == '__main__':

    file_create()
    file = open("Prac4_input.txt", "r")
    data = file.readline()
    print("The data is : ", data)
    a = 0
    t1 = _thread.start_new_thread(sender, (a,))
    t2 = _thread.start_new_thread(receiver, (a,))

Explanation

The libraries needed for this implementation are threading, _thread, random,
    string.
    
    First I have defined the global variables which will be useful for all the functions.
    
    class all_not_recieved is a class derived from exception to define an exception where
    all the bytes arent sent to the receiver.
    
    Function:
        sender: Here, we append the data that is being sent. The character
        is sent one by one. The sender waits for the flag to become 1.
        
        receiver: Here, we check if character is received only after the flag becomes
        0. The receiver waits for the flag to become 0.
        
        
        file_create: A random string of capital letters of length between 10-30
        is generated and saved in a text file.
        
        main: We read the input data and allocate a lock and start the sender
        and receiver threads.

Output

The data is :  TJSSCDWKUHJU1
Sending  :  T
Recieved :  T
Sending  :  J
Recieved :  J
Sending  :  S
Recieved :  S
Sending  :  S
Recieved :  S
Sending  :  C
Recieved :  C
Sending  :  D
Recieved :  D
Sending  :  W
Recieved :  W
Sending  :  K
Recieved :  K
Sending  :  U
Recieved :  U
Sending  :  H
Recieved :  H
Sending  :  J
Recieved :  J
Sending  :  U
Recieved :  U

Observation
You can see in the output a sender thread sends a new character only
    after receiver receives the sent character.

Do like the article if it helps you!

More Articles of Ishika Shah:

Name Views Likes
Basics of Kotlin - 1 787 32
IntelliJ IDEA for Kotlin 552 52
Kotlin Playground 542 0
Introduction to Kotlin 517 0
Python Find Largest Subarray of equal number of 0s and 1s 533 0
Scrape NSE data using Selenium 1094 58
Post on Facebook using Selenium 1983 63
Python Curses Hangman Game 659 55
Python Curses Basics - 2 649 96
Python Text to Speech gTTS module 957 66
Python gaussian curve plot using _thread module 554 4
Python Curses Window Introduction - Part 2 560 32
Python curses number guess game 493 45
Python _thread deadlock illustration 435 14
Python-Curses-Basics-3 567 38
Python Curses Introduction - Part 1 454 66
Python-Curses-Basics-4 510 48
Python GBN protocol using _thread module 812 2
Python wave wave_write objects 740 40
Python concurrent.futures - replacing repititions 542 62
Python urllib with concurrent.future 562 47
Python-Curses-Basics-5 894 70
Python Curses Introduction - Part 3 511 24
Python concurrent.futures - Finding if given numbers are coprimes 523 34
Python-Curses-Basics-7 556 37
Python Curses Basics - 1 906 101
Python Curses Introduction - Part 2 504 9
Python using pandas for json 529 1
Python concurrent.futures - using methods of future objects 491 84
Python stop and wait protocol using _thread module 1708 1
Python _thread basic 456 1
Python concurrent.futures ThreadPoolExecuter Deadlock 697 30
Python wave reading all .wav files 471 60
Python quiz using json 1187 1
Python-Curses-Basics-8 417 48
Python wave copying wav files 738 14
Python concurrent.futures - when to use TPE and PPE? 531 25
Python wave wave_read objects 522 13
Python-Curses-Basics-6 575 46
python - simple calculator using _thread module 758 1
Python concurrent.futures - using map function 503 33
Writing and Reading Json data from a JSON file 510 1
Python concurrent.futures - digit to word using ProcessPoolExecutor 455 65
Python Speech To Text - Google API 569 34
Python wave introduction 556 14
Python magic square _thread module 474 36
JSON basics- Python 518 1
Python - timer using _thread module 509 1
Python sudoku generator with thread 824 76
Python Text file analyzer using _thread module 432 11
Python - stopwatch using _thread module 512 1
Python Curses Window Introduction - Part 1 475 34
Python Concurrent.futures with shutil 567 35
Reading JSON data from a given URL 432 1
python concurrent.futures introduction 519 11
Python Triangle area calculator using _thread 518 1

Comments