In my server code there is a thread per client...
The server call accept() and after that start the thread.
So there is a thread for client that save in RAM the client's message, that will be send to other clients.
Now in RAM I have created a shared memory in which thread read and write(save) client's message :
server.c
Code:
socket()
bind()
..
...
...int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, 10000, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
int *mem=mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
ftruncate(shmid, 4096);
while(1){
...
...
...
}
Now I must synchronized the threads by semaphore to read and write into shared memory.Like this : all thread can read, but when a thread write other thread can't do anything , only WAIT that the writing thread signals it has finished. I don't know how can I do... Can someone help me with some example? how can I manage a semaphore??
Thank you very much!