Link Search Menu Expand Document

System Programming (Pipes & Threads and more)

What is System Programming?

In any programming language, system programming is the process of developing computer system software. The purpose of system programming is to develop various software and software platforms that further help in providing support to other software. Usually, low-level programming languages are used to maintain the software in resource-constrained environments. There are few limitations also such as the unavailability of every programming facility, unique automatic garbage collection, and hardships in debugging.

System Programming in Python

The developers do System programming in Python by utilizing necessary modules made in Python for such purpose. Three of the most critical modules for such a scenario are the following:

  1. sys module
  2. os module
  3. platform module

Following is the example of importing such modules to develop software. Developers usually use PyScripter to write Python scripts for specific applications. It is better to delete those scripts before running PyInstaller, to lower the risk of possible interruption by automated generated files. An example of such code is the following:

import os, platform
print ( os.getcwd() )
os.system( ‘rm -rf *.db _pycache_/build/dist’)
print ( platform.system() )

Pipes in Python

To interact with the operating system, the os module in Python is helpful. The developers can portably use the functionalities that are dependent on the operating system by importing this module. The method os.pipe() provides help to utilize pipes in programming. This method allows the flow of information from one process to another. It also ensures proper delivery of information to the other end by holding the information in the system till the other end has read the passed information. The sample code of using pipes in Python is the following:

import os
r, w = os.pipe() #creating a pipe for read and write operations
pid = os.fork() #creating a child process
if pid > 0:
                os.close( r )
                message= b”This is parent process“
                os.write( w, message )
                print ( “Information sent:” , message.decode() )
else:
                os.close(w)
                r = os.fdopen( r )
                print(“Information read:” ,  r.read() )

Note: fork() is a system call that returns the process id or PID greater than 0 to the parent, and 0 to the child process. If it returns a negative number, it means the fork() was unsuccessful.

The output of the above code is the following:

Information sent: This is parent process
Information read: This is parent process

Similarly, any information is sent and received throughout the program with the help of pipes.

Threads in Python

In programming, the developers utilize threads when there is a need for simultaneously running multiple parts of a program. Additionally, the developers also use threads for the simplification of code and to save time by dividing the workload on multiple threads. Threads are provided with external input and are specified to perform a required task. Using threads in Python brings clarity to program design and makes it more comprehensible. The standard library of Python allows threading. The threading module provides the ability to write code for a separate threading process. Moreover, the users call start() function to start a new instance of any thread. A sample code for threading in Python is the following:

import threading
import time
def threading_function(parameter):
    for i in range(argument):
        print("thread is running")
        time.sleep(1)
if __name__ == "__main__":
    thread = threading.Thread(target = threading_function, argument = (10, ))
    thread.start()
    thread.join()
    print("finishing thread")

The above code shows a simple method to create a thread. The start() function initiates the thread instance. In Python, the join() function waits for a thread to stop working. While working with threads, sometimes the other thread has to wait for one thread to stop working, and then it continues its work. Therefore, join() is used to hold one thread till another is working on the data. Moreover, multi-threading in Python also helps in finding a quick solution to the problem by simultaneously executing various parts of the program.

Other useful articles:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy