Advertisement
zaxisss

Untitled

Nov 29th, 2020 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6.  
  7. int main(){
  8. struct mymsg {
  9. long mtype;
  10. char mtext[100];
  11. } Komunikat_Out, Komunikat_In;
  12. int n, len, res, readfd, writefd;
  13.  
  14. writefd=msgget(10, IPC_CREAT | 0660);
  15. if(writefd<0){
  16. printf("\nBlad!: podczas kreowania kolejki komunikatow wystapil blad\n");
  17. exit(1);
  18. }
  19. //wysylanie komunikatu
  20. sprintf(Komunikat_Out.mtext, "%d", getpid()); len=strlen(Komunikat_Out.mtext);
  21. printf("\nKLIENT: --> Idmsg=%d, PID=%d LEN=%d", readfd, getpid(), len);
  22. Komunikat_Out.mtype=100;
  23. res=msgsnd(writefd, &Komunikat_Out, sizeof(long)+len, 0);
  24. if(res<0){
  25. printf("\nBlad! Podczas wysylania komunikatow wystapil blad\n");
  26. exit(1);
  27. }
  28. printf("\nKlient: dane zostaly wyslane\n");
  29. //odbior
  30. readfd=msgget(10, IPC_CREAT | 0660);
  31. Komunikat_In.mtype=getpid();
  32. res=msgrcv(readfd, &Komunikat_In,1000,getpid(),1);
  33. if(res <0){
  34. printf("\nBlad! odfbior komunikatow wystapil blad\n");
  35. exit(1);
  36. }
  37.  
  38. printf("\nKlient: ODEBRANO (%s)\n", Komunikat_In.mtext);
  39. exit(0);
  40. } //main
  41.  
  42.  
  43. #include <sys/types.h>
  44. #include <sys/ipc.h>
  45. #include <sys/msg.h>
  46. #include <fcntl.h>
  47. #include <stdio.h>
  48.  
  49. int main(){
  50. struct mymsg {
  51. long mtype;
  52. char mtext[100];
  53. } Komunikat_Out, Komunikat_In;
  54. int n, len, res, readfd, writefd;
  55.  
  56. writefd=msgget(10, IPC_CREAT | 0660);
  57. if(writefd<0){
  58. printf("\nBlad!: podczas kreowania kolejki komunikatow wystapil blad\n");
  59. exit(1);
  60. }
  61. //odbior komunikatu
  62. readfd=msgget(10, IPC_CREAT | 0660);
  63. for(;;){
  64. res=msgrcv(writefd,&Komunikat_In,1000,100,1);
  65. if(res<0) {
  66. printf("\nSERWER:BLAD! Odbior komunikatu wystapil blad");
  67. exit(1);
  68. }
  69. if(fork()==0) { //obsluga
  70. sprintf(Komunikat_Out.mtext, "Serwer %d %d", getpid(), Komunikat_In.mtext);
  71. len=strlen(Komunikat_Out.mtext);
  72. printf("\nSERWER:--> SERWER(PID=%d) KLIENT(%s)\n", getpid(), Komunikat_In.mtext);
  73. sscanf(Komunikat_In.mtext,"%d",&Komunikat_Out.mtype);
  74. sleep(30);
  75. res=msgsnd(writefd,&Komunikat_Out, sizeof(long)+len,0);
  76. if(res<0){
  77. printf("\nBLAD! Podczas wysylania komunikatow\n");
  78. exit(1);
  79. }
  80. exit(0);
  81. }
  82. }
  83. wait(0); exit(0);
  84. } //main
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement