Monday 27 January 2014

Operating System Functions

Operating System Functions :
·         Process Management
·         Memory Management
·         Storage Management
·         Mass-Storage Management
·         I/O Subsystem
·         Protection and Security
Process Management
Ø  A process is a program in execution. It is a unit of work within the system. Program is a passive entity, process is an active entity.
Ø  Process needs resources to accomplish its task
o   CPU, memory, I/O, files
o   Initialization data
Ø  Process termination requires reclaim of any reusable resources
Ø  Single-threaded process has one program counter specifying location of next instruction to execute
o   Process executes instructions sequentially, one at a time, until completion
Ø  Multi-threaded process has one program counter per thread
Ø  Typically system has many processes, some user, some operating system running concurrently on one or more CPUs
o   Concurrency by multiplexing the CPUs among the processes / threads
Ø  The operating system is responsible for the following activities in  connection with process management:
Ø  Creating and deleting both user and system processes
Ø  Suspending and resuming processes
Ø  Providing mechanisms for process synchronization
Ø  Providing mechanisms for process communication
Ø  Providing mechanisms for deadlock handling
Ø  Memory Management
Ø  All data in memory before and after processing
Ø  All instructions in memory in order to execute
Ø  Memory management determines what is in memory when
o   Optimizing CPU utilization and computer response to users
Ø  Memory management activities
o   Keeping track of which parts of memory are currently being used and by whom
o   Deciding which processes (or parts thereof) and data to move into and out of memory
o   Allocating and deallocating memory space as needed
Ø  Storage Management
Ø  OS provides uniform, logical view of information storage
o   Abstracts physical properties to logical storage unit  - file
o   Each medium is controlled by device (i.e., disk drive, tape drive)
§  Varying properties include access speed, capacity, data-transfer rate, access method (sequential or random)
Ø  File-System management
o   Files usually organized into directories
o   Access control on most systems to determine who can access what
o   OS activities include
§  Creating and deleting files and directories
§  Primitives to manipulate files and dirs
§  Mapping files onto secondary storage
§  Backup files onto stable (non-volatile) storage media
Ø  Mass-Storage Management
Ø  Usually disks used to store data that does not fit in main memory or data that must be kept for a “long” period of time.
Ø  Proper management is of central importance
Ø  Entire speed of computer operation hinges on disk subsystem and its algorithms
Ø  OS activities
o   Free-space management
o   Storage allocation
o   Disk scheduling
Ø  Some storage need not be fast
o   Tertiary storage includes optical storage, magnetic tape
o   Still must be managed
o   Varies between WORM (write-once, read-many-times) and RW (read-write)
Ø  I/O Subsystem
Ø  One purpose of OS is to hide peculiarities of hardware devices from the user
Ø  I/O subsystem responsible for
o   Memory management of I/O including buffering (storing data temporarily while it is being transferred), caching (storing parts of data in faster storage for performance), spooling (the overlapping of output of one job with input of other jobs)
o   General device-driver interface
o   Drivers for specific hardware devices
Ø  Protection and Security
Ø  Protection – any mechanism for controlling access of processes or users to resources defined by the OS
Ø  Security – defense of the system against internal and external attacks
o   Huge range, including denial-of-service, worms, viruses, identity theft, theft of service
Ø  Systems generally first distinguish among users, to determine who can do what
o   User identities (user IDs, security IDs) include name and associated number, one per user
o   User ID then associated with all files, processes of that user to determine access control
o   Group identifier (group ID) allows set of users to be defined and controls managed, then also associated with each process, file
o   Privilege escalation allows user to change to effective ID with more rights

Operating System Definition

What is an Operating System?
Ø  A program that acts as an intermediary between a user of a computer and the computer hardware.
Ø  Operating system goals:
o   Execute user programs and make solving user problems easier.
o   Make the computer system convenient to use.
Ø  Use the computer hardware in an efficient manner.
Ø  Computer system can be divided into four components
o   Hardware – provides basic computing resources
§  CPU, memory, I/O devices
o   Operating system
§  Controls and coordinates use of hardware among various applications and users
o   Application programs – define the ways in which the system resources are used to solve the computing problems of the users
§  Word processors, compilers, web browsers, database systems, video games
o   Users
§  People, machines, other computers
Ø  OS is a resource allocator
o   Manages all resources
o   Decides between conflicting requests for efficient and fair resource use
Ø  OS is a control program
o   Controls execution of programs to prevent errors and improper use of the computer

Structures Vs Unions

Structure                                       Union

(1) Each member has its own storage    All the members use the same location.
      location
(2) Structure can be declared using the          Union can be declared using the
      keyword struct                                                keyword union
(3) Members of structures can be read     Members of unions cannot be
     with the help of single ‘scanf’                     read all at once with the help
     statement.                                                 of single ‘scanf’ statement.
(4) struct tag-name                                       union tag-name
          {                                                {
                 Data-type member1;                       Data-type member 1;
      Data-type member 2;              Data-type member 2;

      ……………………                          ………..
 };                                             };
