Lab Practice Processes
Lab Practice Processes
A. Basic terminal commands for processes (type man <command> for more info, e.g. man ps)
Live representation of existing processes with some text graphics and system info
top or htop
List the contents of the directory where Linux stores information about the running processes.
This is actually all the information the OS keeps in the memory about each process. There is one
directory for each process in the process table.
ls /proc
How to send a signal to a process (e.g., a signal to terminate). Signal is a type of IPC (Inter-
Process Communication)
kill <pid> (e.g., kill 325)
kill -9 <pid> (the hard way)
killall <process name> (terminates all processes with that name, e.g. killall firefox)
The following example creates a child process which displays the contents of the current directory (using
the program ‘ls’ and the parameter ‘l’ (list): ls -l). The parent process is waiting the child process to
terminate before it continues its operation. The child process doesn’t return anything to the parent.
Explain what will happen at the memory space of the child process once ‘exec’ is called.
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
return 0;
}
C. Lab Practice
Given the previous example, write a C program, called labProc.c, to do the following tasks:
At the beginning, print the parent process id.
Then, create a child to find all files with extension .txt in the current directory and print their
name on the screen
Create a second child (child of child) to find all files with extension .c in the the current directory
and print their name on the screen
Both children must print their process identifier before they terminate:
e.g., “The child with pid=123857 terminated”
The parent process must wait the two children to terminate and then print “Finished executing the
parent process”
– You can create some txt and c files to test your program.
– You can use the command find -name *.txt to find the required files and the execvp function for
exec.
– Use gcc to compile the code.
...
Parent pid=205285
This is the first child:
./lala.txt
The child with pid=205286 terminated
This is the second child:
./fork4.c
./fork2.c
./fork3.c
./wait.c
./fork1.c
./exercise_labprocesses.c
./exec-wait.c
The child with pid=205287 terminated
Finished executing the parent process