Thursday, 24 November 2011

UNIX AND NETWORK PROGRAMMING


UNIX AND NETWORK PROGRAMMING
Unit 1 and 2
SECTION-A

UNIT-1

1.      What do you mean by Operating System?
          It can be defined as a software that controls the hardware resources of the computer and provides an environment under which programs can run.

2.Define Kernel
      Generally the operating system software is called as Kernel, since it is relatively small and resides at the core of the environment.

3.System Call:
       The interface to the Kernel layer of the software called system call. Libraries and common functions that built on the top the system call interface. But applications are free to use both.

4.What is Shell?
    A Shell is a command line interpreter that reads user input and executes commands, The user input to a shell is normally from the terminal or sometimes from a file.

5. List out common Shells.
     Bourne Shell
      Bourne Again Shell
      C Shell
      Korn Shell
     TENEX C Shell

6.Define Unix file system
    It is a hierarchical arrangement of directories and files.  Everything starts in the directory is called root whose name is a single character /.

7.Define File Descriptor
        Is normally a non  negative integer that the kernel uses to identify the files being accessed by a particular process.  Whenever it opens an existing file  or creates a new file, the kernel returns a file descriptor.

8. Define Program
    A program is a executable file residing on a disk in a director.  A program is read into memory and is executed by the kernel as a result of one of the six exec functions.

9.Define Process and Process ID
      An executing instance of a program is called process; every process has a unique numeric identifier called the process ID. Is always a non-negative integer.

10. How the errors are handled in UNIX
      In 2 ways.
1.      extern int errno;
2.      #define errno (*__errno_location())

11. Define signals
Signals are a technique used to notify a process that some condition has occurred.
             Eg. Ignore signal

12.State the functions that are performed by the UNIX OS.
            OPEN
            READ
            WRITE
            LSEEK
            CLOSE

13.Give the syntax for create and close function
         (a)          #include<functl.h>
                        int creat (const char *pathname, mode_t mode);

(b)                 #include <unistd.h>
int close(int files);
14.Define File Sharing?
            Sharing of open files among different process, but before describing the dup function.

15. What are the functions that are used in XSI?
            The extentions that are pread and pwrite are the functions that has been used in XSI.

16.State the types of Files in UNIX        
                        1.Regular file
2Directory file
3.      Block special file
4.      Character special file
5.      FIFO
6.SOCKET
7.Symbolic Link
17.State the Mode of Accessing files and types of files which can be accessed?
            St_mode through which we can access
            Directories,characters special files and so on.

18.Define UMASK
    which is used to sets the file mode creation mask for the process and returns the previous value.  This is one of the function  that doesn’t have an error return

19.What do you mean by file truncation?
      Emptying a file, which  can do with the O_TRUNC flag to open, is a special case of truncation. We use ftruncate in the program when we need to empty a  file after obtaining a lock on  the file.




20.Define Buffering? What is the goal of it.
            The goal of  the buffering provided by the standard I/O library to use the minimum number of read and write calls. They can be achieved by                                    a.Fully buffered
b.Line buffered
c.Unbuffered

                                    UNIT – II
1.State the ways through which process can be terminated?
            a.Return from main
            b.Calling Exit
            c.Calling _exit or _Exit
            d.Return of the last thread from its start routine
            e.Calling  pthread_exit
            f.Calling abort
            g.Receipt of signal
            h.Response of the last thread to a cancellation request

2.State the functions that can be used in Memory Allocation?
            Malloc
            Calloc
            Realloc
            Free
Alternative Memory Allocators are
            Libmalloc
            Vmalloc
3.State the rules that govern the changing of the resource limits
a.       A process can change its soft limit to a value less than or equal
To its hard limit
b.      A process can lower its hard limit to a value greater than or   
Equal to its soft limit.  This lowering of the hard limit is irreversible for normal users
c.      Only a superuser process can raise a hard limit

4. What do you mean child process how it will be created by fork Function
            The new process created by fork is called the child process.
                        #include <unistd.h>
                        pid_t fork(void)
