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 <sys/neutrino.h>
- #include <sys/netmgr.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #include <time.h>
- struct msg {
- char name[100];
- int size;
- int direction; // 0 - asc, 1 - desc
- int command; // 0 - sort, 1 - max, 2 - min, 3 - avg
- };
- int main(void) {
- errno = EOK;
- int channelID; // Channel ID
- pid_t pID; // Process ID
- if ((channelID = ChannelCreate(0)) == -1) { //tworzenie kanału komunikacji
- return EXIT_FAILURE;
- }
- if ((pID = fork()) == -1) { //tworzenie procesu
- perror("Fork failure!");
- return EXIT_FAILURE;
- }
- else if (pID == 0) {
- //PROCES POTOMNY
- char sharedMemoryObject[] = "/shm_object";
- int size = 5;
- int connectionID;
- int command = 2;
- char replyMessage[100];
- printf("CHILD CHANNEL ID: %d\n", channelID); //obecnie używany kanał komunikacji
- printf("CHILD Process: PID:%d\n", getpid()); //wyprintowanie procesu
- connectionID = ConnectAttach(ND_LOCAL_NODE, getpid(), channelID, 0, 0); //ustawienie kanału komunikacji pomiędzy procesem a kanałem
- // ND_LOCAL_NODE - lokalne połączenie, getppid() - pobranie ID procesu, channelID - id kanału, najmniejsze połączenie, flagi
- printf("CHILD Connection id: %d\n", connectionID);
- // Opening sharing memory descriptor
- int fileDescription = shm_open(sharedMemoryObject, O_RDWR | O_CREAT, 0777); //O_RDWR readAndWrite access, O_CREAT - tworzony gdy go nie ma (plik dzielonej pamięci) z mode, 0777 - uprawnienia
- ftruncate(fileDescription, size * sizeof(int)); // drugi argument to długoś jaką chcemy by ten plik miał w bajtach
- int* addres = (int*)mmap(0, size * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fileDescription, 0); //mmap mapuje rejon pamięci w przestrzen adresową procesu
- srand(time(NULL));
- for (int i = 0; i < size; i++) {
- addres[i] = rand() % 100;
- }
- for (int i = 0; i < size; i++) {
- printf("%d\n", addres[i]);
- }
- struct msg m;
- strcpy(m.name, sharedMemoryObject);
- m.size = size;
- m.direction = 0;
- m.command = command;
- MsgSend(connectionID, &m, sizeof(m), replyMessage, 100); //MsgSend() wysłanie wiadomości do kanału. &m adres bufora z wiadomością do wysłania, replyMessage adres bufora z odpowiedzią
- for (int i = 0; i < size; i++) {
- printf("%d\n", addres[i]);
- }
- printf("CHILD RMSG = %s\n", replyMessage);
- munmap(addres, size * sizeof(int));
- close(fileDescription);
- shm_unlink("/shm_object"); //zamknięcie pliku
- ConnectDetach(connectionID); //zamknięcie połączenia
- }
- else {
- /* PARENT PROCESS */
- char rmsg[100] = "Done";
- struct msg m;
- printf("PARENT Process: PID:%d\n", getpid());
- int messageID = MsgReceive(channelID, &m, sizeof(m), NULL);
- printf("PARENT MESSAGE FROM CLIENT %d:\n", pID);
- int fd = shm_open(m.name, O_RDWR, 0777);
- int* address2 = (int*)mmap(0, m.size * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- switch (m.command) {
- case 0:
- for (int i = 0; i < m.size; i++) { //bubble sort
- for (int j = 0; j < m.size - i - 1; j++) {
- if ((m.direction == 0) ? address2[j] > address2[j + 1] : address2[j] < address2[j + 1]) {
- int temp = address2[j];
- address2[j] = address2[j + 1];
- address2[j + 1] = temp;
- }
- }
- }
- break;
- case 1:
- int max = address2[0];
- for (int i = 1; i < m.size; i++) {
- if (address2[i] > max) {
- max = address2[i];
- }
- }
- printf("MAX = %d\n", max);
- break;
- case 2:
- int min = address2[0];
- for (int i = 1; i < m.size; i++) {
- if (address2[i] < min) {
- min = address2[i];
- }
- }
- printf("MIN = %d\n", min);
- break;
- case 3:
- int avg = 0;
- int sum = 0;
- for (int i = 0; i < m.size; i++) {
- sum += address2[i];
- }
- avg = sum / m.size;
- printf("Average = %d\n", avg);
- break;
- default:
- printf("Not implemented.\n");
- }
- munmap(address2, m.size * sizeof(int));
- close(fd);
- MsgReply(messageID, 0, rmsg, sizeof(rmsg) + 1);
- sleep(100);
- ChannelDestroy(channelID);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement