Tag » Semaphore

Art: Pouran Jinchi’s ‘Line of March’ at Third Line, Dubai

‘Roger, roger? Copy that.’ Artist’s exhibition at the Third Line Gallery plays on the martial associations of our games and the abstract potential of calligraphy, writes Nick Leech… 1.040 kata lagi

UAE

Cigarette smoker’s problem - Python code

Q. Write python code for Cigarette smoker’s problem.

Ans: Check the below Python3 code.

import threading
import random
import time

def generateRandomItems():
    item1 = random.randint(1,100)
    item2 = random.randint(1,100)
    item1 %= 3
    item2 %= 3
    if (item1 == item2):
        item2 += 1
        item2 %= 3
    itemList = 
    return itemList

class cigaretteSmoker:
    def __init__(self, rounds):
        self.condMutex = threading.Condition()
        self.vendorSleep = threading.Semaphore(0)
        self.rounds = rounds
        self.ingredients = ['TOBACCO', 'PAPER', 'MATCHES']
        # No item is available on table. 294 kata lagi

Thread safe Buffer Queue - Python code

Q. Implement Thread safe Buffer Queue in Python.

Ans: Check out below Python3 code.

Thread safe buffer queue using condition variable.

from ctypes import *
import threading
import random
import time

class buffer:
    def __init__(self, bufferLength=0, nextNode=None):
        if (0 == bufferLength):
            bufferLength = 1024
        self.data = bytes(bufferLength)
        self.next = nextNode
        self.bufferLength = bufferLength

    def getData(self):
        return self.data[:self.dataLength]

    def setData(self, data, dataLength):
        memmove(self.data, data, dataLength)
        self.dataLength = dataLength

    def getNext(self):
        return self.next

    def setNext(self, nextNode):
        self.next = nextNode

class bufferQueue:
    def __init__(self, length):
        self.head = None
        self.condMutex = threading.Condition()
        for i in range(length):
            newNode = buffer()
            newNode.setNext(self.head)
            self.head = newNode

    def enqueueBuffer(self, node):
        # Get the exclusive access of the buffer queue. 295 kata lagi

Old Bridge - Python code

Q. Write the python code to solve the Old Bridge problem.

Ans: Check out below Python3 code.

import threading
import random
import time

class oldBridge (threading.Thread):
    s_lockBridge = None
    s_traffic = 
    s_mutex = 
    s_cars = 
    s_entry = ['left', 'right']

    @staticmethod
    def initBridge():
        # Initialize bridge lock. 273 kata lagi

Mutex - Sermaphore - Deadlock

1. Mutex VS Semaphore
1 – giống nhau:

cả mutex và semaphore đều được sử dụng để quản lý việc truy xuất của những process tới 1 tài nguyên (thường là vùng nhớ) được shared bởi các process này. 597 kata lagi

Terms

Old Bridge

Q. An old bridge has only one lane and can only hold at most 3 cars at a time without risking collapse. Create a solution that controls traffic so that at any given time, there are at most 3 cars on the bridge, and all of them are going the same direction. 423 kata lagi

Clear semaphore memory

Sometimes I observed about using siteminder or other services semaphore memory even after service stopped remains into shared area.

This need to be cleared

e.g. if on unix machine your username is “digitaluser” then below is command… 14 kata lagi

Infrastructure