which return 0 in child, process ID of child in parent, -1 on error.

5.Define Session
   A session is a collection of one or more process groups. The processes in a process group are usually placed there by a shell  pipeline.

6.What will happen if the signal occurs?
            Disposition of the signal or action associated  the signal will occur
By
                        Ignore the signal
                        Catch the signal
                        Default action apply

7.How will you write the signal function?
            #include <signal.h>
            Void (*signal  (int signo, void (*fun) (int))) int);
   Which returns the previous disposition of signal if OK SIG _ERR on error.

8.What  is the use of kill function?
            The Kill function send a signal to a process or group of processes. The raise function allows a process to send a signal to itself.

9.When the sleep function will be called?
1.      The amount of wall clock time specified by seconds has elapsed
2.      A signal is caught by the process and the signal handler returns

10. Define single Thread Multiple Thread?
            (a)Eachh process is doing only one thing at a time is single thread
            (b)designing a process to do more than one thing at  a time within a single
process, with each thread handling a separate task.

11.Define Mutex
A Mutex is basically a lock that  we set, any other thread that tries to set it will block until we release it.

12.State the Attributes of a Thread
            detachstate
            guardstate
            stackaddr
            stacksize

13.Define Thread safe
            If a function can be safely called by a multiple threads at the same time, we say the function is thread-safe.

14.Define Thread Synchronization
     When multiple threads of control share the same memory, we need to make sure that each thread sees a consistent view of its data. However, when one thread can modify a variable that other threads to ensure that they don’t use an invalid value when accessing the variable’s memory contents.








     






SECTION –B

UNIT-1

1.State and explain in detail about various features of UNIX?

2.What is Kernel? Differentiate  C Shell with Bourne Shell?

3.What are the various utilities available in the UNIX? Explain it with eg.

4.What is Filter explain with eg

5.What are called as Wild card Characters what are the usage of these wild card 
   characters?

6.What do you mean by symbolic Link? Explain it with eg.
   
7.What are important unbuffered I\O functions ? Explain it with eg

8.Explain briefly all flag values used in open command?

9.Define the directory file write the structure of stat.
10. Explain in detail about the functions related with  directories?

UNIT-1I

1.Define Processor and Processes explain  the Inter Process Communication?

2.What do you mean by Command line Argument? Write about the functions available
   to read a write to a line?

3.What are the various ways for a process to terminate?

4.Explain in detail about the global variable Environment?

5.Compare fork and vfork?Explain its function?

6.Describe signal and explain disposition of the Signal?

7.Explain in detail about stages of signal? And also explain what will happen if a blocked
   Signal us generated more than once before the process unblocks the signal?

8.What do you mean  by thread how the functions of thread are implemented in UNIX

UNIX COMMANDS


UNIX COMMANDS
1)      To Display the Date:

[mca0610015@linux ~]$ date
Tue Jan  8 15:20:42 IST 2008

2)      To Display the Time:

[mca0610015@linux ~]$ time

real    0m0.000s
user    0m0.001s
sys     0m0.000s
[mca0610015@linux ~]$

3)To Display the calendar:

[mca0610015@linux ~]$ cal
    January 2008
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

4) To clear the screen:
[mca0610015@linux ~]$clear

5) To Create a new directory:

[mca0610015@linux sample]$ mkdir first
[mca0610015@linux sample]$

6)      To check how many users activated currently:

[mca0610015@linux nana]$ who
mca06100 pts/1        Jan  8 15:21 (192.169.3.20)
mca06100 pts/2        Jan  8 15:17 (192.169.3.10)
mca06100 pts/3        Jan  8 15:27 (192.169.3.33)
mca06100 pts/4        Jan  8 15:18 (192.169.3.36)
mca06100 pts/5        Jan  8 15:18 (192.169.3.18)
mca06100 pts/6        Jan  8 15:19 (192.169.3.43)



7)      To Display the current Login:

[mca0610015@linux nana]$ who am i
mca06100 pts/24       Jan  8 15:24 (192.169.3.44)
[mca0610015@linux nana]$

8)      To Change the Directory:

[mca0610015@linux ~]$ cd sample
[mca0610015@linux sample]$


9)      To Exit to previous directory:

[mca0610015@linux sample]$ cd ..
[mca0610015@linux ~]$

10)  To Create a new File:

[mca0610015@linux ~]$ cat > vel
hi i am velmurugan
[mca0610015@linux ~]$

11)  Display the content of File:

[mca0610015@linux ~]$ cat vel
hi i am velmurugan
[mca0610015@linux ~]$

12)  To Display the Present working directory:

 [mca0610015@linux sample]$ pwd
/home/mca0610015/sample
[mca0610015@linux sample]$


13)  To Display the Long form of information of files:


[mca0610015@linux ~]$ ls -l
total 32
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 15:40 sample
-rw-rw-r--  1 mca0610015 mca0610015   19 Jan  8 15:47 vel
[mca0610015@linux ~]$



14)  To Display the Hidden files

 [mca0610015@linux ~]$ ls -a
.   .bash_history  .bash_profile  .emacs  .kde  nana    vel      .zshrc
..  .bash_logout   .bashrc        .gtkrc  kgln  sample  .xemacs
[mca0610015@linux ~]$



15)  To Copy the file in to another File

[mca0610015@linux ~]$ cp press vel
[mca0610015@linux ~]$ cat vel
hi i am vel
[mca0610015@linux ~]$

16)  To Display the Process State

[mca0610015@linux ~]$ ps
  PID TTY          TIME CMD
 9307 pts/24   00:00:00 bash
12257 pts/24   00:00:00 ps
[mca0610015@linux ~]$

 17)  To Remove the File:

 Before Remove:

 [mca0610015@linux ~]$ ls -l
total 40
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 press
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel

[mca0610015@linux ~]$ rm press

After Remove:
[mca0610015@linux ~]$ ls -l
total 32
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel
[mca0610015@linux ~]$


18)  To Remove the Directory:

Before Remove:

[mca0610015@linux ~]$ ls -l
total 32
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel

[mca0610015@linux ~]$ rmdir nana

After Remove:

[mca0610015@linux ~]$ ls -l
total 24
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel
[mca0610015@linux ~]$


19)  To Sort Ascending and descending order:

 [mca0610015@linux ~]$ cat > mani
vel
arun
sathish
press
manikandan
venki
bala

Ascending:

nana[mca0610015@linux ~]$ sort mani
arun
bala
manikandan
nana
press
sathish
vel
venki

Desending:

[mca0610015@linux ~]$ sort -r mani
venki
vel
sathish
press
nana
manikandan
bala
arun
[mca0610015@linux ~]$

To Execute the C program:

[mca0610015@linux ~]$ cat sam
main()
{
 printf("Welcome");
}
                                   
[mca0610015@linux ~]$ cc sam.c
sam.c:5:2: warning: no newline at end of file
[mca0610015@linux ~]$ ./a.out
Welcome[mca0610015@linux ~]$













