Monday, April 16, 2012

Message Queue - System Software Lab


Message Queue - MCA - System Software Lab
 
#include sys/types.h
#include sys/ipc.h
#include sys/msg.h
#include stdio.h


#define MSGSZ     128



typedef struct msgbuf {
    long    mtype;
    char    mtext[MSGSZ];
} message_buf;


main()
{
    int msqid;
    key_t key;
    message_buf  rbuf;

    printf("\n\tLab program 10 : Message Queue - Reading content from the queue\n");
    key = 10;

    if ((msqid = msgget(key, 0666)) < 0) {
        perror("msgget");
        exit(1);
    }

   
    if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
        perror("msgrcv");
        exit(1);
    }

    printf("\nThe Message read from the Message Queue : %s\n", rbuf.mtext);
    exit(0);
}
The above is the program to read the message from the message Queue acting as a Client program.


#include sys/types.h
#include sys/ipc.h
#include sys/msg.h
#include stdio.h
#include string.h
#include fcntl.h
#include unistd.h
#include stdlib.h


#define MSGSZ     128

typedef struct msgbuf {
         long    mtype;
         char    mtext[MSGSZ];
         } message_buf;

main()
{
    int msqid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    message_buf sbuf;
    size_t buf_length;
    char tmsg[100];
  
    key = 10;
    printf("\n\tLab 10 : Message Queue - Sending Message to the Message Queue\n\n");
    printf("\nEnter a line to store as Message in the Queue : ");
   fgets(tmsg,100,stdin);
    printf("\nCalling msgget with key %#lx and flag %#o\n",key,msgflg);

    if ((msqid = msgget(key, msgflg )) < 0) {
        perror("msgget");
        exit(1);
    }
    else
     printf("\nmsgget: msgget succeeded: msqid = %d\n", msqid);


    sbuf.mtype = 1;
   
    printf("\nmsgget: msgget succeeded: msqid = %d\n", msqid);
   
    (void) strcpy(sbuf.mtext,tmsg);
   
    printf("\nmsgget: msgget succeeded: msqid = %d\n", msqid);
   
    buf_length = strlen(sbuf.mtext) + 1 ;
   
    if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
       printf ("\n%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
        perror("msgsnd");
        exit(1);
    }

   else
      printf("\nMessage: \"%s\" Sent\n", sbuf.mtext);
     
    exit(0);
}
The above is the program for storing content in the message Queue acting as Server program.

Saturday, March 31, 2012

Lab Programs for 1 - 3 - System Software Lab MC9227

Shared Memory Program - System Software Lab

Lab Program - 13 - Shared Memory - System Software Lab


#include “sys/types.h”
#include “sys/ipc.h”
#include “sys/shm.h”
#include “stdio.h”

#define SHMSZ     27

main()
{
    char c;
    int shmid;
    key_t key;
    char *shm,*s;  
/* The shared memory is named as 2012 */
    key = 2012;

    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
        perror("shmget");
        exit(1);
    }
    
/* Mapping the Shared Memory */
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }

    s = shm;
  
/* Storing some text in the Shared Memory */
    for (c = 'a'; c <= 'z'; c++)
        *s++ = c;
    *s = NULL;
/* Wait for the other process that reads the shared memory and alters the content */
    while (*shm != '*')
        sleep(1);

    printf("\n End of the Server Program - Shared Memory");
    exit(0);
}








#include “sys/types.h”
#include “sys/ipc.h”
#include “sys/shm.h”
#include “stdio.h”

#define SHMSZ     27

main()
{
    int shmid;
    key_t key;
    char *shm, *s;

    /*   The shared memory given a name as 2012 */

    key = 2012;

    if ((shmid = shmget(key, SHMSZ, 0666)) < 0) (< = <)
        {
          perror("shmget");
          exit(1);
        }
 /* Map the Shared Memory */   
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
    {
        perror("shmat");
        exit(1);
    }

/* Read the content from the Shared Memory */

    for (s = shm; *s != NULL; s++)
        putchar(*s);
    putchar('\n');

/* Change the first character of the Shared memory to * to indicate the server the content were read */

    *shm = '*';

    printf("\n End of Client Program - Shared Memory");

    exit(0);
}


