Unix C

Queue.h
-------
#include
#include
#include
#include
struct queue
{
long type;
char msg[50];
};
Queue.c
-------
#include "queue.h"
int main()
{
struct queue q;
int mid;
if((mid=msgget(ftok("string",0x99),0644|IPC_CREAT))<0)
{
if(mid!=-1)
{
printf("Message Queue Error
");
exit(1);
}
else
{
printf("Message Already Exist
");
exit(1);
}
}
printf("
Enter the Message Type:");
scanf("%d",&q.type);
while(q.type>0)
{
printf("
Enter the Message:");
getchar();
gets(q.msg);
if(msgsnd(mid,&q,sizeof(q),IPC_NOWAIT)<0)
{
printf("Message send Error
");
exit(1);
}
printf("
Enter the Message Type:");
scanf("%d",&q.type);
}
return 0;
}
Word.c
------
#include "queue.h"
#include
void word(char* str)
{
int i,n,w=1;
n=strlen(str);
for(i=0;i if(str[i]==' ' && str[i-1]!=' ')
w++;
printf("Number of Words in "%s" :%d
",str,w);
}
int main()
{
struct queue q;
int mid;
long type;
if((mid=msgget(ftok("string",0x99),0644 ))<0)
{
printf("Message Queue Error
");
exit(1);
}
printf("
Enter the Message Type to Reterive:");
scanf("%d",&type);
while(msgrcv(mid,&q,sizeof(q),type,IPC_NOWAIT) && errno!=ENOMSG)
{
word(q.msg);
}
msgctl(mid,IPC_RMID,NULL);
}
Reverse.c
---------
#include "queue.h"
#include
void rev(char* str)
{
char s[30];
int i,j,n;
n=strlen(str);
printf("Reverse for "%s" :",str);
for(i=n;i>=0;i--)
putchar(str[i]);
printf("
");
}
int main()
{
struct queue q;
int mid;
long type;
if((mid=msgget(ftok("string",0x99),0644 ))<0)
{
printf("Message Queue Error
");
exit(1);
}
printf("
Enter the Message Type to Reterive:");
scanf("%d",&type);
while(msgrcv(mid,&q,sizeof(q),type,IPC_NOWAIT) && errno!=ENOMSG)
{
rev(q.msg);
}
}