In: Computer Science
Task: there are mainly five types of system calls: Process
Control, File Management, Device
Management, Information Maintenance and Communication. Please write
two python programs
to implement the above two types of system calls.
Lets us first look at some terms that will help us understand system calls better and how they are executed.
Kernel - The kernel is the heart of a computer's operating system. It is in charge of everything that a computer executes such as processes, files, display, memory management, etc.
A System call is a call to the kernel to signal it for a service or a resource. System calls are of many types like process, file, information, communication, and device.
Process Control - Process control refers to the procedure of creating, handling, and terminating a process by a system call to the kernel.
Process control is implemented in python by a package called the OS which has access privileges to communicate with the kernel itself and fork new child process from parent processes. Either we can use the os.fork() to fork a child process from the parent OS process itself or use a package called multiprocessing to create multiple child processes.
Let's look at a code implementation in python. The code is commented for better understanding.
import os
# multiprocessing package
import multiprocessing
# function process to depict process management
def process():
# printing process id
print("ID of child process: {}".format(os.getpid()))
#test function in main
if __name__ == "__main__":
# parent process id
print("ID of main process: {}".format(os.getpid()))
# creating child processes
child = multiprocessing.Process(target=process)
child_2 = multiprocessing.Process(target=process)
child_3 = multiprocessing.Process(target=process)
# starting child processes
child.start()
child_2.start()
child_3.start()
#child process IDs
print("ID of child process : {}".format(child.pid))
print("ID of child_2 process : {}".format(child_2.pid))
print("ID of child_3 process : {}".format(child_3.pid))
# wait until processes are finished
child.join()
child_2.join()
child_3.join()
(2)
A File management system call to the kernel is used to open, read, create and modify files. Files management is an integral part of any operating system.
Let's implement a basic file management code in python that opens a file, modify it and writes to a new file by creating another file.
#open a file in read mode
file = open('cars.txt','r')
#for each line in file read
for lines in file:
print(lines)
#open a new file in read/write/create mode
new_file = open('new_file.txt','x')
#write to the new file
new_file.write("bye")
#close the file
file.close()