Monday, March 26, 2012

Java Magazine - March/April 2012 Issue

Welcome to the March/April 2012 issue of Java Magazine!
The Java ecosystem is only as vibrant as its community.

In this issue's cover story, we profile some of the new, young Java developers who are delivering on the future of the platform

The New Java Developers
Meet some of the young developers creating the future of Java.

Today Decides Tomorrow
The Oracle Academy helps students embrace Java with the launch of a new Java curriculum.

JavaMail Delivers
Use JavaMail and Facelets to create a Web application that can send e-mail.

Clustering and High Availability Made Simple with GlassFish
Enable high-availability support for applications and session failover.

Sharing Data Among Threads Without Contention
Trisha Gee introduces the Disruptor, a 2011 Duke’s Choice Award Winner.

Fix This
Jonathan Giles offers up a JavaFX 2.0 code puzzler.

And more!

PHP Questions asked in Interview

Tuesday, March 20, 2012

Message Queue - System Software Lab - Program 10

Message Queue - System Software Lab - Program 10


Message queues are one of the three IPC (Inter Process Communication) facilities provided by the UNIX operating system apart from semaphores and shared memory. Message queues appeared in an early release of UNIX system V release III as a means of passing messages between processes in an asynchronous way.

Client and Server Program for Message Queue

Client Program Reads the Message from the Message Queue - receiver.
 
#include “sys/types.h”
#include “sys/ipc.h”
#include “sys/msg.h”
#include “stdio.h”

#define MSGSZ     128



typedef struct msgbuf {
    long    mtype;
    char    mtext[MSGSZ];
} message_buf;


main()
{
    int msqid;
    key_t key;
    message_buf  rbuf;

    printf("\n\tLab program 10 : Message Queue - Reading content from the queue\n");
    key = 10;

    if ((msqid = msgget(key, 0666)) < 0) {
        perror("msgget");
        exit(1);
    }

   
    if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
        perror("msgrcv");
        exit(1);
    }

    printf("\nThe Message read from the Message Queue : %s\n", rbuf.mtext);
    exit(0);
}

____________________________________________________

Server Program stores Message into the Message Queue - Sender Program.


#include “sys/types.h”
#include “sys/ipc.h”
#include “sys/msg.h”
#include “stdio.h”
#include “string.h”
#include “fcntl.h”
#include “unistd.h”
#include “stdlib.h”

#define MSGSZ     128

typedef struct msgbuf {
         long    mtype;
         char    mtext[MSGSZ];
         } message_buf;

main()
{
    int msqid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    message_buf sbuf;
    size_t buf_length;
    char tmsg[100];
  
    key = 10;
    printf("\n\tLab 10 : Message Queue - Sending Message to the Message Queue\n\n");
    printf("\nEnter a line to store as Message in the Queue : ");
   fgets(tmsg,100,stdin);
    printf("\nCalling msgget with key %#lx and flag %#o\n",key,msgflg);

    if ((msqid = msgget(key, msgflg )) < 0) {
        perror("msgget");
        exit(1);
    }
    else
     printf("\nmsgget: msgget succeeded: msqid = %d\n", msqid);


    sbuf.mtype = 1;
   
    printf("\nmsgget: msgget succeeded: msqid = %d\n", msqid);
   
    (void) strcpy(sbuf.mtext,tmsg);
   
    printf("\nmsgget: msgget succeeded: msqid = %d\n", msqid);
   
    buf_length = strlen(sbuf.mtext) + 1 ;
   
    if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
       printf ("\n%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
        perror("msgsnd");
        exit(1);
    }

   else
      printf("\nMessage: \"%s\" Sent\n", sbuf.mtext);
     
    exit(0);
}