Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #include <sys/netmgr.h>
- #include <sys/neutrino.h>
- #include <process.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <errno.h>
- struct message {
- char name[100];
- int size;
- };
- int main(void)
- {
- /* Try to create channel */
- int channel_id;
- if ((channel_id = ChannelCreate(0)) == -1){
- printf("Channel create failed!!!");
- return EXIT_FAILURE;
- }
- /* Try to fork(), if not exit with failure */
- pid_t pid = fork();
- if(pid == -1)
- {
- perror("fork");
- return EXIT_FAILURE;
- }else if(pid != 0)
- {
- /*parent*/
- printf("Parent pid: %d\n",getpid());
- printf("Parent channel_id: %d\n",channel_id);
- struct msg m;
- //char msg[100];
- int message_id = MsgRecive(channel_id, &m, sizeof(m), NULL);
- printf(Parent message_id\n",message_id);
- printf("Parent recived message: %s\n",m.name);
- int fd = shm_open( m.name, O_RDWR, 0777 );
- int* addr = (int*)mmap( 0, m.size*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
- for (int i=0; i<m.size;i++)
- for(int j=0;j<m.size-1-i;j++)
- {
- // if ((m.direction==0)?addr[j]>addr[j+1]:addr[j]<addr[j+1]){
- if (addr[j]>addr[j+1])
- {
- int tmp=addr[j];
- addr[j]=addr[j+1];
- addr[j+1]=tmp;
- }
- }
- char rmsg[100];
- //sprintf(rmsg, "%d", strlen(msg));
- MsgReply(message_id, 0, rmsg, strlen(rmsg)+1);
- munmap(addr,m.size*sizeof(int));
- close(fd);
- ChannelDestroy(channel_id);
- }else
- {
- /*child*/
- int connection_id = ConnectAttach(0, getppid(), channel_id, 0, 0);
- printf("Child connection_id: %d\n",connection_id);
- printf("Child pid: %d\n"getpid());
- //char msg[100] = "abcd";
- //sleep(60);
- char name[100]="/shm_object";
- int size=10;
- char rmsg[100];
- int fd = shm_open( "/shm_object", O_RDWR | O_CREAT, 0777 );
- ftruncate(fd, size*sizeof(int));
- int* addr = (int*)mmap( 0, size*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
- srand(time(NULL));
- for (int i=0;i<size;i++){
- addr[i]=rand()%100;
- }
- for (int i=0;i<size;i++){
- printf("%d\n",addr[i]);
- }
- printf("\n\n");
- //sleep(30);
- struct msg m;
- strcpy(m.name,name);
- m.size=size;
- m.direction=1;
- MsgSend(connection_id, &m, sizeof(m), rmsg, 100);
- printf("Child recived rep: %s\n",rmsg);
- for (int i=0;i<size;i++)
- printf("%d\n",addr[i]);
- ConnectDetach(connection_id);
- munmap(addr,size*sizeof(int));
- close(fd);
- shm_unlink("/shm_object");
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement