Created
April 4, 2017 04:17
-
-
Save Zulcom/46fbcf951ed95aff343fbbb74d520b4e to your computer and use it in GitHub Desktop.
Пример 5й лабы от Шарова В.В.
This file contains 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 <unistd.h> | |
#include <sys/ipc.h> | |
#include <stdio.h> | |
#include <sys/msg.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
#define TYPE_MSG 1 | |
typedef void signalfunction(int); | |
int msgID; | |
// pid_t ppid; | |
void* childrenFE; | |
int numChildren, Time; | |
struct structMSG{ | |
int Type; | |
int Data; | |
unsigned int senderPID; | |
}msg; | |
void sendMSG(int data, int msgid) | |
{ | |
msg.Type = TYPE_MSG; | |
msg.Data = data; | |
msgsnd(msgid, &msg, sizeof(int), 0); | |
} | |
void fillArray(void* FE, size_t size, int range[2]){ | |
int* Array = (int*)FE; | |
for (int i = 0; i < size; i++){ | |
Array[i] = rand() % range[1] - range[0]; | |
} | |
} | |
void printArray(void* FE, size_t size, int rowLen) | |
{ | |
int* Array = (int*)FE; | |
for (int i = 0; i < size; i++){ | |
printf("[%d]\t", Array[i]); | |
if ((i + 1) % rowLen == 0){ | |
printf("\n"); | |
} | |
} | |
} | |
int calcSumRow(void* FE, int rowLen, int noRow) | |
{ | |
int* Array = (int*)FE; | |
int sum = 0; | |
int beg = rowLen * noRow; | |
int end = rowLen * (noRow + 1); | |
for (int i = beg; i < end; i++){ | |
if (Array[i] > 0 && (Array[i] % 2) != 0){ | |
sum += Array[i]; | |
} | |
} | |
return sum; | |
} | |
int calcSumColumn(void* FE, int rowLen, int noCol) | |
{ | |
int* Array = (int*)FE; | |
int end = rowLen * rowLen - 1; | |
int sum = 0, index; | |
for (int i = 0; i < end; i += rowLen){ | |
index = i + noCol; | |
if (Array[index] > 0 && (Array[index] % 2) != 0){ | |
sum += Array[index]; | |
} | |
} | |
return sum; | |
} | |
void printRes(void* rowFE, void* colFE, int rowLen) | |
{ | |
printf("\n"); | |
int* sRow = (int*)rowFE; | |
int* sCol = (int*)colFE; | |
for (int i = 0; i < rowLen; i++){ | |
printf("Строка %d: %d.\n", i, sRow[i]); | |
} | |
printf("\n"); | |
for (int i = 0; i < rowLen; i++){ | |
printf("Столбец %d: %d.\n", i, sCol[i]); | |
} | |
} | |
void terminate() | |
{ | |
printf("Прошло %d секунд. Завершение работы программы...\n\n", Time); | |
pid_t* children = (pid_t*)childrenFE; | |
for (int i = 0; i < numChildren; i++){ | |
kill(children[i], SIGTERM); | |
} | |
} | |
void handlerTerm() | |
{ | |
printf("Процесс с PID %d завершен.\n", getpid()); | |
exit(0); | |
} | |
int main() | |
{ | |
system("clear"); | |
// ppid = getpid(); | |
printf("Время работы программы: "); | |
scanf("%d", &Time); | |
alarm(Time); | |
signal(SIGTERM, handlerTerm); | |
signal(SIGALRM, terminate); | |
int size2; | |
int range[] = {20, 30}; | |
msgID = msgget(IPC_PRIVATE, IPC_CREAT | 0666); | |
printf("Кол-во строк квадратной матрицы: "); | |
scanf("%d", &size2); | |
printf("\nМатрица:\n\n"); | |
int A[size2][size2], sumsCol[size2], sumsRow[size2]; | |
int countArray = sizeof A / sizeof A[0][0]; | |
pid_t children[size2]; | |
fillArray(&A[0][0], countArray, range); | |
printArray(&A[0][0], countArray, size2); | |
size_t sizeInt = sizeof(int); | |
for (int i = 0; i < size2; i++){ | |
if (fork() == 0){ | |
children[i] = getpid(); | |
sendMSG(calcSumRow(&A[0][0], size2, i), msgID); | |
sendMSG(calcSumColumn(&A[0][0], size2, i), msgID); | |
while(1){ | |
sleep(1); | |
} | |
} | |
msgrcv(msgID, &msg, sizeInt, 0, 0); | |
sumsRow[i] = msg.Data; | |
msgrcv(msgID, &msg, sizeInt, 0, 0); | |
sumsCol[i] = msg.Data; | |
} | |
childrenFE = &children[0]; | |
numChildren = size2; | |
printRes(&sumsCol[0], &sumsRow[0], size2); | |
printf("\n"); | |
while(1) pause(); | |
} |
This file contains 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 <unistd.h> | |
#include <sys/ipc.h> | |
#include <stdio.h> | |
#include <sys/msg.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
#define TYPE_MSG 1 | |
int msgID; | |
void* childrenFE; | |
int numChildren, Time; | |
struct structMSG{ | |
int Type; | |
int Data; | |
unsigned int senderPID; | |
}msg; | |
void sendMSG(int data, int msgid) | |
{ | |
msg.Type = TYPE_MSG; | |
msg.Data = data; | |
msgsnd(msgid, &msg, sizeof(int), 0); | |
} | |
void fillArray(void* FE, size_t size, int range[2]){ | |
int* Array = (int*)FE; | |
for (int i = 0; i < size; i++){ | |
Array[i] = rand() % range[1] - range[0]; | |
} | |
} | |
void printArray(void* FE, size_t size, int rowLen) | |
{ | |
int* Array = (int*)FE; | |
for (int i = 0; i < size; i++){ | |
printf("[%d]\t", Array[i]); | |
if ((i + 1) % rowLen == 0){ | |
printf("\n"); | |
} | |
} | |
} | |
int calcSumRow(void* FE, int rowLen, int noRow) | |
{ | |
int* Array = (int*)FE; | |
int sum = 0; | |
int beg = rowLen * noRow; | |
int end = rowLen * (noRow + 1); | |
for (int i = beg; i < end; i++){ | |
if (Array[i] > 0 && (Array[i] % 2) != 0){ | |
sum += Array[i]; | |
} | |
} | |
return sum; | |
} | |
int calcSumColumn(void* FE, int rowLen, int noCol) | |
{ | |
int* Array = (int*)FE; | |
int end = rowLen * rowLen - 1; | |
int sum = 0, index; | |
for (int i = 0; i < end; i += rowLen){ | |
index = i + noCol; | |
if (Array[index] > 0 && (Array[index] % 2) != 0){ | |
sum += Array[index]; | |
} | |
} | |
return sum; | |
} | |
void printRes(void* rowFE, void* colFE, int rowLen) | |
{ | |
printf("\n"); | |
int* sRow = (int*)rowFE; | |
int* sCol = (int*)colFE; | |
for (int i = 0; i < rowLen; i++){ | |
printf("Строка %d: %d.\n", i, sRow[i]); | |
} | |
printf("\n"); | |
for (int i = 0; i < rowLen; i++){ | |
printf("Столбец %d: %d.\n", i, sCol[i]); | |
} | |
} | |
void terminate() | |
{ | |
printf("Прошло %d секунд. Завершение работы программы...\n\n", Time); | |
pid_t* children = (pid_t*)childrenFE; | |
for (int i = 0; i < numChildren; i++){ | |
kill(children[i], SIGTERM); | |
} | |
} | |
void handlerTerm() | |
{ | |
printf("Процесс с PID %d завершен.\n", getpid()); | |
exit(0); | |
} | |
int main() | |
{ | |
system("clear"); | |
printf("Время работы программы: "); | |
scanf("%d", &Time); | |
alarm(Time); | |
struct sigaction sigact; | |
sigemptyset(&sigact.sa_mask); | |
sigact.sa_flags = 0; | |
sigact.sa_handler = terminate; | |
sigaction(SIGALRM, &sigact, 0); | |
int size2; | |
int range[] = {20, 30}; | |
msgID = msgget(IPC_PRIVATE, IPC_CREAT | 0666); | |
printf("Кол-во строк квадратной матрицы: "); | |
scanf("%d", &size2); | |
printf("\nМатрица:\n\n"); | |
int A[size2][size2], sumsCol[size2], sumsRow[size2]; | |
int countArray = sizeof A / sizeof A[0][0]; | |
pid_t children[size2]; | |
fillArray(&A[0][0], countArray, range); | |
printArray(&A[0][0], countArray, size2); | |
size_t sizeInt = sizeof(int); | |
for (int i = 0; i < size2; i++){ | |
if (fork() == 0){ | |
children[i] = getpid(); | |
sendMSG(calcSumRow(&A[0][0], size2, i), msgID); | |
sendMSG(calcSumColumn(&A[0][0], size2, i), msgID); | |
struct sigaction sigact1; | |
sigemptyset(&sigact1.sa_mask); | |
sigact1.sa_flags = 0; | |
sigact1.sa_handler = handlerTerm; | |
sigaction(SIGTERM, &sigact1, 0); | |
while(1){ | |
sleep(1); | |
} | |
} | |
msgrcv(msgID, &msg, sizeInt, 0, 0); | |
sumsRow[i] = msg.Data; | |
msgrcv(msgID, &msg, sizeInt, 0, 0); | |
sumsCol[i] = msg.Data; | |
} | |
childrenFE = &children[0]; | |
numChildren = size2; | |
printRes(&sumsCol[0], &sumsRow[0], size2); | |
printf("\n"); | |
while(1) pause(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment