Sunday, July 15, 2012

Example on semaphore & shared memory

/* sem.c */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void)
 {
pid_t pid;
int *shared; /* pointer to the shm */
int shmid;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
if (fork() == 0)
{ /* Child */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Child pointer %u\n", shared);
*shared=1;
printf("Child value=%d\n", *shared);
sleep(2);
printf("Child value=%d\n", *shared);
}
 else
{ /* Parent */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Parent pointer %u\n", shared);
printf("Parent value=%d\n", *shared);
sleep(1);
*shared=42;
printf("Parent value=%d\n", *shared);
sleep(5);
shmctl(shmid, IPC_RMID, 0);
}
}

COMPILE:   $ cc sem.c -o simplesema
                     $ ./simplesema

IPC File transfer using Message Q's

/* file sending to message Q */

    # include <stdio.h>
    # include <string.h>
    # include <sys/stat.h>
    # include <sys/msg.h>
    # include <sys/ipc.h>
    # include <sys/types.h>

    struct message
    {
    long msg_type;
    char msg_data[1024];
    };

    int main(int arc,char*argv[])
    {
    //message structure
    FILE*f;
    int x,y,i=0;
    char ch;
    int key=1234;
    int msg_id;
    struct message msg_obj1={100,"I"};

    //function to put file to message Q
    if((f=fopen(argv[1],"r"))==NULL)
    perror("\nUnable to open file for reading...");
    else
    {
        while((ch=getc(f))!=EOF)
        {
        msg_obj1.msg_data[i++]=ch; //writing to msg_data
        }   
        fclose(f); //closing file
    }
       
    //creating msg q id
    if((msg_id=msgget(key,IPC_CREAT|0644))==-1)
    perror("\nUnable to get message id...");
    else
    printf("\nmsgid=%d",msg_id);

    /*writing message to the q */
    if((x=msgsnd(msg_id,&msg_obj1,strlen(msg_obj1.msg_data),IPC_NOWAIT))==-1)
    perror("\nUnable to send message...");
    else
    printf("\nSend Msg Success : return %d",x);

    return 0;   
    }


compile :     $ cc msgsndQ.c -o msgsndQ
        $ ./msgsndQ <any file name>
       
       


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/* file receiving from message Q */
/* msgrecvQ.c */

    # include <stdio.h>
    # include <string.h>
    # include <sys/stat.h>
    # include <sys/msg.h>
    # include <sys/ipc.h>
    # include <sys/types.h>

    //message structure
   
    struct message
    {
    long msg_type;
    char msg_data[1024];
    };
    int main(int argc,char*argv[])
    {
    FILE*f;
    int x,y,i=0;
    char ch;
    struct message msg_obj2={100,"M"};
    int msg_id;
   
    //creating msg q id
    if((msg_id=msgget(1234,0644))==-1)
    perror("\nUnable to get message id...");
    else
    printf("\nmsgid=%d",msg_id);

    /* rec message from q*/
    if((y=msgrcv(msg_id,&msg_obj2,1024,100,MSG_NOERROR))==-1)
    perror(":- msgrcv error...");
    else
    printf("\nRec Bytes : %d",y);

    if((f=fopen(argv[1],"w"))==NULL)
    perror("\nUnable to open file for writing...");
    else
    {
        for(i=0;msg_obj2.msg_data[i]!=0;i++)
        {
        putc(msg_obj2.msg_data[i],f);
        }   
        fclose(f); //closing file
    }
    return 0;   
    }

compile :     $ cc msgrecvQ.c -o msgrecvQ
        $ ./msgrecvQ   <any new file name>
        // test whether the file received or not
//narrate the output