Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/sem.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/shm.h>
- #include <string.h>
- #include <errno.h>
- #include <iostream>
- #include <string>
- #define SHMSZ 10
- #define SHMGET_ERROR 10
- #define SHMAT_ERROR 11
- #define SHMDT_ERROR 12
- #define SEMOP_ERROR 20
- #define SEMGET_ERROR 21
- #define SEMCTL_ERROR 22
- #define READ_ERROR 30
- #define WRITE_ERROR 31
- int allocateMemory( char** shm )
- {
- int shmid; // shared memory segment ID
- key_t key = 5512; // key for shared memory
- if ((shmid = shmget(key, SHMSZ, 0666)) < 0)
- {
- printf("shmid failed with error %s\n", strerror(errno));
- return(SHMGET_ERROR);
- }
- *shm = (char*)shmat(shmid, NULL, 0);
- if ( (int)(**shm) == -1)
- {
- printf("shmat failed with error %s\n", strerror(errno));
- return(SHMAT_ERROR);
- }
- return 0;
- }
- int main(int argc,char *argv[])
- {
- key_t key1 = 12345;
- int semid;
- unsigned short semval;
- int cnt = 5;
- struct sembuf wait0, wait1, signal0, signal1;
- wait0.sem_num = 0;
- wait0.sem_op = -1;
- wait0.sem_flg = SEM_UNDO;
- wait1.sem_num = 1;
- wait1.sem_op = -1;
- wait1.sem_flg = SEM_UNDO;
- signal0.sem_num = 0;
- signal0.sem_op = 1;
- signal0.sem_flg = SEM_UNDO;
- signal1.sem_num = 1;
- signal1.sem_op = 1;
- signal1.sem_flg = SEM_UNDO;
- semid = semget(key1, 2 ,IPC_CREAT);
- semval = 1;
- semctl(semid,0, SETVAL, semval);
- semctl(semid,1, SETVAL, semval);
- int shmid; // shared memory segment ID
- key_t key = 5678; // key for shared memory
- char *shm, *s; // pointers to shared memory
- allocateMemory(&shm);
- // if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
- // perror("shmget");
- // exit(1);
- // }
- // if ((shm = (char*)shmat(shmid, NULL, 0)) == (char *) -1) {
- // perror("shmat");
- // exit(1);
- // }
- int childpid = fork();
- int messageSentFully = 1;
- int messageReceivedFully = 1;
- if(childpid == 0)
- {
- std::string fullMessage;
- while ( 1 )
- {
- puts("Reader wait1");
- semop(semid,&wait1,1);
- messageReceivedFully = 0;
- //semop(semid,&signal1,1);
- while( messageReceivedFully == 0)
- {
- puts("Reader wait0");
- semop(semid,&wait0,1);
- // if ( messageReceivedFully == 1)
- // {
- // puts("\nReceived message:");
- // }
- char msg[SHMSZ] = {0};
- s = shm;
- for (int i = 0; i < SHMSZ; ++i)
- {
- msg[i] = s[i];
- }
- fullMessage += msg;
- //puts(fullMessage.c_str());
- if ( msg[SHMSZ-1] == 0 )
- {
- messageReceivedFully = 1;
- }
- else
- {
- messageReceivedFully = 0;
- }
- puts("Reader signal0");
- semop(semid,&signal0,1);
- }
- puts("\nReceived message:");
- puts( fullMessage.c_str() );
- if (fullMessage == "quit\n")
- {
- break;
- }
- fullMessage = "";
- puts("Reader signal1");
- semop(semid,&signal1,1);
- }
- semctl(semid,0,IPC_RMID);
- printf("Semaphore removed from the System = %s\n", strerror(errno));
- }
- else
- {
- while(1)
- {
- puts("Writer wait1");
- semop(semid,&wait1,1);
- std::string message;
- puts("\nEnter message:");
- messageSentFully = 0;
- //semop(semid,&signal1, 1);
- while( messageSentFully == 0)
- {
- puts("Writer wait0");
- semop(semid, &wait0, 1);
- puts("Writer signal1");
- semop(semid, &signal1, 1);
- char msg[SHMSZ] = {0};
- int bytesRead = read( STDIN_FILENO, msg, SHMSZ );
- if (bytesRead < SHMSZ)
- {
- messageSentFully = 1;
- }
- else
- {
- messageSentFully = 0;
- }
- s = shm;
- for (int i = 0; i < SHMSZ; ++i)
- {
- s[i] = msg[i];
- }
- message += msg;
- puts("Writer signal0");
- semop(semid, &signal0, 1);
- }
- message = "";
- if ( message == "quit\n")
- {
- break;
- }
- //semop(semid,&wait1,1);
- //semop(semid,&signal1,1);
- }
- }
- shmdt((void*)shm);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement