Wednesday, February 5, 2014

INTRODUCTION TO UNIX AND UNIX LIKE SYSTEMS

WHAT IS UNIX?

The essentials of a UNIX or UNIX-like operating system consist of a 'kernel' and 'system programs'. In addition there also are the 'application program' for helping specific user tasks.

A kernel constiture the heart of the OS, and perform severl low level chores like keeping track of the files on disk, starting and running the programs, managing resources for the processes, communicate by exchanging packets of information with the network etc. Another way to look at Kernel is to think of it as an interface between the hardware and users. It prevents the direct access to hardware, and force the processes to do so using the tools (System Calls) provided by it.

The System Calls provided by Kernel are used by System Programs in order to implement various facilities provided by the operating system. A "mount" command is an example of the System program. The Application Programs can also user the System Calls but they are not System Programs because the intent here is to assist user in one of his tasks and not to get the system working.

The line between what is a system call and what is an application is not sharp and there are several programs which can be midway between the two.

The UNIX/UNIX-like OS provide several services some of which we shall list here:

  1. The "first" Process: init
  2. Logins from terminals: getty+login
  3. System Logs: syslog
  4. Periodic command executon: cron, at service
  5. Graphical User Interface: X Window System
  6. Networking Support
  7. Network Logins: telnet, rlogin, ssh
  8. Network File Systems: NFS (Supported by Kernel),CIFS
  9. Electronic mail, Printer Support.
Finally we have a USER MANUAL that comes installed automatically on every UNIX/LINUX machine and can be used for quick reference. Here is a summary of various sections of the LINUX MANUAL
  1. Executable programs or shell commands
  2. System calls (functions provided by the kernel)
  3. Library calls (functions within program libraries)
  4. Special files (usually found in /dev)
  5. File formats and conventions eg /etc/passwd
  6. Games
  7. Miscellaneous (including macro packages and conventions)
  8. System administration commands (usually only for root)
  9. Kernel routines [Non standard]

BASIC TERMINOLOGY OF UNIX SYSTEM PROGRAMMING

We shall follow a very concise approach with less talk and more programming. But before we begin it will be worthwhile to have a look at following terms:
  1. File: A near good definition of a file in UNIX system that that its an object residing either on external or internal memmory, that can be written to, or read from or both.
  2. File System: The file system refers to tree like hierarchy into which the files are organised in UNIX.
  3. Directories: The directories are special files that represent the nodes of the file-hierarchy and appear to the user as if they contain other files. But in reality it containscontain directory entries, which are the objects who associate a file to its filename - thus making an impression that actual files reside there.
  4. Processes: Although we shall discuss the idea of what a process is in detail, it shall suffice to note here that it is an instance of running a program. Each process has an 'id' number called "pid" as well as a 'parent-process-id' called "ppid", about both of which we shall talk more later.

Now that we have some idea of what involves in our endeavor to start with UNIX/LINUX system programming, its time to start!!

A "HELLO SYSTEMS WORLD" PROGRAM

Here's our first program which will print a simple message on to the terminal.
#include 
int main(int argc, char* argv[]){
  const char *buffer = "Hello Systems World !\n";
  //Write to console, 22 bytes from the buffer.
  write(1,buffer,22);
}

Output:
 Hello Systems World !
 
The main function defines a C string and then invokes "write(2)" function to print it on the screen. The function "write(2)" is provided by the library "unistd.h" and can be seen in the man pages as follows:
man 2 write
 
Some brief exercepts from man information are here:
Library:
    #include 
Prototype:
    ssize_t write(int fd, const void *buf, size_t count);
Description:
    write count bytes from buffer pointed to by buf, and
    write to the file associated with descriptor fd.
Return:
    On Success: number of bytes returned.
    On Error: returns -1, set 'errno'
 
Now its pretty easy to see whats happening in the program. We create a buffer to hold the message, pass it to write along with a number 22 (size of buffer) and the file descriptor of the terminal. And lo we have our first on screen print !!

SUMMARY

We have taken a brief tour of fundamental ideas that we need for starting enroute to system programming. We also saw a very simple application and learned about the use to "write" call. In the next post we shall have a longer look at the I/O operations and will learn about buffered/unbuffered modes of doing so.

No comments:

Post a Comment