Note that there are some explanatory texts on larger screens.

plurals
  1. POHomework - One Server serves multiple Clients with Semaphores and Shared Memory
    primarykey
    data
    text
    <p>Hello everyone here am I with another homework issue. I have to write a Client and a Server program so that the Server (with shared memory and semaphores) can communicate with the Client. A client gets data from the stdin sends it to the server, the server sorts it and sends it back. The problem is that the server has to serve multiple clients and I wrote it so it could serve only one. If someone could give me that mind push how to achieve that would be nice.</p> <p>here are my client :</p> <p><a href="https://c.privatepaste.com/7590dd1f30" rel="nofollow">Client</a></p> <p>and server: </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/ipc.h&gt; #include &lt;sys/sem.h&gt; #include &lt;sys/shm.h&gt; #include &lt;errno.h&gt; #define SEMKEY1 6666 #define SEMKEY2 7777 #define SHMKEY 8888 #define HSIZE 128 union semun { int val; /*value for SETVAL*/ struct semid_ds *buf; /*buffer for IPC_STAT, IPC_SET*/ unsigned short int *array; /*array for getall, setall*/ struct seminfo *__buf; /*buffer for IPC_INFO*/ }; int P(int semid){ struct sembuf occupy; int res; occupy.sem_num = 0; occupy.sem_op = -1; occupy.sem_flg = 0; res = semop(semid, &amp;occupy, 1); if(res &lt; 0){ fprintf(stderr,"P() Failed"); exit(1); } return res; } int V(int semid){ struct sembuf release; int res; release.sem_num = 0; release.sem_op = 1; release.sem_flg = 0; res = semop(semid, &amp;release, 1); if(res &lt; 0){ fprintf(stderr,"V() failed"); exit(1); } return res; } int getSem(int key){ int semid; int errno; if((semid = semget(key, 1, 0)) &lt; 0){ fprintf(stderr, "getSem failed for key %d because %d \n ", key, errno); exit(1); } return semid; } /*STRUCKT die fuer die SHARED MEMORY benutzt wird*/ typedef struct srtelem { long elem; int flg; }SMSTRCKT; int long_comp(const void *a, const void *b){ const int *ai = (const int *) a; const int *bi = (const int *) b; return *ai - *bi; } void print_hangar(long *hangar, int i){ int j; for(j = 0; j &lt; i; j++){ printf("%ld \n", *(hangar+j)); } } int main(){ int semid1, semid2, shm_id, count; int errno, index; SMSTRCKT *shmptr; long hangar[HSIZE]; /*Semaphore hollen*/ semid1 = getSem(SEMKEY1); semid2 = getSem(SEMKEY2); count = 0; index = 0; printf("\t**SERVER**\n"); /*Shared memory anlegen*/ if((shm_id = shmget(SHMKEY, sizeof(SMSTRCKT), 066)) &lt; 0){ fprintf(stderr, "SHMGET failed because of %d\n",errno); exit(1); } /*Shared memory anhaengen*/ if((shmptr = (SMSTRCKT *) shmat(shm_id, NULL, 0)) == (SMSTRCKT*) -1){ fprintf(stderr,"SHMAT failed because of %d\n", errno); exit(1); } /*Get date from the Client*/ while((shmptr-&gt;flg) == 1){ P(semid2); if(shmptr-&gt;flg != 0){ printf("elem %d \n ",(int) shmptr-&gt;elem); *(hangar+count) = shmptr-&gt;elem; count++; } V(semid1); } qsort(hangar,count, sizeof(long),long_comp); /*Send the result to the Client*/ while(index &lt; count){ P(semid1); shmptr-&gt;elem = *(hangar+index); /*printf(" elem %ld index %d\n", shmptr-&gt;elem, index);*/ ++index; V(semid2); } P(semid1); shmptr-&gt;flg++; V(semid2); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload