Skip to main content
留学咨询

辅导案例-INFO1112

By May 15, 2020No Comments

INFO1112 Week 3 Tutorial Processes, Files and Shell Processes, files, and shell if statements When a program is running it is called a process and a Unix system will have over a hundred processes at any one time. Only a few will be directly started by users, the rest are system processes. Question 1: Process List Command We can discover what processes are running at any one time using the ps command. From last week we dabbled with the process command but we will revisit the command. Each process executed on the system is given a unique identifier – a process ID, or PID for short. The ps command can be used to list your (and others’) running processes. To see this we can use the sleep command, which will waits (or sleeps) for the given number of seconds. • Use the man command to read about the ps command • Use ps to list the users processes – what programs are shown? – what are their process ids? – what are the parent process ids? – explain why a process has a parent process? • Use ps to list all processes running in the system • Write a shell command pipeline to count the number of processes in the system 1 INFO1112 Processes, Files and Shell Question 2: Parent and child processes In a unix system, processes start other processes using the two system calls ” fork ” and ” exec ” (several variations). We can access these system calls from Python using the “os” module. This was demonstrated in the lecture. Python documentation is found at python.org – look at the Library documentation for “os” and “time”. • Write two python programs: parent.py and child.py, where parent.py uses the functions in the os module to start child.py running. Verify they are running with suitable printed messages. • Modify child.py so that it uses the sleep function of the “time” module to sleep for 60 seconds before exiting. Start the parent.py program asynchronously from the shell (use &)and then use ps to view the status of parent.py and child.py. What is the status? In Unix, when a process completes execution, it will return an integer result called an exit code which communicates to the shell whether the process has completed successfully, or has failed. The bash shell has an if statement that lets us test the exit code of a program. For example, the grep command will return 0 (“True”) if one or more lines match the pattern and 1 (“False”) if no lines match. As usual, see the manual entry for grep for more detail. • Write a shell script that uses an if statement to test if child.py is sleeping (use ps and grep in a pipeline) and print the message “Child is sleeping” if it is, “Child is not sleeping” if not. Question 3: Zombie Processes We can encounter a situation where a process finishes execution but has not been removed from the process table. You can execute the following code and observe the process table once you have started executing. import os import time pid = os.fork(); if pid > 0: time.sleep(40) else: exit(0) • What did you observe in the process table? • What could we do to deal with this process? OS and Network Platforms Page 2 of 4 INFO1112 Processes, Files and Shell Question 4: Kill a process You may encounter a situation when a pesky process wants to continue execute even though there may be a logical error. Typically the SIGINT signal is used to simply interrupt the process and shut it down gracefully, however, processes can handle a slew of signals within the system with the exception of SIGKILL. With the following program, launch it and send signals to the process using the kill command. $ kill You can use kill -l to get a list of signals available on your system. Unix-like operating systems provide an implementation of posix signals. These signals allow the kernel and other processes to communicate to a process. Within the context of the shell, they can be used for job control, Question 5: Process Control We can actually control our processes using signals by using SIGSTOP and SIGCONT. These allow us to stop execution of a process and resume it at a later time. Construct a shell script that will toggle the execution state of a process. If the process is currently running, this will send a signal to stop the process from executing (stopped state), if the process has been stopped, it will resume execution. Use bash command line arguments ($0, $1, … ) to receive the process id from the user. A running Unix system will have several file systems mounted. These file systems are either real data stores with conventional files that contain data, or services where the file system driver is providing information or performing services in response to file system access. We explored the files and directories in the /proc directory in lectures as an example of this behaviour. Example using the script: $ ./toggle_proc.sh 1112 pid 1112 has been stopped $ ./toggle_proc.sh 1112 pid 1112 has been resumed OS and Network Platforms Page 3 of 4 INFO1112 Processes, Files and Shell Question 6: Inspecting the filesystem We explored the files and directories in the /proc directory in lectures as an example of this be- haviour. • Use the mount command to find out how many file systems are mounted on your system. • How many of those are connected to mass storage devices? • What are the different types of file system? OS and Network Platforms Page 4 of 4

admin

Author admin

More posts by admin