Message queue IPC example in C

//message queue server
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500
struct msg
{
 long int msgtype;
 char msgfromclient[1024];
 int pid;
}msgbuf;

int main()
{
 int mqid,n,fd,m1;
 mqid=msgget(KEY,0666|IPC_CREAT);
 while(1)
 {
 msgrcv(mqid,&msgbuf,sizeof(msgbuf),1,0);
 printf("Filename from client %s\n",msgbuf.msgfromclient);

//sending requested file data to client
 fd=open(msgbuf.msgfromclient,O_RDONLY);
 n=read(fd,msgbuf.msgfromclient,1024);
 msgbuf.msgtype=msgbuf.pid;
 msgbuf.pid=getpid();
 msgsnd(mqid,&msgbuf,sizeof(msgbuf),0);
 }
return 0;
}
//client
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500

struct msg
{
 long int type;
 char a[1024];
 int pid;
}p,p1;
int main()
{
 int m;
 m=msgget(KEY,0);
//fill the structure to send data
 p.type=1;
 printf("\nEnter the msg");
 scanf("%s",&p.a);
 pid_t pid;
 p.pid=getpid();
 msgsnd(m,&p,sizeof(p),0);
 msgrcv(m,&p1,sizeof(p),p.pid,0);
 printf("%s",p1.a);
return 0;
}

//output:
ServerTerminal$ ./server
ClientTerminal$ ./client
Enter the msg hello.txt

ServerTerminal$ 
Filename from client hello
ClientTerminal$ ./client
contents of hello.txt

Synchronized shared memory server and client in C

Client writes  a text in shared memory.
Then server reads that and convert to upper case and write back to shared memory.
Client reads toggled text from shared memory.
//Server
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>

#define MY_KEY 1983
#define SHM_SIZE 0x1000

void toggleCase(char* buff, int cnt);

int main()
{
int semId, shmId;
char* pShm;
struct sembuf smop;

semId=semget(MY_KEY,2,0660 | IPC_CREAT);

semctl(semId,0,SETVAL,0);
semctl(semId,1,SETVAL,0);
shmId = shmget(MY_KEY, SHM_SIZE, 0660 | IPC_CREAT);

pShm=shmat(shmId,NULL,0);

while(1)
{
smop.sem_num = 0;
smop.sem_op = -1;
smop.sem_flg = 0;
semop(semId,&smop,1);

strcpy(pShm+256,pShm);
toggleCase(pShm+256, strlen(pShm+256));

smop.sem_num = 1;
smop.sem_op = 1;
smop.sem_flg = 0;
semop(semId,&smop,1);

}
return 0;
}

void toggleCase(char* buff, int cnt)
{
printf("\ntoggle\n");
int ii;
for(ii=0;ii<cnt;ii++)
{
if((buff[ii]>='A')&&(buff[ii]<='Z'))
buff[ii]+=0x20;
else if((buff[ii]>='a')&&(buff[ii]<='z'))
buff[ii]-=0x20;
}
}

//Client
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>

#define MY_KEY 1983
#define SHM_SIZE 0x1000

#define MSG_LEN 256
#define RESP_MSG_START 256

//void toggleCase(char* buff, int cnt);

int main()
{
int semId, shmId;
char* pShm;
struct sembuf smop;

semId=semget(MY_KEY,2,0);

//semctl(semId,0,SETVAL,0);
//semctl(semId,1,SETVAL,0);

shmId = shmget(MY_KEY, SHM_SIZE,0);

pShm=shmat(shmId,NULL,0);

fgets(pShm,MSG_LEN,stdin);

smop.sem_num = 0;
smop.sem_op = 1;
smop.sem_flg = 0;
semop(semId,&smop,1);

smop.sem_num = 1;
smop.sem_op = -1;
smop.sem_flg = 0;
semop(semId,&smop,1);
puts(pShm+RESP_MSG_START);
return 0;
}

//void toggleCase(char* buff, int cnt)
//{
//printf("\ntoggle\n");
//}

//terminal 1: ./server
//terminal 2: ./client
india
check
//terminal 1: 
//terminal 2:
INDIA  

Does friend function follows access specifier rule in C++

No, friend functions do not follow access specifier rules as they are not members function. Friend function declared in any access specfier can access private data of the class using object.
#include<iostream>
using namespace std;
class sample
{
private:
        int a;
private:
        void privateNormalFunction(){};
private:
        friend void f1(sample);
protected:
        void friend f2(sample);
public:
        friend void f3(sample);
};
void f1(sample s)
{
s.a=10;
cout<<"f1::a"<<s.a<<endl;
}
void f2(sample s)
{
s.a=10;
cout<<"f2::a"<<s.a<<endl;
}
void f3(sample s)
{
s.a=10;
cout<<"f3::a"<<s.a<<endl;
}
int main()
{
sample s;
f1(s);
f2(s);
f3(s);
return 0;
}                                                                                                                                               
                                                                                                                                                

Private virtual function

