Sie sind auf Seite 1von 18

1

BIL 244 Systems Programming

Erkan Zergeroglu, Ph.D

Office : 202 Computer Engineering Department


Email : ezerger@bilmuh.gyte.edu.tr

BIL 244 – System Programming


2

Introduction

What is System Programming ?


• Computer systems are composed of Hardware and Programs running on
hardware (Software). On most systems, most of the time, it is the Software that has
to be upgraded to enhance the usability of the Hardware!!! So it is crutial to learn
how to
• Talk to the Operating Sistem (O/S)
• Access Hardware
• Do low level programming

So in this Course we will

• Look at the Standard C Library and how we can use it


• Look at Standards for O/S level programming
• Look at how the theory of O/Ss is applied to the practice of actual
systems programming

BIL 244 – System Programming


3

Topics Covered in this Course

• This course covers the System Services


– The brains of OS
– System Services enable everything else
• Topics NOT covered
– Device Drivers
– OS internals
– Graphical User Interface (GUI) programming
– COM, DCOM, and MFC
– MS Developer Studio, .NET or any other non standard programming
environment

BIL 244 – System Programming


4

Course Outline

1. Getting Started (This Week)


2. Programs Processes and Threads (Ch2)
3. Processes in UNIX (Ch3)
4. Unix I/O (Ch4)
5. Files and Directories (Ch5)
6. UNIX Special Files (Ch6)
7. Signals, Times and Timers (Ch8-Ch9)
8. Posix Threads (Ch12)
9. Thread Synchronization (Ch13)
10. Critical Sections and Semaphores (Ch14)
11. Posix Inter-process Communication (Ch15)
12. Sockets and Network Programming (??)
13. Asynchronous I/O (??)
BIL 244 – System Programming
5

OVERVIEW Philosophy- Simpler is Better

Basic Rules of Programming

1. Make each program do one thing well. To do a new job, build afresh rather
than complicate old programs by adding new features.
2. Expect the output of every program to become the input to another, as yet
unknown program. Don’t clutter output with extraneous information. Avoid
stringently columnar of binary input formats. Don’t insist on interactive input.
3. Design and build software, even operating systems, to be tried early, ideally
within weeks. Don’t hesitate to throw away the clumsy parts and rebuild
them.
4. Use tools in preference to unskilled help to lighten a programming task,
even if you have to detour to build the tools and expect to throw some of
them out after you’ve finished using them.

BIL 244 – System Programming


6

What I will be looking at in a Program ?

• Modularity: Write simple parts connected by clean interfaces


• Clarity: Clarity is better than cleverness
• Composition: Design programs to be connected to other programs
• Separation: Separate policy from mechanism; separate interfaces
from engines
• Simplicity: Design for simplicity; add complexity only where you
must
• Parsimony : Write a big program only when it is clear by
demonstration that nothing else will do
• Transparency: Design for visibility to make inspection and
debugging easier
• Representation: Fold knowledge into data so program logic can be
stupid and robust

BIL 244 – System Programming


7

POSIX

• POSIX is a set of standard operating system interfaces


based on the Unix operating system
• The need for standardisation arose because of enterprises
using computers wanted to be able to develop programs
that could be used on different manufacturers computers
without being re-coded
• Unix was selected as the basis for a standard system
interface partly because it was “manufacturer neutral”
• However, several major versions of Unix existed so there
was a need to develop a common denominator system
• Informally, each standard in the POSIX set is defined by a
decimal following the POSIX

BIL 244 – System Programming


POSIX : Portable Operating System Interchange

• Thus POSIX.1 is the standard for an application program


interface in the C language.
• POSIX.2 is the standard shell and utility interface (i.e. the users
command interface with the operating system)
• These are the main POSIX interfaces however there are others
such as POSIX.4 for thread management
• These standards are now followed for most Unix / Linux
versions and many components of Windows are now POSIX
compliant

BIL 244 – System Programming


9

Sample Program
Simple file copy example

• Before writing a program


– Have a thorough understanding of problem
– Carefully plan your approach for solving it
• While writing a program
– Know what “building blocks” are available
– Use good programming principles

BIL 244 – System Programming


/* Basic cp file copy program. Standard C library Implementation. */
/* cp file1 file2: Copy file1 to file2. */

#include <stdio.h>
#include <errno.h>
#define BUF_SIZE 256

int main (int argc, char *argv [])


{
FILE *in_file, *out_file;
char rec [BUF_SIZE];
size_t bytes_in, bytes_out;

if (argc != 3) {
printf ("Usage: cp file1 file2\n“ );
return 1;
}
in_file = fopen (argv [1], "rb“);
if (in_file == NULL) {
perror (argv [1]);
return 2;
}
out_file = fopen (argv [2], "wb");
if (out_file == NULL) {
perror (argv [2]);
return 3;
}

Standard C Implementation
/* Process the input file a record at a time. */

while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file)) > 0) {


bytes_out = fwrite (rec, 1, bytes_in, out_file);
if (bytes_out != bytes_in) {
perror ("Fatal write error.");
return 4;
}
}

fclose (in_file);
fclose (out_file);
return 0;
}

Standard C Implementation
Naming Conventions

• Long and descriptive


– WaitForSingleObject WaitForMultipleObjects

• Predefined descriptive data types in upper case


– BOOL, DWORD, LPDWORD, ...
• Predefined types avoid the * operator and make
distinctions:
– LPTSTR (defined as TCHAR *) and
– LPCTSTR (defined as const TCHAR *)

• Variable names in API descriptions use


“Hungarian” notation -
– lpFileName — long pointer [to a zero terminated string]

BIL 244 – System Programming


/* Basic cp file copy program. Implementation */
/* cp file1 file2: Copy file1 to file2. */

#include <stdio.h>
#include <errno.h>
#define BUF_SIZE 256

int main (int argc, char *argv [])


{
FILE *fPtrInFile, *fPtrOutFile;
char chRec [BUF_SIZE];
size_t bytesIn, bytesOut;

if (argc != 3) {
printf ("Usage: cp file1 file2\n“ );
return 1;
}

fPtrInFile = fopen (argv [1], "rb“);


if (fPtrInFile == NULL) {
perror (argv [1]);
return 2;
}
fPtrOutFile = fopen (argv [2], "wb");
if (fPtrOutFile == NULL) {
perror (argv [2]);
return 3;
}

I can do better !!
/* Process the input file a record at a time. */

while ((bytesIn = fread (rec, 1, BUF_SIZE, fPtrInFile)) > 0) {


bytesOut = fwrite (chRec, 1, bytesIn, fPtrOutFile);
if (bytesOut != bytesIn) {
perror ("Fatal write error");
return 4;
}
}

fclose (fPtrInFile);
fclose (fPtrOutFile);
return 0;
}

I can do Better !!
/* Basic cp file copy program.
Win32 Implementation using CopyFile for convenience and possible performance. */
/* cp file1 file2: Copy file1 to file2. */

#include <windows.h>
#include <stdio.h>
#define BUF_SIZE 256

int main (int argc, LPTSTR argv [])


{
if (argc != 3) {
printf ("Usage: cp file1 file2\n");
return 1;
}
if (!CopyFile (argv [1], argv [2], FALSE)) {
printf ("CopyFile Error: %x\n", GetLastError ());
return 2;
}
return 0;
}

Win32 Implementation using CopyFile


The Three Main Tools

• Shell
– Also referred as a terminal, or a console, is a program that allows the
user tun other programs
– Once started the shell informs the use that it is waiting for its next
comment via prompt. On most system its like username at a computer
like
erkan@BIL244LabTop>
– Some of the commonly used shell common shell programs are
Command Description
grep Search files for specific text
ls List files and their attributes
man Display manual (help) for command/program (if available)
more Display a text using pausable sorting
time Measure a running program
sort Sort lines in a text file

BIL 244 – System Programming


The Three Main Tools

• Text Editor

– Basic operation of a text editor is allow a user to write/read


and edit code, save it to a file and load it when needed.
– Find one you can easily use and make sure it is standard on
most systems
– “vi” is one of the oldest editor in Unix environment. I use it,
it might look complicated at first but when you get familiar
to it, you will like it ☺

BIL 244 – System Programming


The Three Main Tools

• Compiler and Debugger


– Complier
• The standard compiler in most Unix/Linux systems is GNU-C
which is usually executed as “gcc”
– Debugger
• Is (in my opinion) the most important tool for the system
programmer
• Allows the programmer to observe the execution of the program,
pausing it while it runs, in order ro examine the values of variables.
• It allows the programmer to step through a program executing it
one line at a time
• The debugger itself is a program and like text editor there are many
versions of it. The GNU-C debugger is executed by “gdb”

BIL 244 – System Programming

Das könnte Ihnen auch gefallen