In: Computer Science
4. PROPOSED SOLUTION (PSEUDOCODE)
• Start.
• Initialize the clear cycle=1 and part[2],generated=0.
• Declare the parts of the car as ‘engine’, ‘chassis’, ‘ss’.
• Initialize the semaphore sem t ready.
• Assign the ids from 0 to 3.
• Declare the function void * gen branch(void*arg) which generates
the assembly
branch.
• In the above function declare i,j,k=0 .
• Repeat the step 7 until while(1).
• Declare sleep(1) so that when the loop runs the next time it puts
threads to sleep for
1 sec.
• Declare sem wait( ) which will decrement the value pointed by the
semaphore when
greater than zero.
• So, sem wait(&ready) and if (clear cycle= =1) then
• i=k, increment the value of i by 1 and assign it to j.
• Assign k=j.
• Declare sem post( ) which increments the value pointed by the
semaphore.
• Print “ Parts are being moved”.
• Declare the function void*assembly(void*arg) which does the
assembly in
assembly branch.
• In the above function declare flag=1.
• Repeat the step 16 until while(1).
• Check if the generated branch is not the any two other
branches.
• Print “ Branch has completed the assembly”.
• Check if clear cycle==1 and flag= = 0 then print “ THE CYCLE IS
COMPLETE”.
• Declare sem init(&ready,0,1) which will initialize the
unnamed semaphore.
• Return 0 and stop.
please tell me the name of this algorithm
please tell me the name of the given above one.
The Above Pseudocode is the Algorithm of Semaphores in Operating Sytem in which particularly Binary semaphore algorithm (also called Mutex)
What is Semaphore ?
Semaphore is an simple variable algorithm which is used to solve critical section problems also called "Mutual Exclusion" and achieve the process synchronization in the "Multiprocessing Environment ". Multiprocessing Environment is an environment where we can perform multiple process in same time to utilize our "CPU" time properly which is most important in modern world of computers our work to be done in an efficient way.
"Critical Section Problem" which is know as there are some resources like "Registers" and "RAM" in our computer, These are important components for the process to be done for us in an efficient way.These components are expensive in cost and other features. So they are limited in our computers, so we need to use them properly to achieve Multiprocessing in our computer. Assume that there is program has executing in particular time, In Process(Execution of program) it need one resource which is like variable which holds the particular memory location for other part of program, so our computer make the "Variable" as a "Critical Section" (Where only process can be executed in particular time ) and use the Resource efficiently to execute the program.
Properties of Semaphores are
To achieve Multiprocessing by the Critical Section we use Algorithms like Semaphores and Monitors.
Semaphores are two types Binary Semaphore and Counting Semaphore. This Semaphores has two functions like SemWait and SemIncrement
** In the above Pesudocode we use Binary Semaphore which is also called Mutex to achieve process synchronization in multiprocessing with SemWait and SemPost(SemIncrement) functions to achieve critical section Which is the Generating Car By using Car parts.
Small Description about Functions in above code are
In lines 1 to 3 we are declaring the required variables to generate a car in line we are initializing Semaphores 0 to 3 with "Sem t " ready function which is the function where the process start and cycle to check one car is completely generated or not.
And Sem Wait() function in line 10 where the process is in waiting position where other process(one car is being generated)
And SemPost() function in line 14 where the process of generating the car by using the car parts (Resources ) is being done
Function void * gen Branch function to create sub process in the program to create cycle of generating car.
Function void * gen Assembly function in the critical section which is the one which assembles the car parts and make cycle complete when the cycle is complete Check if clear cycle==1 and flag= = 0 then print “ THE CYCLE IS COMPLETE”. is being executed and make other semaphore init ready function to perform another cycle of car assembly will be done.
This process is done by using the same resources for particular time to generate desired output for the customer. This is called process synchronization.