Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/neutrino.h>
- #include <process.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <time.h>
- int printErrorMessage(char* message, char* shortMessage);
- int main(int argc, char *argv[])
- {
- pid_t server,client;
- int * _memAddress;
- int _channel,i,j,temp;
- int receiveid;
- int _sharedMemory;
- char _data1[10],_data2[10];
- int connection;
- server = getpid();
- if ((_channel = ChannelCreate(0)) == -1)
- printErrorMessage("Could not create channel", "ChannelCreate failure\n", errno);
- if ((client = fork()) == -1)
- printErrorMessage("Could not fork processes", "fork failure\n", errno);
- if (client == 0)
- {
- printf("START: CHILD process \n");
- if ((connection = ConnectAttach(0, server, _channel, 0, 0)) == -1)
- printErrorMessage("Could not attach to channel", "ConnectAttach failure", errno);
- strcpy(_data1, "kaktus");
- _sharedMemory = shm_open(_data1, O_RDWR | O_CREAT, 0777);
- if (_sharedMemory == -1)
- printErrorMessage("Could not open shared memory", "shm_open failure", errno);
- if (ftruncate(_sharedMemory, 10) == -1 )
- printErrorMessage("Could not truncate.", "ftruncate failure", errno);
- _memAddress = (int*)mmap(0, 10, PROT_READ | PROT_WRITE, MAP_SHARED, _sharedMemory, 0 );
- if (_memAddress == MAP_FAILED)
- printErrorMessage("CHILD process mmap failed", "mmap failure", errno);
- srand( time( NULL ) );
- for (i = 0; i < 10; i++){
- _memAddress[i] = rand() % 500 + 100;
- printf("%d ", _memAddress[i]);
- }
- printf("\n");
- if ((MsgSend(connection, &_data1, sizeof(_data1), &_data2, sizeof(_data2))) == -1)
- printErrorMessage("MessageSend failed", "MsgSend failure", errno);
- for(i = 0; i < 10; i++)
- printf("%d ",_memAddress[i]);
- printf("\n");
- shm_unlink(_data1);
- printf("END: CHILD process \n");
- }
- else {
- int status;
- pid_t waitForClient;
- //odbieranie
- printf("START: MOTHER process\n");
- if((receiveid = MsgReceive(_channel, &_data1, sizeof(_data1), NULL)) == -1)
- printErrorMessage("Receiving message failed", "MsgReceive failure", errno);
- _sharedMemory = shm_open(_data1, O_RDWR | O_CREAT, 0777);
- if(_sharedMemory == -1)
- printErrorMessage("Could not open shared memory", "shm_open failure", errno);
- _memAddress = (int*)mmap(0,10,PROT_READ | PROT_WRITE,MAP_SHARED, _sharedMemory, 0 );
- if(_memAddress == MAP_FAILED)
- printErrorMessage("MOTHER process mmap failed", "mmap failure", errno);
- for(i = 0; i < 10; i++){
- for( j=i+1;j<10;j++){
- if(_memAddress[i]>_memAddress[j]){
- temp = _memAddress[i];
- _memAddress[i] = _memAddress[j];
- _memAddress[j] = temp;
- }
- }
- }
- close(_sharedMemory);
- if((MsgReply(receiveid,0,&_data1, sizeof(_data1))) == -1)
- printErrorMessage("Message reply failed.", "Message error", errno);
- do {
- waitForClient = waitpid(client, &status, 0);
- } while(WIFEXITED(status) == 0 );
- printf("END: MOTHER process\n");
- }
- return EXIT_SUCCESS;
- }
- int printErrorMEssage(char* message, char* shortMessage, int err){
- printf(message);
- printf(" %d \n", err);
- perror(shortMessage);
- return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement