We use lock to keep our valuables safe from thieves or mischief. Lock to door is for the valuables in house. Lock to bank locker is to keep gold or documents safe. Lock/password to an account is to keep is private. In the same manner, multiple processes requires lock to keep shared memory addresses private for particular operations. Lock will ensure that only one process will perform operation on it and others may not even access its value. Consider a bank software with two process. One is withdrawal and another is deposition. If both the processes run parallel with balance as shared memory address then, the value will of balance at the end of both processes will be wrong. To do this lock is applied to shared memory during each process. import multiprocessing ... lock = multiprocessing.Lock( ) ... lock.acquire( ) ... lock.release( ) ... Acquire is the function to lock the shared memory for one particular process for that moment. Release function will release the lock and this memory address is free to be accessed by another process.
Github Link
0 Comments
As we already know, each process has its own memory allocation. So, if a variable is declared in one process, then we can't update it in another process. Even if we declare a variable as global variable, it will be defined only for a process. Suppose main program is process 1 and calculation of squares of given numbers is the process 2. An empty list named result is declared in both the processes. If we append the squares of numbers to result list in process 2 then, it will not update the result list declared in process 1 as both of these lists have different memory addresses. So, we will create a shared memory which can be used by both the processes. Below is the syntax to define shared Array, Value and Queue. import multiprocessing ... arr = multiprocessing.Array( data_type, size ) val = multiprocessing.Value( data_type, numerical_value ) q = multiprocessing.Queue( ) ... All these memories can be shared in another process by passing them as argument in the process. Here updating the value of these variables in any process will update their value in other process too.
To understand the use of this concept check links below: github link 1 github link 2 Multi-processing is used to do multiple tasks at a time in computer. It speeds up the process. We already have seen the difference between multiprocessing and threading. So, here we will study about how begin with multiprocessing. Python contains a multiprocessing module. This module has various functions available to create a process. Below is a piece of code to create a process. import multiprocessing ... p = multiprocessing.Process( target = function_name, args = (arg1,arg2,...)) p.start() p.join() ... Process is a function used to create a process. It has the target function which will be executed as the process begins and arguments for the target function as its arguments. Start() function will initiate the process and Join() function will wait till the process is finished.
To understand in a better way, visit the github link. Both are the ways of multi-tasking. Computer can do multiple tasks at a time. Just like running google chrome, VLC media player and python program at same time is multi-tasking. Each of these is known as a process. These processes can be seen in windows task manager (in Windows) and in terminal (in Linux). Each process has a process id known as PID. Each process has its own memory location. Each thread acts within a process. There can be multiple threads within a single process. The memory address is shared by each thread. If a variable is globally declared in a process, then each thread can access this variable. The above image makes the concept more clear. On the other hand, processes has their own address space. They need inter-process communication techniques to communicate with each other. Globally declared variable for one process can't be accessed by other process. The above image will make it more clear. File, shared memory & message pipes are some examples of inter-process communication techniques. Advantages of Process over Threading:
Error or memory leak in one process won't hurt execution of another process. But if there is memory leak in one thread then it can damage other threads. Hence every software creates its own process. So, if there is any failure in one software, it will not harm another soft wares in computer. |
AuthorVinit is a computer scientist and roboticist. His research focuses on making machines intelligent. Categories |