2. program to create, open, write, Read and Copy.

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int n,n1,nn,nn1,m,nn2,nn3,nn4;
main()
{
 char fname[50];
 char oldfile[50];
 char newfile[50];
 char con[100],con1[100];

 do
 {
   printf("1-Create\n");
   printf("2-Write\n");
   printf("3-Read\n");
   printf("4-Copy\n");
   printf("5-MERGE\n");
   printf("Enter your choice:\n");
   scanf("%d",&m);
   switch(m){
    case 1: printf("Create New:\n");
            printf("Enter the File Name:\n");
           scanf("%s",fname);
           n=creat(fname,00777);
           if(n<0){
               printf("ERROR File not Created\n");
            }
               else
            {
               printf("Successfully Created\n");
           }close(n);

           break;
    case 2: printf("WRITE\n");
            printf("Enter the File Name:\n");
            scanf("%s",fname);
            n = open(fname,O_WRONLY);
            if(n<0)
            {  printf("ERROR\n");
            }
              else
            {
               printf("Enter the Content:\n");
               scanf("%s",con);
               n1 = write(n,con,100);
            }
            close(n);
            break;
    case 3: printf("READ\n");
            printf("Enter the File Name:\n");
            scanf("%s",fname);
            n=open(fname,O_RDONLY);
            n1 = read(n,con,100);
            if(n1<0)
            {  printf("ERROR\n");
            }
               else
            {
               printf("%s",con);

            }
             close(n);
            break;
    case 4: printf("\nCOPY");
            printf("Enter the New File Name:\n");
            scanf("%s",fname);
            n = creat(fname,00777);
            if(n<0)
            {
               printf("Error File Not Created\n");
            }
            else
            {
                printf("Successfully Created");
            }
            close(n);
            printf("Enter the Old File Name\n");
            scanf("%s",oldfile);
            n = open(oldfile,O_RDONLY);
            n1 = read(n,con,100);
            if(n1<0)
            {
              printf("ERROR");
            }
            else
            {
                 printf("%s",con);
            }
            printf("Enter the New File Name\n");
            scanf("%s",newfile);
            nn = open(newfile,O_WRONLY);
            if(nn<0)
            {
              printf("ERROR");
              }
            else
            {
               nn1 = write(nn,con,100);
            }
            close(n);
            close(nn);
            break;

    case 5: printf("Merge\n");
            printf("Enter First File Name:");
            scanf("%s",fname);
            n = open(fname,O_RDONLY);
            n1 = read(n,con,100);
            if(n1<0)
            {
                printf("ERROR...");
            }
            else
            {
                printf("%s",con);
            }
            printf("Enter the Second File:\n");
            scanf("%s",oldfile);
            nn = open(oldfile,O_RDONLY);
            nn1 = read(nn,con1,100);
            if(nn1<0)
            {
                printf("ERROR");
            }
            else
            {
                printf("%s",con1);

            }
            printf("Enter the New File NAME:");
            scanf("%s",newfile);
            nn2 = creat(newfile,00777);
            if(nn2<0)
            {
              printf("ERROR");
            }
             else
            {
               nn3 = write(n,con,100);
               nn4 = write(nn1,con1,100);
            }
            close(n);
            close(n1);
            close(nn);
            close(nn1);
            close(nn2);
            close(nn3);
            close(nn4);
            break;
         }
}while(m<=5);
}
             

OUTPUT:
























3. Directory.c

#include<stdio.h>
#include<dirent.h>
#include<fcntl.h>
#include<sys/types.h>
main()
{
  char d_name[10];
  DIR *dp;
  struct dirent *dest;
  printf("Enter the Directory Name:");
  scanf("%s",d_name);
  dp=opendir(d_name);
  if(dp<0)
  {
    printf("DIRECTORY DOES NOT EXIST...");
  }
  else
  {
    while((dest=readdir(dp))!=NULL)
    {
      printf("%s",dest->d_name);
    }
  }
  closedir(dp);
}

Output:

[mca0610015@localhost ~]$ cc directory.c
[mca0610015@localhost ~]$ ./a.out
Enter the Directory Name:sample
kar...firstoneba[mca0610015@localhost ~]$













4 . File Types:

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
struct stat *buff;
char fname[20];
int n;
printf("\nEnter File Name:");
scanf("%s",fname);
n=lstat(fname,buff);
if(n<0)
  printf("ERROR");
else
{
 printf("\n\nFile Type:");
 if(S_ISREG(buff->st_mode))
    printf("REGULAR FILE");
 else if(S_ISDIR(buff->st_mode))
    printf("Directory File");
 else if(S_ISCHR(buff->st_mode))
    printf("Character File");
 else if(S_ISBLK(buff->st_mode))
    printf("Block File");
 else if(S_ISFIFO(buff->st_mode))
    printf("Fifo File");
 else
    printf("No Such File");
}
printf("\n\nFile Size: %d",buff->st_size);
printf("\n\nUSER ID: %d",buff->st_uid);
printf("\n\nGroup ID:%d",buff->st_gid);
return 1;
}










