003 Linux System ProgrammingIssues, queries and suggestions related to Linux, Linux Programming, IPC, Interprocess Communication, Synchronization, Semaphore, System Programming, Linux Software Development.
Two issues that i was facing while creating communication using FIFOs for more than 1 clients :-
1. When more than 1 client is made, server can't able to notice which client is sending addition request , subtraction request and result gets over-written.
2. The client gets the result, server finishes all the operation and after that server does not finishes but at the end hanging of server occurs.
Is your code working fine for the 3 clients when they are receiving result simultaneously or parallelly ?
It is not working in my case as when I run 3 clients simultaneously using ./client1 & ./client2 & ./client3 then my results are getting intermixed and every time different clients begin first.
When one client is writing to server, server is computing the result and writing it back to client. But, as all 3 clients are working simultaneously, so it means that all 3 clients are ready to read the result from server. So, we never know which client is chosen by scheduler to get entertained. So, the correct result is coming from server, but probability that it will be received by the correct client is just 1/3 (in case of 3 clients). Client may receive the correct result, or may not be.....
Client requests are written into the fifo randomly (without any pre-defined order), and results are also not written into the clients as per the same order as above. Thus synchronization problem is caused.
The issues are resolved by using semaphores,signal handlers. Semaphores are used at the shared region so that one process at a time can used the shared region and no over written of data occurs.
I have used shared memory for sending the request by the clients to the server and message queue for receiving the result and in adder,multiplication,subtraction and division function i have used pipes first for sending the request by the server. The shared memory data is written into the pipe but data cannot be read by any of the functions(adder,subtract,multiplication or division), maybe somewhere it gets block. Because I have checked the structure data after it was written into a pipe the data written was accurate which client had sent but while on the reader side it shows the number of bytes readed but I'm unable to access the structure data, no printf statements worked and if I remove those printf statements then also the functions did not worked till end,after reading the number of bytes it got stuck and the reading and writing/reading on pipes/FIFO is not proper because if the writer is writing the data, then reader is reading but again the printf statements of writer shows and reader gets maybe blocked. Then, I have replaced the pipes with FIFO but same problem arises.
My project is working fine when I run my script for one time but it fails to give desired results when run on second time .......... it goes into the clients' process in second time but doesn't able to read result and also clients are not able to receive signals when run on second time . I have unlinked REQFIFO and RESFIFO after result read by server also closed file descipters where necessary . When i press ctrl+c then it is giving garbage and sometimes giving intermixed value.
I think i need to know where i should unlink REQFIFO and RESFIFO.
I think there is some synchronisation problem in your project. You should try to use "kill" system call before opening and writing the RESFIFO in the server and in client side, opening and reading the RESFIFO should be done inside the flag which is set and reset by the Signal Handler.
I think you should set your flag =0; as may be it is n't able to send because of garbage content so you should send it after "ipcrm -a" to remove msgid.
Rahul check your Structure ! You have used used typedef and then giving the name 'd ' to structure ... Now this. 'd' will behave as a data type of your Structure.... Further you have assigned values just pointed by 'd' , this 'd' is a data type ....
You should give a variable name of your Structure .. For Example...
d result Now put values in this variable---
result.pid = getpid(); result.res = 100;
Then you should use this variable in your messagequeue like below :
1:created pipe successfully. 2:using the concept of pipes in client server communication to run 1client. 3:using the conecpt of pipes in client server communication to run 3clients. 4:successfully run 10 clients. Next challenge is to implement FIFO and signals
3- in child we have excel the client and vendor process and we have passed the read and write file descriptor.
4-client process is writing the request which is read in the server process.
5- in server process the request sent by client write to the vendor process, vendor reads the request and perform the operation and again write the result to the server.
6- server reads the result written by the vendor and writes it to the client using write fd.
7- in client process the result is read by the client.
8- server is communicating the 3 clients and 3 vendors
All the above process is done by the pipes only.
3 clients are running successfully using pipes only.
in project 003, to avoid zombies , trap a SIGCHLD ,and in its signal handler ,issue the wait3 system call with WNOHANG option.
by using this approach i am able to avoid zombies but i have faced following new problem ,
in server , i have implemented a thread semaphore ,apply sem_wait before creating a new thread and release this semaphore(sem_post) in processReq (thread ) after opening the fifo.
after running a handler , the sem_wait generate a error : Interrupted system call
for better understanding please go through my code once.