0% found this document useful (0 votes)
28 views

Lab Practice Processes

The document describes a lab practice on processes in operating systems. It includes: 1) Basic terminal commands to work with processes like ps, top, pstree, ls /proc, and kill. 2) An example C program using fork, exec, and wait to create a child process to run ls -l. 3) An assignment to write a C program called labProc.c that creates two child processes - one to find .txt files and another to find .c files, and has the parent process wait for both children to finish.

Uploaded by

Dokin Nikod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lab Practice Processes

The document describes a lab practice on processes in operating systems. It includes: 1) Basic terminal commands to work with processes like ps, top, pstree, ls /proc, and kill. 2) An example C program using fork, exec, and wait to create a child process to run ls -l. 3) An assignment to write a C program called labProc.c that creates two child processes - one to find .txt files and another to find .c files, and has the parent process wait for both children to finish.

Uploaded by

Dokin Nikod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

[CSCI 332 - Spring 2023] Operating Systems

Lab Practice (Processes)


NOTE: You don’t have to submit, but questions in the exams may come from this lab.

A. Basic terminal commands for processes (type man <command> for more info, e.g. man ps)

 Get a snapshot of the current processes in the system (process table).


Which process has pid = 1?
ps or ps -el or ps -ax

 Live representation of existing processes with some text graphics and system info
top or htop

 Display the tree of existing processes.


pstree

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)

B. Example with fork, exec, and wait

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>

int main( void ) {


char *argv[3] = {"Command", "-l", NULL};
int p = fork();
if ( p == 0 ) {
execvp( "/bin/ls", argv );
}else{
wait( NULL );
printf( "Finished executing the parent process\n");
}

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.

The first lines of the code may be as follows:


...

int main( void ) {


printf("Parent pid=%i\n", getpid());
char *argv1[3] = {"Command", "-name", "*.txt"};
char *argv2[3] = {"Command", "-name", "*.c"};

...

Your output should look similar to the following:

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

You might also like