OUTPUT:
[mca0610015@linux ~]$ cc fourth.c
[mca0610015@linux ~]$ ./a.out

Enter File Name:sample


File Type:Directory File

File Size: 4096

USER ID: 583

Group ID:584[mca0610015@linux ~]$






























5. SYSTEM DETAILS

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/utsname.h>
int main()
{
    struct utsname name;
    int n;
    n=uname(&name);
    if(n<0)
       printf("ERROR");
    else
    {

       printf("\n\nSYSTEM NAME: %s",name.sysname);

       printf("\n\nNODE NAME  : %s",name.nodename);

       printf("\n\nRELEASE    : %s",name.release);

       printf("\n\nVERSION    : %s",name.version);

       printf("\n\nMACHINE NAME : %s\n",name.machine);
     }
     return 1;
}

OUTPUT:
[mca0610015@linux ~]$ ./a.out


SYSTEM NAME: Linux

NODE NAME  : linux.example.com

RELEASE    : 2.6.9-5.ELsmp

VERSION    : #1 SMP Wed Jan 5 19:30:39 EST 2005

MACHINE NAME : i686
[mca0610015@linux ~]$



6. FORK:

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
main()
{
  int no,n;
  char name[20];
  float m1,m2,m3,m4,m5,tot,avg;

  printf("\nSTUDENTS DETAILS\n");
  printf("\nEnter the No:");
  scanf("%d",&no);
  printf("\nEnter the Name:");
  scanf("%s",name);
  printf("Enter Five marks");
  scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
  n = fork();
  printf("%d",n);
  if(n==0)
  {
    tot = m1+m2+m3+m4+m5;
    printf("Total Marks is: %f",tot);
    avg = tot/5;
    printf("Average is :%f",avg);

  }
}


OUTPUT:
[mca0610015@linux ~]$ ./a.out
STUDENTS DETAILS
Enter the No:10
Enter the Name:press
Enter Five marks50
50
50
50
50
0Total Marks is: 250.000000Average is :50.0000007388[mca0610015@linux ~]$



7. VFORK:

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
 int empno,n;
 char empname[50];
 float bp,hra,da,pf,net;
 printf("\nEnter Employee No:");
 scanf("%d",&empno);
 printf("\nEnter Employee Name:");
 scanf("%s",empname);
 printf("\nEnter Basic Pay::");
 scanf("%f",&bp);
 n =vfork();
 if(n==0)
 {
   printf("Im in Child");
   exit(1);
 }
 else
 {
   hra = bp*3/100;
   da = bp*4/100;
   pf = bp*5/100;
   net = bp+hra+da-pf;
   printf("\nI'm in Parent");
   printf("\nHRA   :%f",hra);
   printf("\nDA    :%f",da);
   printf("\nPF    :%f",pf);
   printf("\n-------------*\n");
   printf("\nNET PAY : %f",net);

 }
}








OUTPUT:

[mca0610015@linux ~]$ ./a.out

Enter Employee No:10

Enter Employee Name:log

Enter Basic Pay::10000
Im in Child
I'm in Parent
HRA   :300.000000
DA    :400.000000
PF    :500.000000
-------------*

NET PAY : 10200.000000

[mca0610015@linux ~]$         



























8. pipe fork – employee details.

#include<stdio.h>
struct emp{
  char name[20];
  int id;
  float bpay,hra,da,pf,np;


}e;
int main()
{
int fd[2],pid=0,n;
char buf[100];
pipe(fd);
printf("Enter the Employee Name:");
scanf("%s",e.name);
printf("Enter the Employee id:");
scanf("%d",&e.id);
printf("Enter the Basic Pay:");
scanf("%f",&e.bpay);
pid=fork();
if(pid==0)
{

  close(fd[1]);

  n=read(fd[0],&e,50);
  printf("n read =%d",n);
  e.hra=e.bpay*.01;
  e.da =e.bpay*.02;
  e.pf =e.bpay*.03;
  e.np =e.bpay+e.hra+e.da-e.pf;



  printf("\n\t\t\tEMPLOYEE DETAILS");
  printf("\nNAME     :%s",e.name);
  printf("\nID       :%d",e.id);
  printf("\nBASIC PAY:%f",e.bpay);
  printf("\nHRA      :%f",e.hra);
  printf("\nDA       :%f",e.da);
  printf("\nPF       :%f",e.pf);
  printf("\nNET PAY  :%f",e.np);
}
else
{
close(fd[0]);
n=write(fd[1],&e,sizeof(e));
printf("n write =%d",n);
//printf("p=%d",e.name);
}
}
[mca0610044@linux ~]$ cc pipeemp.c
[mca0610044@linux ~]$ ./a.out
Enter the Employee Name:jeni
Enter the Employee id:123
Enter the Basic Pay:1000.
n write =44n read =44
                        EMPLOYEE DETAILS
NAME     :jeni
ID       :123
BASIC PAY:1000.000000
HRA      :10.000000
DA       :20.000000
PF       :30.000000
NET PAY  :1000.000000[mca0610044@linux ~]$




9. FIFO FILE:

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define FIF1 "one.1"
#define FIF2 "two.1"
int main()
{
   int fd,fd1,pid,fd3;
   char buf[100],buf1[100],fname[100],buf2[100];
   mkfifo(FIF1,00777);
   mkfifo(FIF2,00777);
   pid=fork();
   if(pid==0)
   {
      fd=open(FIF1,O_WRONLY);
      printf("Enter the filename");
      scanf("%s",&fname);
      write(fd,fname,strlen(fname));
      close(fd);
      fd=open(FIF2,O_RDONLY);
      read(fd,buf,100);
      printf("%s",buf);
      close(fd);
   }

   else
   {
      fd1= open(FIF1,O_RDONLY);
      read(fd1,buf1,100);
      printf("%s",buf1);
      close(fd1);
      fd3=open(buf1,O_RDONLY);

 if(fd3>0)
        read(fd3,buf2,100);
     else
      //  strcpy(buf2,"File does not exist");
{
perror("First");
exit(1);
}
     close(fd3);

     fd1=open(FIF2,O_WRONLY);
     write(fd1,buf2,100);
     close(fd1);
  }
Unlink(FIF1);
Unlink(FIF2);
}

OUTPUT:
[mca0610046@linux ~]$ cc fifofile.c
[mca0610046@linux ~]$ ./a.out
Enter the filename/home/mca0610046/check
hai welcome
/home/mca0610046/checkLinux[mca0610046@linux ~]$











10. server.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
int main()
{
  struct msg
  {
    long type;
    char text[10],text1[10];
  }m;
  int n,n1,ms,mr;

  n=msgget((key_t)46,00666|IPC_CREAT);
n1=msgget((key_t)461,00666|IPC_CREAT);
 while(1)
  {
  printf("Enter the text to write in to the queue:");
  scanf("%s",m.text);
m.type=100;
//  printf("\nqid=%d",n);
  ms=msgsnd(n,&m,10,0);
 // printf("\nms=%d",ms);
  if(ms>=0)
  {
 // printf("\nMessage is%s=",m.text);
  printf("\nmessage send successfully");
  }
  else
  {
    printf("\nmessage not send\n");
  }
msgrcv(n1,&m,10,0,0);
printf("\n Received    :%s",m.text);

 }
}


Client.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
int main()
{
   struct msg
   {
      long type;
      char text[10];
   }m;
   int n,mr,ms,n1;
   char buf[10];

   n=msgget((key_t)46,00666|IPC_CREAT);
   n1=msgget((key_t)461,00666|IPC_CREAT);
   while(1)
   {

//   printf("\n%d",n);
    mr=msgrcv(n,&m,10,0,0);

 printf("\nReceived   :%s",m.text);

printf("Enter message:");
scanf("%s",m.text);
m.type=100;
ms=msgsnd(n1,&m,10,0);
printf("n1=%d",n1);
if(ms<0)
{
perror("send..");
exit(1);
}
}
}