5) Structure members can be initialized           Only first member of the union can be
     at the time of declaration                  initialized at the time of declaration
    struct st-record student1=                union item abc= {100};
                             {60,180.75};                

Sunday 26 January 2014

Character strings & String functions

Character Strings :

 

A string is a sequence of characters. Any sequence or set of characters defined within double quotation symbols is a constant string.

 

 Various operations on strings:

Reading string displaying strings

Combining or concatenating strings

Copying one string to another.

Comparing string & checking whether they are equal

Extraction of a portion of a string

 

Strings are stored in memory as ASCII codes of characters that make up the string appended with ‘\0’(ASCII value of null). Normally each character is stored in one byte, successive characters are stored in successive bytes.

                 The last character is the null character having ASCII value zero.

 

Initializing Strings:

Following the discussion on characters arrays, the initialization of a string must the following form which is simpler to one dimension array.

 

char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};

 

J

A

N

U

A

R

Y

\0


 

/*String.c string variable*/

#include < stdio.h >

main()

{

char month[15];

printf (“Enter the string”);

gets (month);

printf (“The string entered is %s”, month);

} 

 

Character string terminated by a null character ‘\0’.

A string variable is any valid C variable name & is always declared as an array. The general form of declaration of a string variable is

 

Char string_name[size];

 

The size determines the number of characters in the string name.

 

Example:

 

char month[10];

char address[100];

 

The size of the array should be one byte more than the actual space occupied by the string since the complier appends a null character at the end of the string.

 

Reading Strings from the terminal:

The function scanf with %s format specification is needed to read the character string from the terminal.

 

Example:

 

char address[15];

scanf(“%s”,address);

 

Scanf statement terminates the statement as soon as it finds a blank space, suppose if we type the string new york then only the string new will be read and since there is a blank space after word “new” it will terminate the string.

 

Note that we can use the scanf without the ampersand symbol before the variable name.

In many applications it is required to process text by reading an entire line of text from the terminal.

 

The function getchar can be used repeatedly to read a sequence of successive single characters and store it in the array.

 

We cannot manipulate strings since C does not provide any operators for string. For instance we cannot assign one string to another directly.

 

For example:

 

String=”xyz”;   not valid.

String1=string2;   not valid.

 

To copy the chars in one string to another string we may do so on a character to character basis.

 

Writing strings to screen:

The printf statement along with format specifier %s to print strings on to the screen. The format %s can be used to display an array of characters that is terminated by the null character

For example: printf(“%s”,name); can be used to display the entire contents of the array name.

 

String operations (string.h)
C language recognizes that string is a different class of array by letting us input and output the array as a unit and are terminated by null character. C library supports a large number of string handling functions that can be used to array out many o f the string manipulations such as:

·         Length (number of characters in the string).
·         Concatenation (adding two are more strings)
·         Comparing two strings.
·         Substring (Extract substring from a given string)
·         Copy(copies one string over another)
To do all the operations described here it is essential to include string.h library header file in the program.

strlen() function:
This function counts and returns the number of characters in a string. The length does not include a null character.
Syntax: n=strlen(string);
          Where n is integer variable. This receives the value of length of the string.
Example:
length=strlen(“Hollywood”);
        The function will assign number of characters 9 in the string to a integer variable length.
strcat() function:
when you combine two strings, you add the characters of one string to the end of other string. This process is called concatenation. The strcat() function joins 2 strings together. It takes the following form
strcat(string1,string2)

string1 & string2 are character arrays. When the function strcat is executed string2 is appended to string1. the string at string2 remains unchanged.
Example:
strcpy(string1,”sri”);
strcpy(string2,”Bhagavan”);
Printf(“%s”,strcat(string1,string2);

From the above program segment the value of string1 becomes sribhagavan. The string at str2 remains unchanged as bhagawan.

Strcmp() function:
In c you cannot directly compare the value of 2 strings in a condition like if(string1==string2)
Most libraries however contain the strcmp() function, which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same. The syntax of strcmp() is given below:
Strcmp(string1,string2);
String1 & string2 may be string variables or string constants. String1, & string2 may be string variables or string constants some computers return a negative if the string1 is alphabetically less than the second and a positive number if the string is greater than the second.
Example:
strcmp(“Newyork”,”Newyork”) will return zero because 2 strings are equal. strcmp(“their”,”there”) will return a 9 which is the numeric difference between ASCII ‘i’ and ASCII ’r’. strcmp(“The”, “the”) will return 32 which is the numeric difference between ASCII “T” & ASCII “t”.

strcpy() function:
C does not allow you to assign the characters to a string directly as in the statement name=”Robert”;
Instead use the strcpy(0 function found in most compilers the syntax of the function is illustrated below.
strcpy(string1,string2);
Strcpy function assigns the contents of string2 to string1. string2 may be a character array variable or a string constant.
strcpy(Name,”Robert”);
In the above example Robert is assigned to the string called name.