/MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler...../
/* I M SERVER */
/*A WONDERFUL IMPLEMENTATION USING 3CLIENTS ..2PIPES ONLY...CREATING NEW PROCCESSES........IN FOR LOOP REPLACING PROCESSES.....KILLING CHILD PROCESS & NO SIGNAL.............*/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/sem.h>//to use process level semaphore....sys/ipc.h is already included into it.....
#include<sys/types.h>
union semun//union to intialise semaphore definded in linux/sem.h ...if not used ..then define it .
{
int val;
};
union semun init;//variable of union is declared..at the point of declaration of variable of structure or union ...elements of that union or structure will acquire space.........
int main()
{
int kid,tid;
kid=semget(786,1,777|IPC_CREAT);//semaphores are created
printf("**kid=%d in ser **\n",kid);
tid=semget(787,1,777|IPC_CREAT);
printf("tid=%d in ser \n",tid);
if(semctl(kid,0,SETVAL,init) != -1)//sem is intialised
printf("sem is initialised\n");
if(semctl(tid,0,SETVAL,init) != -1)
printf("sem is initialised\n");
char buff[4];
int res=0,i,count=0,j=0,fd[2],arr[2],ret;
char wr[4],rd[4],ind[4];
ret=pipe(fd);
if(ret==0)//one pipe is for all the clients
printf("pipe for clients is prepaired\n");
ret=pipe(arr);
if(ret==0)//one pipe is for all the processes
printf("pipe for processes is prepaired\n");
for(i=1;i<4;)//in for loop fork is called...for loop is to write data by three no. of clients...
{
j++;//i=2 j=1....j variable is taken to check the condition that every time ..when new process will...create.....then every time diffrent ./client clients file will replace the process ...so i have to replace..........name of those client files every time.....so i have to put all execl...commands in any perticular condition ...in which only required client file may replace the process...that condition is defined by j......
printf("j=%d\n",j);
ret=fork();//fork is called will create 2 processes ...one is child & other is parent
if(ret==0)//in child
{
printf("in chield of clients\n");
sprintf(wr,"%d",fd[1]);
sprintf(rd,"%d",fd[0]);
i=16;//i have taken a variable i to operate the loop...whose value will always be changed...I KNOW WHEN ANY MODIFICATION IS DONE ...THAT VARIABLE WILL TAKE DIFFRENT SPACE...bcz...when else is closed in this loop...then...for next time of loop..this child will also go further into loop with parent..& again call to loop to create a new child again....i need only parent to call fork again & agin...& create new ..child.....not child....so i have to kill child here either using signal...or without signal..i choose without signal.....what i have done is that....value of i will be checked for .child ...in for loop..will got its condition false...so this child finshes at end ..of this if(ret==0)..
//ONE MORE IMPORTENT THING IS THAT...THIS PCB WILL BE REPLACED ...IF I WANT THIS CHILD FIND WRONG CONDITION FOR NEXT TIME OF LOOP...THEN I HAVE TO SEND THIS i=16,.... AS ARGUMENT IN EXECL......
sprintf(ind,"%d",i);//to send it in execl..to another process which is going to be replaced here..1st i have to convert this i into character ..put character type i into ind.......
printf("rd=%s wr=%s ind=%s\n",rd,wr,ind);//send also value of i as argument..make condition false for this for loop
if(j==1)//if j==1.......1st time replace client1
{
ret=execl("./client1","client1",rd,wr,ind,NULL);//write into client1 when j==1;
printf("ret=%d\n",ret);
}
if(j==2)//j=2...2nd time of loop will relace this process......means while parent will operate which read data frm this client & send to its process
{
ret=execl("./client2","client2",rd,wr,ind,NULL);//same..3rd time of loop
printf("ret=%d\n",ret);
}
if(j==3)
{
ret=execl("./client3","client3",rd,wr,ind,NULL);
printf("ret=%d\n",ret);
}
}
else
{
//sleep(1);
count=read(fd[0],buff,4);//it will block here untill write...means while 1st ,2nd & 3rd time of loop data will be read here...no need to sleep here...bcz read will block parent untill data is written to pipe by client
i++;//++ment i every time..........to continue the loop
printf("i=%d\n",i);
printf("count=%d read (frm client to server)=%s\n",count,buff);
count=write(arr[1],buff,4);//write into processing pipe to process the data
printf("count write(frm server to p)=%d\n",count);
}
}
j=0;//again make j=0 ...to again replace diffrent ..processes....at diffrent conditions ...in no. of times of loop...at diffrent created child by fork in loop
count=read(arr[0],&res,4);//DATA WRITTEN BY DIFF. PROCESS ARE READ...ONE BY ONE IN THIS LOOP..WRITER ON PROCESS SIDE FINISHED...EITHER IT IS READING RESULT FROM PIPE
i++;
printf("i=%d\n",i);
printf("count read frm (p to server)=%d res=%d\n",count,res);
count=write(fd[1],&res,4);//WRITING RESULT INTO BACK TO CLIENTS ONE BY ONE.......
printf("count write (frm server to c)=%d\n",count);
a.sem_flg=SEM_UNDO;//DO NOT MAKE MISTAKE OF APPLYING SEMAPHORE HERE,,,BCZ...AFTER WRITING ...THIS SEMAPHORE WILL ACQUIRE UR PROCESS......& READS SAME DATA here......IT IS DIFFRENT WHEN CHAR data OUTPUT WILL BE READ INTO int TYPE VARIABLE ....WHAT WILL BE ITS OUTPUT?...BUT IT IS READING THE SAME>>>>>>>
printf("ANU \n");*/
count=read(fd,&res,4);
printf("count read..c1=%d RES=%d\n",count,res);
/*if(semop(tid,&a,1) != -1)
printf("semrs c1\n");
a.sem_num=0;
a.sem_op=1;
a.sem_flg=SEM_UNDO;*///will read garbage....1st of all