akevintg

(CLIENT) CLIENT-SERVER (Session 9)

Jan 5th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. //Link Soal : https://www.dropbox.com/s/sse2lot7e6ohdl9/O1-T0316-RG01-09.pdf?dl=0
  2.  
  3. #include <stdio.h>
  4. #include <sys/sem.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <sys/types.h>
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. struct data{
  14.     char pilih[10];
  15. };
  16.  
  17. int main(){
  18.     struct sembuf s;
  19.     int h=shmget(123456, sizeof(struct data),IPC_CREAT|0666);
  20.     int e=semget(123456,1,IPC_CREAT|0666);
  21.     struct data *suit=(struct data*)shmat(h,NULL,066);
  22.     semctl(e,0,SETVAL,0);
  23.     int flag=0;
  24.     do{
  25.         do{
  26.             flag=0;
  27.             printf("Masukan Pilihan[batu|gunting|kertas] : ");
  28.             scanf("%[^\n]",suit->pilih);getchar();
  29.             if(strcasecmp(suit->pilih,"batu")==0||strcasecmp(suit->pilih,"kertas")==0||strcasecmp(suit->pilih,"gunting")==0)
  30.                 flag=1;
  31.             else if(strcasecmp(suit->pilih,"exit")==0)
  32.                 break;
  33.         }while(flag==0);
  34.         s.sem_num=0;
  35.         s.sem_op=1;
  36.         s.sem_flg=0;
  37.         semop(e,&s,1);
  38.         printf("Press Enter");
  39.         getchar();
  40.     }while(strcasecmp(suit->pilih,"exit")!=0);
  41.     shmdt(&s);
  42.     shmctl(h,IPC_RMID,0);
  43.     semctl(e,0,IPC_RMID);  
  44.     return 0;
  45. }
Add Comment
Please, Sign In to add comment