Created
March 28, 2012 14:37
-
-
Save libbkmz/2226698 to your computer and use it in GitHub Desktop.
stranger-lab-4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/ipc.h> | |
#include <sys/msg.h> | |
#include <sys/wait.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
const int N1 = 7; | |
const int N2 = 4; | |
int A[N1] = {1,2,3,4,5,6,7}; //массивы | |
// int B[N2] = {9,3,5,7}; | |
long result1, result2; | |
int summa (int *array, int size ) | |
{ | |
unsigned int out=0; | |
for(int i=0;i<size;i++) | |
{ | |
out+=array[i]; | |
} | |
return out; | |
} | |
int proizved (int *array, int size) | |
{ | |
unsigned int out=1; | |
for(int i=0;i<size;i++) | |
{ | |
out*=array[i]; | |
} | |
return out; | |
} | |
int main() | |
{ | |
int queue1 = msgget(0x1000, 0644|IPC_CREAT); | |
int queue2 = msgget(0x1001, 0644|IPC_CREAT); | |
//child | |
unsigned int data; | |
data = summa(A, N1); // calculateing data_1 for parent | |
msgsnd(queue1,&data, sizeof(data), 0); // send data_1 to parent | |
msgrcv(queue2, &data, sizeof(data), 0, 0); // receiveng data from parent, | |
result1 = data; // save date from parent | |
data = proizved(A, N1); // calc data_2 for parent | |
msgsnd(queue1,&data, sizeof(data), 0); // send data_2 to parent | |
msgrcv(queue2, &data, sizeof(data), 0, 0); // receive data from parent | |
result2 = data; // save data from parent | |
usleep(100);// for beauty print | |
printf("B summa: %li\n", result1); | |
printf("B proizved: %li\n", result2); | |
_exit(0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/ipc.h> | |
#include <sys/msg.h> | |
#include <sys/wait.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
const int N1 = 7; | |
const int N2 = 4; | |
// int A[N1] = {1,2,3,4,5,6,7}; //массивы | |
int B[N2] = {9,3,5,7}; | |
long result1, result2; | |
int summa (int *array, int size ) | |
{ | |
unsigned int out=0; | |
for(int i=0;i<size;i++) | |
{ | |
out+=array[i]; | |
} | |
return out; | |
} | |
int proizved (int *array, int size) | |
{ | |
unsigned int out=1; | |
for(int i=0;i<size;i++) | |
{ | |
out*=array[i]; | |
} | |
return out; | |
} | |
int main() | |
{ | |
int queue1 = msgget(0x1000, 0644|IPC_CREAT); | |
int queue2 = msgget(0x1001, 0644|IPC_CREAT); | |
pid_t pid = fork(); | |
//parent | |
unsigned int data; | |
data = summa(B, N2); | |
msgsnd(queue2,&data, sizeof(data), 0); | |
msgrcv(queue1, &data, sizeof(data), 0, 0); | |
result1 = data; | |
data = proizved(B, N2); | |
msgsnd(queue2,&data, sizeof(data), 0); | |
msgrcv(queue1, &data, sizeof(data), 0, 0); | |
result2 = data; | |
printf("A summa: %li\n", result1); | |
printf("A proizved: %li\n", result2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment