Table of Content

We write a few simple example programs using system calls and APIs for Unix-like systems and Windows

Experiment and Programming Environment

Windows

Microsoft Visual Studio Community Edition is free to download and install. When setting it up, make sure you select and install Windows SDK and C/C++ compiler.

Linux

On Debian Linux, run the following command as root,

apt-get install build-essential manpages-dev

Example Programs

We write a CLI program that copies a file to another. In the following, we show an implemenation on Linux using Linux system calls, and that on Windows using Windows APIs. Both programs are in C++.

Example 1 The copyfile Program for Linux

  1. Open a terminal in the Linux system.
  2. In the Linux system, create the copyfile.cpp file using an editor, such as, nano or vi. The source code of the progrom is on the instructor’s Github repository.
  3. To compile the program, issue the following command,
    c++ copyfile.cpp -o copyfile
    
  4. To run the program, run the command below
    ./copyfile
    

    that shows the usage of the program. For instance, to copy file “copyfile.cpp” to “copyfile2.cpp”, we run,

    ./copyfile copyfile.cpp copyfile2.cpp
    
  5. To verify that copyfile.cpp and copyfile2.cpp are identical, run
    diff copyfile.cpp copyfile2.cpp
    

Example 2 The CopyFile Program for Windows

Although you can compile and run the program using the Visual Studio, we prefer to compile and run it from the command line as the program is a simple on.

  1. On the Windows system, locate the “Developer Command Prompt for VS”, and run it to open a Developer Command Prompt.
  2. On the Developer Command Prompt, change the working directory to the home directory
    cd %USERPROFILE%
    
  3. Create the CopyFile.cpp file using an editor, such as, notepad++ or notepad. The source code of the progrom is on the instructor’s Github repository.
  4. To compile the program, issue the following command,
    cl /W3 /EHsc CopyFile.cpp /FeCopyFile.exe
    
  5. To run the program, run the command below
    CopyFile
    

    that shows the usage of the program. For instance, to copy file “copyfile.cpp” to “copyfile2.cpp”, we run,

    CopyFile CopyFile.cpp CopyFile2.cpp
    
  6. To verify that CopyFile.cpp and CopyFile2.cpp are identical, run
    FC CopyFile.cpp CopyFile2.cpp
    

Exploration

Complete the following two tasks:

  1. Trace System Calls.

    Once you have correctly designed and tested the program, trace the system calls invoked by the program. Linux systems provide the strace utility with which we can do just that, e.g.,

    strace ./FileCopy
    
  2. Categorize System Calls

    Based on the output of strace, record all the system calls issued by your FileCopy program, and categorize these system calls in a table. The categories or the types can be Process control, Memory Management, File Management, Device Management, Information Maintenance, Communications, Protection, and so on.