Perl Programming Part I :: Perl Data Types

Monday, April 12, 2010 Posted by Kanishka Dilshan 0 comments

Type Identifier Containable Data
Scalar
$
Number or string
Array
@
List of values indexed by number
Hash
%
Group of values indexed by string
Subroutine
&
Subroutine callable by Perl
References
\
References to anything of that name

Scalar Data Types
In Perl the scalar data type is used in 3 contexts.
  1. String context
  2. Numeric context
  3. Miscellaneous context
String Context
  1. $name = "Kanishka" ;

Numeric Context
  1. $height = 34 ;

Miscellaneous Context
  1. $temperature = 31.47;


Array Data Types
An array in Perl can contain number of scalar variables.  The containing scalars may be related to different contexts. for example the same array can contain numeric data as well as string data.
Declaring an array
  1. @myArr=("Kaniskha",21,"Galle");

in above code an array called myArr is created and. 3 different data are stored inside it.
  1. @myArr=();

above code makes the myArr empty.
Note : Arrays in perl is not fixed. so the programmer can expand the size of any array when he/she need.
perl arrays are zero based.
Assigning Values to Arrays
arrays can be assigned in 2 ways. Either assign at once  or assign individually. This is up to the programmer.

Assigning an array at once
  1. @myArr=("Kaniskha",21,"Galle");

Assigning an array individually
  1. @myArr=();
  2. $myArr[0]="Kaniskha";
  3. $myArr[1]=21;
  4. $myArr[2]="Galle";

Hash Data Types
In Perl Hash data type is an unordered set of scalar data indexed by string scalar data item. If you are familiar with PHP you may be heared  about associative arrays. The hash data type in perl is identical to an associative array. Perl provides this hash data type using a hash table algorithm.
Like the standard array hash also can be populated individually.

Declaring a Hash ( Associative Array )
  1. %fruits = ( "O" => "Orange" , "A" => "Apple" , "B" => "Banana");

Please note that the => operator is equivalent to a comma. The main purpose of =>  operator is make association between pairs.
So we can write above code segment as following statement also.
  1. %fruits = ( "O" ,"Orange" , "A" , "Apple" , "B" , "Banana");

Like standard array , hash also can be populated by individually.
  1. $fruits{"O"}="Orange";
  2. $fruits{"A"}="Apple";
  3. $fruits{"B"}="Banana";
Labels:

Systems Programming :: System Calls For Process Management II

Sunday, April 4, 2010 Posted by Kanishka Dilshan 0 comments
fork() and wait() system calls

fork() system call
using fork system call we can create new child processes. Though we call fork() once it returns twice! once per processes (at parent process and child process). At parent process it returns PID of the child process. At child process it returns 0. Using this feature we can identify parent and child processes separately.
lets see an example to understand the behaviour of fork() system call
example 1:


 1: #include <stdio.h>
 2: 
 3: int main()
 4: {
 5:  printf("Parent says \t: Parent process started...\n");
 6:  int PID=fork();
 7:  if(PID==0)
 8:  {
 9:   //this code block is executed by the child process
10:   int childPID=getpid();
11:   int parentPID=getppid();
12:   printf("Child says \t: Child PID -> %d \n",  childPID);
13:   printf("Child says \t: My Parent's PID -> %d \n",  parentPID);
14:  }
15:  else
16:  {
17:   //this code block is executed by the parent process (PID!=0)
18:   int parentPID=getpid();
19:   printf("Parent says \t: my Child's PID %d \n",PID);
20:   printf("Parent says \t: my PID -> %d \n",parentPID);
21:  }
22:  
23:  printf("Both say \t: This statement is executed by child and parent \n");
24:  return 0; //end of the program
25: }


output 1:
Labels:

Systems Programming :: System Calls For Process Management I

Posted by Kanishka Dilshan 0 comments
Process Management System Calls

A process can be either program or a command. To perform a task process need CPU time, memory(address space), files & I/O devices. Every process is comprised of 4 attributes
  1. code (instructions)
  2. data
  3. stack
  4. unique process ID (PID)
getpid(), getppid() system calls

getpid() system call returns the process ID of the current process.
getppid() system call returns the parent's process ID of the current process

Sample program to demonstrate getpid() and getppid() functions



 1: #include <stdio.h>
 2: 
 3: int main()
 4: {
 5:  int pid=getpid(); //get current process ID
 6:  int ppid=getppid(); //get parent process ID
 7:  printf("PID (Current process ID) -> %d \n",pid);
 8:  printf("PPID (Parent process ID) -> %d \n",ppid);
 9:  return 0;
10: }

Output :



PPID is always greater than the PID.
The parent of all process is the init process its ID is always 1
to realize this execute following command and see the output
 ps -eo user,pid,ppid,stat,args
Output :
USER       PID  PPID STAT COMMAND
root         1     0 Ss   /sbin/init
root         2     0 S<   [kthreadd]
root         3     2 S<   [migration/0]
root         4     2 S<   [ksoftirqd/0]
root         5     2 S<   [watchdog/0]
root         9     2 S<   [events/0]
root        11     2 S<   [khelper]
root        12     2 S<   [kstop/0]
root        14     2 S<   [kintegrityd/0]
root        16     2 S<   [kblockd/0]
root        18     2 S<   [kacpid]
root        19     2 S<   [kacpi_notify]
root        20     2 S<   [cqueue]
root        21     2 S<   [ata/0]
...............................................


system() Library function and execl() system call
there is a considerable difference in between system() library function and execl() system call. when system() is executed a new process is created. But when execl() is executed the memory and instructions of original process is replaced by the new process and continues. So any instruction of original process will not be executed after the execl().

Lets consider following programs to identify the different between system() library function and execl() system call.

Program1: (system() library function)


1: #include <stdio.h>
2: 
3: int main()
4: {
5:  system("date");
6:  printf("I am here!\n");
7: }


Output 1:

Program 2: (execl() library function)


1: #include <stdio.h>
2: 
3: int main()
4: {
5:  execl("/bin/date","date",0);
6:  printf("I am here! \n");
7: }


Output 2:

When compare both output you can see the the  "I am here!" line is not printed in the output 2. The reason is execl() replaced the memory area and instructions of original process by the date utilities instructions and memory.

Note : you can use gcc or cc utilities (GNU C Compilers) to compile above examples. I've used gcc to compile above programs.

Labels:

Systems Programming :: System Calls

Saturday, April 3, 2010 Posted by Kanishka Dilshan 0 comments
Operating system divides the system memory(RAM) into two parts.
  • Kernel Space 
  • User Space
The memory area reserved for kernel is used by device drivers and kernel extensions only.
The user space is the reserved memory area for user mode applications. User space can be swapped out to disk when necessary. Normally the kernel space is not swapped out to disk.

The kernel provides important services for the user mode applications.
  • Opening files
  • Reading files
  • Writing to files
  • Making network connections
  • Making new processes
System calls are used to make use of these services.
Relation between kernel space and the user space
Note:  I have used open() system call for this demontration.

void main()
{
    printf("This is a user mode application\n");
    ......................................
    ......................................
    ......................................
    int FD=open("MyFile.txt",0);
    ......................................
    ......................................
}
                                             User Space
_______________________________________________________
                                          
                                           Kernel Space
int open(char *_fileName, int mode)
{
        ......................................
        ......................................
        push    dword mode
        push    dword flags
        push    dword _fileName
        mov eax, 5
        add esp, byte 12
        ......................................
        ......................................
        ......................................
}


Difference between Library Functions and System Calls
  • System calls
           Executed by the OS
           Generally performs an single specific operation
  • Library Functions
           Executed in the user program
           May call system calls
           May perform more than one task
           More reliable than system calls

The relationship between system calls and library functions can be depicted as follows.

Library Functions System Calls
fopen() create(),open(),close()
fclose() read(),write()
system() chmod(),chown()
malloc() fork()
getc() unlink()

Refer following web site for more C library functions.
Labels: