.. _Extended2Processes:

.. raw:: html

   <script>ODSA.SETTINGS.DISP_MOD_COMP = true;ODSA.SETTINGS.MODULE_NAME = "Extended2Processes";ODSA.SETTINGS.MODULE_LONG_NAME = "Extended Example: Listing Files with Processes";ODSA.SETTINGS.MODULE_CHAPTER = "Processes and OS Basics"; ODSA.SETTINGS.BUILD_DATE = "2021-06-14 17:15:25"; ODSA.SETTINGS.BUILD_CMAP = false;JSAV_OPTIONS['lang']='en';JSAV_EXERCISE_OPTIONS['code']='java';</script>


.. |--| unicode:: U+2013   .. en dash
.. |---| unicode:: U+2014  .. em dash, trimming surrounding whitespace
   :trim:


.. This file is part of the OpenCSF eTextbook project. It was
.. auto-generated by scripts from the OpenDSA eTextbook project.
.. See https://OpenCSF.org for more details. OpenCSF is distributed
.. under a Creative Commons Attribution-NonCommercial 4.0 International
.. License (see http://creativecommons.org/licenses/by-nc/4.0/),
.. Copyright (c) 2019-2021 by Michael S. Kirkpatrick. OpenDSA is
.. distributed under an MIT open source license, Copyright (c) 2012-2021
.. by the OpenDSA Project Contributors.

.. avmetadata::
   :author: Michael S. Kirkpatrick
   :requires:
   :satisfies: Extended Example Processes
   :topic: Extended Example, Signal Handlers, Processes

Extended Example: Listing Files with Processes
==============================================

In this Extended Example, the main process starts by overwriting the ``SIGINT`` signal handler. The
process then enters a loop (line 29) where it repeatedly retrieves user input in the
``process_input()`` function. This function (lines 72 – 102) provides a simplified shell that
accepts two possible commands: ``ls`` and ``exit``. If the user enters the ``ls`` command, the main
process uses ``fork()`` and ``exec()`` to list files in the current directory (``list_files()`` in
lines 49 – 65), similar to how ``bash`` works. The ``exit`` command will terminate the program by
returning false from ``process_input()``. Finally, the overwritten ``SIGINT`` handler (lines 36 –
46) will also check for and ``kill()`` child processes that may have become stuck (e.g., if the
directory contains thousands of files).

.. codeinclude:: Processes/ExtEx-2.c
   :linenos: true

