003 Linux System ProgrammingIssues, queries and suggestions related to Linux, Linux Programming, IPC, Interprocess Communication, Synchronization, Semaphore, System Programming, Linux Software Development.
The Server. It will create... a semaphore for synchronization a common request message Q, a thread that will read the request from request q. As no client would have sent request... this thread would go on block on read.
when the client sends in the request. the thread will read the request. find the operation (if supported) then invokes the processing client and passes the request onto it. when the result is available. This thread will send it back to the requesting client. (meanwhile the server creates another thread for reading from the request q)
Hi all, I have implemented multithreading with one client and one processing client . I have used message queue IPC mechanism to communicate between client and server. And created thread in server to load the processing client executable with in the thread using fork() and execlp(). In processing client i did the operation and created msgQ of to send the client. It's working fine.
But in case of multi clients i have taken 2 clients and synchronized the client request using semaphore . server is able to handle both client successfuly , problem is that when server get a request from 1st client and server is not running and 2nd client also send a request to server that time message is overwrite by the last client because i have common msgQ for both clients. so proceesing clients getting the same operands from both client and able to send same data to both clients. So can anyone please help me on this. NOTE: for clear understanding go through my code once.
void signal_handler(int sig) { printf("I got a signal Number:%d\n", sig); }
int main() { struct msgbuf { long mtype; int cpid; char msgtext[BUF_SZ]; };
struct msgbuff { long mtype; int result; };
struct msgbuf msgbuf1; struct msgbuff msgbuff1;
msgbuf1.mtype = 1; msgbuff1.mtype = getpid();
int semid; struct sembuf sop; union semun { int val; /* Value for SETVAL */ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */ }; union semun sem1; int ret1; key_t key = 5678; // sem1.val = 1; semid = semget(key, 2, IPC_CREAT|0666); if (semid == -1) { perror("semget"); exit(EXIT_FAILURE); }
int size = sizeof(msgbuf1.msgtext) + sizeof(msgbuf1.cpid); int sizep = sizeof(msgbuff1.result); int msgid, msgid1, ret; char *p; signal(2, signal_handler); printf("The size of msg to be passed:%d\n", size);
msgbuf1.cpid = getpid(); printf("The pid of client is:%d\n", msgbuf1.cpid);
p = fgets(msgbuf1.msgtext, BUF_SZ, stdin); if (p == NULL) { perror("fgets"); exit(EXIT_FAILURE); }
This will make another attempt to take semaphore if it fails for the first time. Acquiring semaphores may fail as the semaphore variable works in the atomic mode. If one process is doing test-set operation and the other starts testing then the second process will not be able to acquire the semaphore.
What can be the possible reasons that in server, sem_wait is not working properly even after sem_post in done in vendor and the semaphore variable has been checked by sem_getvalue and it has been incremented but not reflecting at server sem_wait().
when server recieved the data from shared memory which consists of pid and result from vendor ,the pid is not same which was send by the client but result is same. Also the wrong pid and result prints in the starting before writng the data into the pipe.
You're trying to implement the thread semaphore between the server and the vendor using sem_init() so that only one thread can read the data from a client and send the data to the vendor for some operation and then, sending back to server using shared memory and you have implemented sem_wait() before sending the data to the vendor and after reading from the server you sem_post() it, right? If it's yes, then the possible reason for the semaphore variable not reflecting in sem_wait() after you sem_post() it in the vendor is the pshared argument in the sem_init(). The pshared variable should be 1 and the most important thing to make it happen is to create a shared memory containing the thread semaphore object so that both the processes can share that semaphore object.
Use thread semaphore, if your main thread is running more than once. And for pid and result, please go through you program one more time. I'm sure you can debug it.
What is the difference between multi-threading and hyper-threading?
Would it be correct to say that Hyper-threading is Intel's proprietary implementation of simultaneous multi-threading used to improve parallelization of computers at the processor level ?
local.h:4:2: error: unknown type name ‘LocalInfra’ 4 | LocalInfra *linfra; | ^~~~~~~~~~ In file included from server.c:5: local.h:1:16: error: redefinition of ‘struct total_infra’ 1 | typedef struct total_infra | ^~~~~~~~~~~ In file included from declarations.h:3, from server.c:4: local.h:1:16: note: originally defined here 1 | typedef struct total_infra | ^~~~~~~~~~~ In file included from server.c:5: local.h:5:2: error: conflicting types for ‘Total_infr’ 5 | }Total_infr; | ^~~~~~~~~~ In file included from declarations.h:3, from server.c:4: local.h:5:2: note: previous declaration of ‘Total_infr’ was here 5 | }Total_infr; | ^~~~~~~~~~ server.c: In function ‘main’: server.c:26:2: error: conversion to non-scalar type requested 26 | tinfra->linfra = (LocalInfra)(*fptr[2])(0); // create_local_infra() | ^~~~~~ server.c:30:20: error: ‘linfra’ undeclared (first use in this function); did you mean ‘tinfra’? 30 | sem_wait(&tinfra-linfra->sem); | ^~~~~~ | tinfra server.c:30:20: note: each undeclared identifier is reported only once for each function it appears in make: *** [makefile:24: server.o] Error 1 make: Leaving directory '/system_programming/client_server_using_ipc/server' server build failed