Code:#include<stdio.h> #include<stdlib.h> #include <sys/un.h> #include <netinet/in.h> #include<string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include<sys/shm.h> #include<pthread.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include<unistd.h> int newsockfd; //variables for shared and semaphore int sem_id,shmid; int mysemid[100]; void *sharedptr; struct mystruct { sem_t mysem; pthread_t tid; long int count; int flag; }; 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) */ }sem_union; //end of special purpose variables void *mythreadfunc(void *arg) { int n; int segment; char buffer[4]; memset(buffer,0,sizeof(buffer)); printf("going to read"); fflush(stdout); while( n= read(newsockfd, buffer, 4)) { segment=atoi(buffer); memset(buffer,0,sizeof(buffer)); struct sembuf sem_b; /*sem_b.sem_num=0; sem_b.sem_op=-1; sem_b.sem_flg=SEM_UNDO;*/ // printf("inside server--received segment-->%d--data is-->",segment); printf("inside server--received segment-->%d\n>",segment); fflush(stdout); /*if(semop(mysemid[segment],&sem_b,1)==-1) { fprintf(stderr,"semaphore lock failed"); sleep(10); continue; }*/ printf("threadid-->%d---count-->%ld\n",(int)((((struct mystruct *)sharedptr)+segment)->tid),(((struct mystruct *)sharedptr)+segment)->count); //(((struct mystruct *)sharedptr)+segment)->flag=0; /*sem_b.sem_num=0; sem_b.sem_op=1; sem_b.sem_flg=SEM_UNDO; if(semop(mysemid[segment],&sem_b,1)==-1) { fprintf(stderr,"semaphore unlock failed"); sleep(100); }*/ } } int main(int argc,char **argv) { int sockfd, portno, clilen; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(PF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR creating socket"); exit(0); } memset( &serv_addr,0, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family =AF_INET; serv_addr.sin_addr.s_addr =inet_addr("127.0.0.1"); serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(0); } listen(sockfd,50); clilen = sizeof(cli_addr); int i=0; for ( i=0;i<100 ;i++ ) { if(i==0) mysemid[i]=semget((key_t)102,1,0666|IPC_CREAT); else mysemid[i]=semget((key_t)i,1,0666|IPC_CREAT); if(mysemid[i]<0) { perror("semid error occured"); exit(1); } sem_union.val=1; if((semctl(mysemid[i],0,SETVAL,sem_union))<0) { perror("semid error occured initioalization"); exit(1); } }//end for key_t key = 1234; shmid=shmget(key,100*sizeof(struct mystruct),0644|IPC_CREAT); if(shmid==-1) printf("shmid error"); sharedptr=shmat(shmid,(void*)0,0); if(sharedptr==(void *)-1) { fprintf(stderr,"shared memory error"); fflush(stderr); exit(0); } memset(sharedptr,0,100*sizeof(struct mystruct)); sem_init(&((((struct mystruct *)sharedptr)+5)->mysem), 0, 1); while(1) { newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); printf("connection accepted"); fflush(stdout); if (newsockfd < 0) { perror("ERROR on accept"); exit(0); } printf("connection accepted"); fflush(stdout); pthread_t thread_ID; int value; pthread_create(&thread_ID , NULL, mythreadfunc , &value ) ; } for(i=0;i<100;i++) { semctl(mysemid[i],0,IPC_RMID); } return 0; } //end main