//Can virtual function be private?
//Suppose virtual function vf() is declared as private in base.Even though vf is overriden as public in derived compiler will throw error of private at run time. But one work around could be given to declare a virtual function as private by making main() as friend function of that class.Because friend functions can access the private members.
#include<iostream>
using namespace std;
class base
{
private:
        virtual void vf(){cout<<"base::vf"<<endl;}
//friend int main();
};
class derived:public base
{
public:
void vf(){cout<<"derved::vf;"<<endl;}
};
int main()
{
base* bp = new derived;
bp->vf();//error: ‘virtual void base::vf()’ is private

return 0;
}

Vtable, vptr content and size postmortem

#include<iostream>
using namespace std;
class EmptyClass
{};
class PolymorphicVF
{
virtual void vf(){}
};
class PolymorphicDesstructor
{
virtual ~PolymorphicDesstructor()
{
}
};
class PolymorphicPureDestructor
{
virtual ~PolymorphicPureDestructor()=0;
};
class AbstractClassWithOnePureVF
{
virtual void pvf()=0;
};

class classWithOnepureVFAnd4vf
{
virtual void vf1(){};
virtual void vf2(){};
virtual void pvf()=0;
virtual void vf3(){};
virtual void vf4(){};
};

class Base
{
public:
virtual void vf(){}
};
class SingleDerivedNoOverridevf: public Base
{
};
class SingleDerivedNoOverridevf1: public Base
{
};
class SingleDerivedNoOverridevf2AndOneDM: public Base
{
public:
    char c;
};
class SingleDerivedWithOverridevf: virtual public Base
{
public:
    void vf(){}
};
class SingleDerivedWithOverridevf1: virtual public Base
{
public:
    void vf(){}
};;
class DiamiondProblemWithNoFinalOverrider:public SingleDerivedNoOverridevf, public SingleDerivedNoOverridevf1
{
};
class DiamiondProblemWithFinalOverrider:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1
{
void vf(){}
};
class DiamondDerivedWithThree:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1, public SingleDerivedNoOverridevf1
{
void vf(){}
};
class DerivedWithThreeDifferent:public EmptyClass,public PolymorphicVF,public PolymorphicDesstructor
{};
int main()
{
cout<<"sizeof(EmptyClass)="<<sizeof(EmptyClass)<<endl;//1
cout<<"sizeof(PolymorphicVF)="<<sizeof(PolymorphicVF)<<endl;//8
cout<<"sizeof(PolymorphicDesstructor)="<<sizeof(PolymorphicDesstructor)<<endl;//8
cout<<"sizeof(PolymorphicPureDestructor)="<<sizeof(PolymorphicPureDestructor)<<endl;//8
cout<<"sizeof(AbstractClassWithOnePureVF)="<<sizeof(AbstractClassWithOnePureVF)<<endl;//8
cout<<"sizeof(classWithOnepureVFAnd4vf)="<<sizeof(classWithOnepureVFAnd4vf)<<endl;//8
cout<<"sizeof(Base with one vf)="<<sizeof(Base)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf)="<<sizeof(SingleDerivedNoOverridevf)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf2AndOneDM)="<<sizeof(SingleDerivedNoOverridevf2AndOneDM)<<endl;//16
cout<<"sizeof(SingleDerivedWithOverridevf)="<<sizeof(SingleDerivedWithOverridevf)<<endl;//8
cout<<"sizeof(DiamiondProblemWithNoFinalOverrider)="<<sizeof(DiamiondProblemWithNoFinalOverrider)<<endl;//16
cout<<"sizeof(DiamiondProblemWithFinalOverrider)="<<sizeof(DiamiondProblemWithFinalOverrider)<<endl;//16
cout<<"sizeof(DiamondDerivedWithThree)="<<sizeof(DiamondDerivedWithThree)<<endl;//24
cout<<"sizeof(DerivedWithThreeDifferent)="<<sizeof(DerivedWithThreeDifferent)<<endl;//24

return 0;
}
/*
sizeof(EmptyClass)=1
sizeof(PolymorphicVF)=8
sizeof(PolymorphicDesstructor)=8
sizeof(PolymorphicPureDestructor)=8
sizeof(AbstractClassWithOnePureVF)=8
sizeof(classWithOnepureVFAnd4vf)=8
sizeof(Base with one vf)=8
sizeof(SingleDerivedNoOverridevf)=8
sizeof(SingleDerivedNoOverridevf2AndOneDM)=16
sizeof(SingleDerivedWithOverridevf)=8
sizeof(DiamiondProblemWithNoFinalOverrider)=16
sizeof(DiamiondProblemWithFinalOverrider)=16
sizeof(DiamondDerivedWithThree)=24
sizeof(DerivedWithThreeDifferent)=16
*/

//How to check content of vtable
//compiled with command:
//g++ -fdump-class-hierarchy vtable.cpp

//vtables are taken from generated class table file by above command:
//vtable.cpp.002t.class

Class EmptyClass
   size=1 align=1
   base size=0 base align=1
EmptyClass (0x0x7fc113139d80) 0 empty
//No vtable for non polymorphic class EmptyClass

Vtable for PolymorphicVF
PolymorphicVF::_ZTV13PolymorphicVF: 3u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI13PolymorphicVF)
16    (int (*)(...))PolymorphicVF::vf

Class PolymorphicVF
   size=8 align=8
   base size=8 base align=8
PolymorphicVF (0x0x7fc113139de0) 0 nearly-empty
    vptr=((& PolymorphicVF::_ZTV13PolymorphicVF) + 16u)