Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <fcntl.h>
- #include <stdio.h>
- int main(){
- struct mymsg {
- long mtype;
- char mtext[100];
- } Komunikat_Out, Komunikat_In;
- int n, len, res, readfd, writefd;
- writefd=msgget(10, IPC_CREAT | 0660);
- if(writefd<0){
- printf("\nBlad!: podczas kreowania kolejki komunikatow wystapil blad\n");
- exit(1);
- }
- //wysylanie komunikatu
- sprintf(Komunikat_Out.mtext, "%d", getpid()); len=strlen(Komunikat_Out.mtext);
- printf("\nKLIENT: --> Idmsg=%d, PID=%d LEN=%d", readfd, getpid(), len);
- Komunikat_Out.mtype=100;
- res=msgsnd(writefd, &Komunikat_Out, sizeof(long)+len, 0);
- if(res<0){
- printf("\nBlad! Podczas wysylania komunikatow wystapil blad\n");
- exit(1);
- }
- printf("\nKlient: dane zostaly wyslane\n");
- //odbior
- readfd=msgget(10, IPC_CREAT | 0660);
- Komunikat_In.mtype=getpid();
- res=msgrcv(readfd, &Komunikat_In,1000,getpid(),1);
- if(res <0){
- printf("\nBlad! odfbior komunikatow wystapil blad\n");
- exit(1);
- }
- printf("\nKlient: ODEBRANO (%s)\n", Komunikat_In.mtext);
- exit(0);
- } //main
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <fcntl.h>
- #include <stdio.h>
- int main(){
- struct mymsg {
- long mtype;
- char mtext[100];
- } Komunikat_Out, Komunikat_In;
- int n, len, res, readfd, writefd;
- writefd=msgget(10, IPC_CREAT | 0660);
- if(writefd<0){
- printf("\nBlad!: podczas kreowania kolejki komunikatow wystapil blad\n");
- exit(1);
- }
- //odbior komunikatu
- readfd=msgget(10, IPC_CREAT | 0660);
- for(;;){
- res=msgrcv(writefd,&Komunikat_In,1000,100,1);
- if(res<0) {
- printf("\nSERWER:BLAD! Odbior komunikatu wystapil blad");
- exit(1);
- }
- if(fork()==0) { //obsluga
- sprintf(Komunikat_Out.mtext, "Serwer %d %d", getpid(), Komunikat_In.mtext);
- len=strlen(Komunikat_Out.mtext);
- printf("\nSERWER:--> SERWER(PID=%d) KLIENT(%s)\n", getpid(), Komunikat_In.mtext);
- sscanf(Komunikat_In.mtext,"%d",&Komunikat_Out.mtype);
- sleep(30);
- res=msgsnd(writefd,&Komunikat_Out, sizeof(long)+len,0);
- if(res<0){
- printf("\nBLAD! Podczas wysylania komunikatow\n");
- exit(1);
- }
- exit(0);
- }
- }
- wait(0); exit(0);
- } //main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement