mirror of
https://github.com/ada-dmitry/TMP_C712.git
synced 2026-09-24 01:00:19 +00:00
restart rep
This commit is contained in:
@@ -0,0 +1,80 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define SIZE 100
|
||||||
|
|
||||||
|
void matrixMultiply(int matrix1[SIZE][SIZE], int matrix2[SIZE][SIZE], int result[SIZE][SIZE], int n) {
|
||||||
|
int i, j, k;
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
for (j = 0; j < n; j++) {
|
||||||
|
result[i][j] = 0;
|
||||||
|
for (k = 0; k < n; k++) {
|
||||||
|
result[i][j] += matrix1[i][k] * matrix2[k][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char* file1 = "matrix1.txt"; // Имя файла для первой матрицы
|
||||||
|
char* file2 = "matrix2.txt"; // Имя файла для второй матрицы
|
||||||
|
char* fileResult = "result_matrix.txt"; // Имя файла для результирующей матрицы
|
||||||
|
|
||||||
|
int matrix1[SIZE][SIZE], matrix2[SIZE][SIZE], result[SIZE][SIZE]; // Матрицы
|
||||||
|
|
||||||
|
// Открываем файл первой матрицы
|
||||||
|
int fd1 = open(file1, O_RDONLY);
|
||||||
|
if (fd1 == -1) {
|
||||||
|
perror("Не удалось открыть файл для первой матрицы");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Считываем данные первой матрицы из файла
|
||||||
|
int bytesRead = read(fd1, matrix1, sizeof(int) * SIZE * SIZE);
|
||||||
|
if (bytesRead == -1) {
|
||||||
|
perror("Ошибка при чтении первой матрицы");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
close(fd1); // Закрываем файл
|
||||||
|
|
||||||
|
// Открываем файл второй матрицы
|
||||||
|
int fd2 = open(file2, O_RDONLY);
|
||||||
|
if (fd2 == -1) {
|
||||||
|
perror("Не удалось открыть файл для второй матрицы");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Считываем данные второй матрицы из файла
|
||||||
|
bytesRead = read(fd2, matrix2, sizeof(int) * SIZE * SIZE);
|
||||||
|
if (bytesRead == -1) {
|
||||||
|
perror("Ошибка при чтении второй матрицы");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
close(fd2); // Закрываем файл
|
||||||
|
|
||||||
|
int n = bytesRead / (sizeof(int) * SIZE); // Размер матрицы
|
||||||
|
|
||||||
|
matrixMultiply(matrix1, matrix2, result, n); // Выполняем умножение матриц
|
||||||
|
|
||||||
|
// Открываем файл для результирующей матрицы
|
||||||
|
int fdResult = open(fileResult, O_WRONLY | O_CREAT | O_TRUNC);
|
||||||
|
if (fdResult == -1) {
|
||||||
|
perror("Не удалось открыть файл для результирующей матрицы");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Записываем результирующую матрицу в файл
|
||||||
|
int bytesWritten = write(fdResult, result, sizeof(int) * n * n);
|
||||||
|
if (bytesWritten == -1) {
|
||||||
|
perror("Ошибка при записи результирующей матрицы");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
close(fdResult); // Закрываем файл
|
||||||
|
|
||||||
|
printf("Результирующая матрица успешно записана в файл '%s'\n", fileResult);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,52 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
#include <sys/sem.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(const char *pname)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [-cx] pathname proj-id num-sems\n",
|
||||||
|
pname);
|
||||||
|
fprintf(stderr, " -c Use IPC_CREAT flag\n");
|
||||||
|
fprintf(stderr, " -x Use IPC_EXCL flag\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int semid, nsems, flags, opt;
|
||||||
|
key_t key;
|
||||||
|
|
||||||
|
flags = 0;
|
||||||
|
while ((opt = getopt(argc, argv, "cx")) != -1) {
|
||||||
|
switch (opt) {
|
||||||
|
case 'c': flags |= IPC_CREAT; break;
|
||||||
|
case 'x': flags |= IPC_EXCL; break;
|
||||||
|
default: usage(argv[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc != optind + 3)
|
||||||
|
usage(argv[0]);
|
||||||
|
|
||||||
|
key = ftok(argv[optind], argv[optind + 1][0]);
|
||||||
|
if (key == -1) {
|
||||||
|
perror("ftok");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
nsems = atoi(argv[optind + 2]);
|
||||||
|
|
||||||
|
semid = semget(key, nsems, flags | 0600);
|
||||||
|
if (semid == -1) {
|
||||||
|
perror("semget");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("ID = %d\n", semid);
|
||||||
|
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <sys/sem.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define errExit(msg) \
|
||||||
|
do \
|
||||||
|
{ \
|
||||||
|
perror(msg); \
|
||||||
|
exit(EXIT_FAILURE); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
union semun
|
||||||
|
{ /* Used in calls to semctl() */
|
||||||
|
int val;
|
||||||
|
struct semid_ds *buf;
|
||||||
|
unsigned short *array;
|
||||||
|
#if defined(__linux__)
|
||||||
|
struct seminfo *__buf;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#define MEM_SIZE 4096
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/ipc.h>
|
||||||
|
#include <sys/sem.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
|
#include "svshm_string.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int semid, shmid;
|
||||||
|
char *addr;
|
||||||
|
union semun arg, dummy;
|
||||||
|
struct sembuf sop;
|
||||||
|
|
||||||
|
/* Create shared memory and semaphore set containing one
|
||||||
|
semaphore. */
|
||||||
|
|
||||||
|
shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
|
||||||
|
if (shmid == -1)
|
||||||
|
errExit("shmget");
|
||||||
|
|
||||||
|
semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
|
||||||
|
if (semid == -1)
|
||||||
|
errExit("semget");
|
||||||
|
|
||||||
|
/* Attach shared memory into our address space. */
|
||||||
|
|
||||||
|
addr = shmat(shmid, NULL, SHM_RDONLY);
|
||||||
|
if (addr == (void *)-1)
|
||||||
|
errExit("shmat");
|
||||||
|
|
||||||
|
/* Initialize semaphore 0 in set with value 1. */
|
||||||
|
|
||||||
|
arg.val = 1;
|
||||||
|
if (semctl(semid, 0, SETVAL, arg) == -1)
|
||||||
|
errExit("semctl");
|
||||||
|
|
||||||
|
printf("shmid = %d; semid = %d\n", shmid, semid);
|
||||||
|
|
||||||
|
/* Wait for semaphore value to become 0. */
|
||||||
|
|
||||||
|
sop.sem_num = 0;
|
||||||
|
sop.sem_op = 0;
|
||||||
|
sop.sem_flg = 0;
|
||||||
|
|
||||||
|
if (semop(semid, &sop, 1) == -1)
|
||||||
|
errExit("semop");
|
||||||
|
|
||||||
|
/* Print the string from shared memory. */
|
||||||
|
|
||||||
|
printf("%s\n", addr);
|
||||||
|
|
||||||
|
/* Remove shared memory and semaphore set. */
|
||||||
|
|
||||||
|
if (shmctl(shmid, IPC_RMID, NULL) == -1)
|
||||||
|
errExit("shmctl");
|
||||||
|
if (semctl(semid, 0, IPC_RMID, dummy) == -1)
|
||||||
|
errExit("semctl");
|
||||||
|
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/sem.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
|
#include "svshm_string.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int semid, shmid;
|
||||||
|
char *addr;
|
||||||
|
size_t len;
|
||||||
|
struct sembuf sop;
|
||||||
|
|
||||||
|
if (argc != 4)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s shmid semid string\n", argv[0]);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
len = strlen(argv[3]) + 1; /* +1 to include trailing '\0' */
|
||||||
|
if (len > MEM_SIZE)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "String is too big!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get object IDs from command-line. */
|
||||||
|
if((key = ftok("bin/sh", 'A')) = -1) errExit
|
||||||
|
shmid = atoi(argv[1]);
|
||||||
|
semid = atoi(argv[2]);
|
||||||
|
|
||||||
|
/* Attach shared memory into our address space and copy string
|
||||||
|
(including trailing null byte) into memory. */
|
||||||
|
|
||||||
|
addr = shmat(shmid, NULL, 0);
|
||||||
|
if (addr == (void *)-1)
|
||||||
|
errExit("shmat");
|
||||||
|
|
||||||
|
memcpy(addr, argv[3], len);
|
||||||
|
|
||||||
|
/* Decrement semaphore to 0. */
|
||||||
|
|
||||||
|
sop.sem_num = 0;
|
||||||
|
sop.sem_op = -1;
|
||||||
|
sop.sem_flg = 0;
|
||||||
|
|
||||||
|
if (semop(semid, &sop, 1) == -1)
|
||||||
|
errExit("semop");
|
||||||
|
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#define NUM 4
|
||||||
|
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||||
|
#define errExit1(msg) { perror(msg); _exit(EXIT_FAILURE); }
|
||||||
|
|
||||||
|
void child_handler(int sig) {
|
||||||
|
pid_t pid;
|
||||||
|
while((pid = waitpid(-1, NULL, WNOHANG)) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void int_handler(int sig){
|
||||||
|
printf("Ctrl-C pressed, do nothing...\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
struct sigaction new_act, old_act;
|
||||||
|
sigemptyset (&new_act.sa_mask);
|
||||||
|
new_act.sa_flags = 0;
|
||||||
|
new_act.sa_flags = SA_RESTART;
|
||||||
|
new_act.sa_handler = child_handler;
|
||||||
|
|
||||||
|
sigaction (SIGCHLD, &new_act, &old_act);
|
||||||
|
|
||||||
|
struct sigaction nint_act, oint_act;
|
||||||
|
sigemptyset (&nint_act.sa_mask);
|
||||||
|
nint_act.sa_flags = 0;
|
||||||
|
nint_act.sa_flags = SA_RESTART;
|
||||||
|
nint_act.sa_handler = int_handler;
|
||||||
|
|
||||||
|
sigaction (SIGINT, &nint_act, &oint_act);
|
||||||
|
|
||||||
|
int pipefd[NUM][2];
|
||||||
|
pid_t cpid[NUM];
|
||||||
|
|
||||||
|
int nfds, num_open_fds;
|
||||||
|
struct pollfd *pfds;
|
||||||
|
int ready;
|
||||||
|
|
||||||
|
unsigned char i,tt;
|
||||||
|
char buf;
|
||||||
|
int rr;
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
num_open_fds = nfds = NUM;
|
||||||
|
pfds = calloc(nfds, sizeof(struct pollfd));
|
||||||
|
if (pfds == NULL) errExit("malloc");
|
||||||
|
|
||||||
|
for (i=0;i<NUM;i++){
|
||||||
|
tt=(unsigned char)rand()%9;
|
||||||
|
dprintf(STDOUT_FILENO,"child %d life time is %X\n",i,tt);
|
||||||
|
|
||||||
|
if (pipe(pipefd[i]) == -1) errExit("pipe");
|
||||||
|
pfds[i].fd = pipefd[i][0];
|
||||||
|
pfds[i].events = POLLIN;
|
||||||
|
if ((cpid[i] = fork()) == -1) errExit("fork");
|
||||||
|
|
||||||
|
if (cpid[i] == 0) {
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
for (int j=0; j<i; j++){
|
||||||
|
close(pipefd[j][0]);
|
||||||
|
close(pipefd[j][1]);
|
||||||
|
}
|
||||||
|
write(pipefd[i][1], argv[1], strlen(argv[1]));
|
||||||
|
write(pipefd[i][1], &i, 1);
|
||||||
|
write(pipefd[i][1], &tt, 1);
|
||||||
|
sleep(tt);
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
_exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j=0; j<i; j++) close(pipefd[j][1]);
|
||||||
|
|
||||||
|
dprintf(STDOUT_FILENO,"Opened %d pipes, waiting...\n",num_open_fds);
|
||||||
|
|
||||||
|
while (num_open_fds > 0) {
|
||||||
|
|
||||||
|
do{
|
||||||
|
dprintf(STDOUT_FILENO,"\ntry poll:\n");
|
||||||
|
for (i = 0; i < NUM; i++) pfds[i].revents=0;
|
||||||
|
ready = poll(pfds, nfds, -1);
|
||||||
|
if(ready == -1)
|
||||||
|
switch (errno) {
|
||||||
|
case EINTR: dprintf(STDOUT_FILENO,"\nPoll interrupted, repeat.\n");
|
||||||
|
continue;
|
||||||
|
default: errExit("poll");
|
||||||
|
}
|
||||||
|
} while (ready <= 0);
|
||||||
|
|
||||||
|
for (i = 0; i < NUM; i++) {
|
||||||
|
if (pfds[i].revents != 0) {
|
||||||
|
dprintf(STDOUT_FILENO," fd=%d; events: %7s%8s%9s%8s%8s ", pfds[i].fd,
|
||||||
|
(pfds[i].revents & POLLIN) ? "POLLIN " : "",
|
||||||
|
(pfds[i].revents & POLLHUP) ? "POLLHUP " : "",
|
||||||
|
(pfds[i].revents & POLLNVAL) ? "POLLNVAL " : "",
|
||||||
|
(pfds[i].revents & POLLPRI) ? "POLLPRI " : "",
|
||||||
|
(pfds[i].revents & POLLERR) ? "POLLERR " : "");
|
||||||
|
if (pfds[i].revents & POLLIN) {
|
||||||
|
|
||||||
|
do {
|
||||||
|
rr=read(pfds[i].fd, &buf, 1);
|
||||||
|
if (rr < 0)
|
||||||
|
switch (errno) {
|
||||||
|
case EINTR: dprintf(STDOUT_FILENO,"\nRead interrupted, repeat.\n");
|
||||||
|
continue;
|
||||||
|
default: errExit("read");
|
||||||
|
}
|
||||||
|
if(rr == 0 )
|
||||||
|
dprintf(STDOUT_FILENO,"\n");
|
||||||
|
else
|
||||||
|
dprintf(STDOUT_FILENO,"[%2X]",buf);
|
||||||
|
} while (rr > 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if ((pfds[i].revents & (POLLHUP | ~POLLIN))) {
|
||||||
|
dprintf(STDOUT_FILENO,"Closing [%d], pipefd %d, pfds.fd %d\n",i,pipefd[i][0],pfds[i].fd);
|
||||||
|
if (close(pfds[i].fd) < 0 ) errExit("close pfds");
|
||||||
|
pfds[i].fd = -1;
|
||||||
|
pfds[i].events=0;
|
||||||
|
num_open_fds--;
|
||||||
|
dprintf(STDOUT_FILENO,"Still opened fds %d\n",num_open_fds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Niggers");
|
||||||
|
while (1) pause;
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
LIB =
|
||||||
|
|
||||||
|
default: run
|
||||||
|
|
||||||
|
build:
|
||||||
|
gcc func.h func.c main.c -o matrix_app ${LIB}
|
||||||
|
|
||||||
|
run: build
|
||||||
|
./matrix_app
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
|
||||||
|
#include "func.h"
|
||||||
|
|
||||||
|
void init_matr(int c, short int mat[c][c])
|
||||||
|
{
|
||||||
|
for (int i = 0; i < c; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < c; j++)
|
||||||
|
{
|
||||||
|
mat[i][j] = rand() % N;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void out_matr(int c, short int mat[c][c])
|
||||||
|
{
|
||||||
|
for (int i = 0; i < c; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < c; j++)
|
||||||
|
{
|
||||||
|
printf("[%d] ", mat[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void trans(int c, short int mat[c][c])
|
||||||
|
{
|
||||||
|
for (int i = 0; i < c; i++)
|
||||||
|
{
|
||||||
|
for (int j = i + 1; j < c; j++)
|
||||||
|
{
|
||||||
|
short c;
|
||||||
|
c = mat[i][j];
|
||||||
|
mat[i][j] = mat[j][i];
|
||||||
|
mat[j][i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <time.h>
|
||||||
|
#define N 10
|
||||||
|
#define n 100
|
||||||
|
|
||||||
|
void init_matr(int c, short int mat[c][c]);
|
||||||
|
void out_matr(int c, short int mat[c][c]);
|
||||||
|
void trans(int c, short int mat[c][c]);
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
|
||||||
|
#include "func.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int pipefd[n][2], fd_ans;
|
||||||
|
caddr_t addr_ans;
|
||||||
|
pid_t cpid[n];
|
||||||
|
short mat1[n][n], mat2[n][n];
|
||||||
|
fd_ans = open(argv[2], O_RDWR | O_CREAT);
|
||||||
|
srand(time(NULL));
|
||||||
|
init_matr(n, mat1);
|
||||||
|
init_matr(n, mat2);
|
||||||
|
out_matr(n, mat1);
|
||||||
|
trans(n, mat2);
|
||||||
|
printf("\n");
|
||||||
|
out_matr(n, mat2);
|
||||||
|
printf("\n");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
if (pipe(pipefd[i]) == -1)
|
||||||
|
{
|
||||||
|
perror("pipe");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
cpid[i] = fork();
|
||||||
|
if (cpid[i] == -1)
|
||||||
|
{
|
||||||
|
perror("fork");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cpid[i] == 0)
|
||||||
|
{
|
||||||
|
int result[n];
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
for (int j = 0; j < i; j++)
|
||||||
|
{
|
||||||
|
close(pipefd[j][0]);
|
||||||
|
close(pipefd[j][1]);
|
||||||
|
}
|
||||||
|
for (int j = 0; j < n; j++)
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int k = 0; k < n; k++)
|
||||||
|
{
|
||||||
|
sum += mat1[i][k] * mat2[j][k];
|
||||||
|
}
|
||||||
|
result[j] = sum;
|
||||||
|
}
|
||||||
|
write(pipefd[i][1], result, n * sizeof(int));
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Resulting mat\n");
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
waitpid(cpid[i], NULL, WNOHANG);
|
||||||
|
int result[n];
|
||||||
|
read(pipefd[i][0], result, n * sizeof(int));
|
||||||
|
for (int j = 0; j < n; j++)
|
||||||
|
{
|
||||||
|
//printf("[%d] ", result[j]);
|
||||||
|
addr_ans = mmap((caddr_t)0, PROT_READ | PROT_WRITE, MAP_SHARED, fd_dst, 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
}
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,91 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <error.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#define SERVER_PATH "/tmp/unix_sock.server"
|
||||||
|
#define CLIENT_PATH "unix_sock.client"
|
||||||
|
#define DATA "Hello from client"
|
||||||
|
#define BUF 128
|
||||||
|
|
||||||
|
int main(int argc,char *argv[]){
|
||||||
|
|
||||||
|
int client_sock, rc, len;
|
||||||
|
struct sockaddr_un server_sockaddr;
|
||||||
|
struct sockaddr_un client_sockaddr;
|
||||||
|
char buf[BUF];
|
||||||
|
size_t bs=BUF;
|
||||||
|
char cpath[22];
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
sprintf(cpath,"%03d-%s",rand()%1000,CLIENT_PATH);
|
||||||
|
len = sizeof(client_sockaddr);
|
||||||
|
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
|
||||||
|
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (client_sock == -1) {
|
||||||
|
perror("socket");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
client_sockaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(client_sockaddr.sun_path, cpath);
|
||||||
|
|
||||||
|
unlink(cpath);
|
||||||
|
rc = bind(client_sock, (struct sockaddr *) &client_sockaddr, len);
|
||||||
|
if (rc == -1){
|
||||||
|
perror("bind");
|
||||||
|
close(client_sock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
server_sockaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(server_sockaddr.sun_path, SERVER_PATH);
|
||||||
|
rc = connect(client_sock, (struct sockaddr *) &server_sockaddr, len);
|
||||||
|
if(rc == -1){
|
||||||
|
perror("connect");
|
||||||
|
close(client_sock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc > 1)
|
||||||
|
strcpy(buf, argv[1]);
|
||||||
|
else
|
||||||
|
strcpy(buf, DATA);
|
||||||
|
|
||||||
|
printf("Sending data...\n");
|
||||||
|
rc = send(client_sock, buf, strlen(buf), 0);
|
||||||
|
if (rc == -1) {
|
||||||
|
perror("send");
|
||||||
|
close(client_sock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("Data sent!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Waiting to recieve data...\n");
|
||||||
|
memset(buf, 0, sizeof(buf));
|
||||||
|
rc = recv(client_sock, buf, bs,0);
|
||||||
|
if (rc == -1) {
|
||||||
|
perror("recv");
|
||||||
|
close(client_sock);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("DATA RECEIVED = %s\n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(client_sock);
|
||||||
|
unlink(cpath);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <error.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||||
|
#define SerrExit(s,msg) do { close(s); perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||||
|
|
||||||
|
#define SERVER_PATH "/tmp/unix_sock.server"
|
||||||
|
#define CLIENT_PATH "unix_sock.client"
|
||||||
|
#define DATA "Hello from client"
|
||||||
|
#define BUF 128
|
||||||
|
#define FINS "exit_s"
|
||||||
|
#define FINC "exit_c"
|
||||||
|
//#define INFO
|
||||||
|
|
||||||
|
int rcvBuffer(int sock_fd){
|
||||||
|
int rcvBufferSize;
|
||||||
|
socklen_t sockOptSize = sizeof(rcvBufferSize);
|
||||||
|
getsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvBufferSize, &sockOptSize);
|
||||||
|
#ifdef INFO
|
||||||
|
printf("initial socket receive buf %d\n", rcvBufferSize);
|
||||||
|
#endif
|
||||||
|
return rcvBufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
int recvCount(const void *s, int size){
|
||||||
|
register int count;
|
||||||
|
register int mark;
|
||||||
|
register char *p;
|
||||||
|
p=(char *)s;
|
||||||
|
count = 0;
|
||||||
|
mark = 0;
|
||||||
|
do {
|
||||||
|
if (*p && !(*(p+1))) count++;
|
||||||
|
#ifdef INFO
|
||||||
|
printf("[%c][%X]\n",*p,*p);
|
||||||
|
#endif
|
||||||
|
p++;
|
||||||
|
mark++;
|
||||||
|
} while (!((*p == '\0') && (*(p+1)=='\0')) && (mark < size));
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc,char *argv[]){
|
||||||
|
|
||||||
|
int client_sock, rc, len;
|
||||||
|
struct sockaddr_un server_sockaddr;
|
||||||
|
struct sockaddr_un client_sockaddr;
|
||||||
|
char obuf[BUF];
|
||||||
|
char **ibuf;
|
||||||
|
size_t obs=BUF,ibs;
|
||||||
|
char cpath[22];
|
||||||
|
|
||||||
|
sprintf(cpath,"%d-%s",getpid(),CLIENT_PATH);
|
||||||
|
len = sizeof(client_sockaddr);
|
||||||
|
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
|
||||||
|
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (client_sock == -1) {
|
||||||
|
perror("socket");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
client_sockaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(client_sockaddr.sun_path, cpath);
|
||||||
|
|
||||||
|
unlink(cpath);
|
||||||
|
rc = bind(client_sock, (struct sockaddr *) &client_sockaddr, len);
|
||||||
|
if (rc == -1) SerrExit(client_sock,"bind");
|
||||||
|
|
||||||
|
server_sockaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(server_sockaddr.sun_path, SERVER_PATH);
|
||||||
|
rc = connect(client_sock, (struct sockaddr *) &server_sockaddr, len);
|
||||||
|
if (rc == -1) SerrExit(client_sock,"connect");
|
||||||
|
|
||||||
|
ibs=rcvBuffer(client_sock);
|
||||||
|
if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
||||||
|
|
||||||
|
do {
|
||||||
|
memset(obuf, 0, obs);
|
||||||
|
printf("> ");
|
||||||
|
scanf("%s",obuf);
|
||||||
|
|
||||||
|
printf("Sending data...\n");
|
||||||
|
rc = send(client_sock, obuf, strlen(obuf)+1, 0);
|
||||||
|
if (rc == -1) {
|
||||||
|
perror("send");
|
||||||
|
close(client_sock);
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
printf("Data sent!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Waiting to recieve data...\n");
|
||||||
|
memset(ibuf, 0, ibs);
|
||||||
|
rc = recv(client_sock, ibuf, ibs,0);
|
||||||
|
if (rc == -1) {
|
||||||
|
perror("recv");
|
||||||
|
close(client_sock);
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
int c = recvCount(ibuf,ibs);
|
||||||
|
#ifdef INFO
|
||||||
|
char *p = (char *)ibuf;
|
||||||
|
printf("count: %d resieved\n",c);
|
||||||
|
for (int i = 0; i < 32; i++){
|
||||||
|
printf("[%c][%X]",*p,*p);
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
#endif
|
||||||
|
#ifndef INFO
|
||||||
|
char p[obs];
|
||||||
|
char *tp = (char *)ibuf;
|
||||||
|
// if ((ibuf=(char **)malloc(ibs)) == NULL ) errExit("malloc");
|
||||||
|
for (int i = 0; i < c; i++){
|
||||||
|
memset(p,0,obs);
|
||||||
|
while (*tp) {strncat(p,tp,1);tp++;}
|
||||||
|
tp++;
|
||||||
|
// printf("DATA RECEIVED [%ld] = %s\n", strlen(ibuf[i]),ibuf[i]);
|
||||||
|
printf("DATA RECEIVED [%ld] = %s\n", strlen(p),p);
|
||||||
|
// printf("DATA RECEIVED [%ld] = %s\n", strlen(p[i]),p[i]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (strcmp(obuf,FINC) && strcmp(obuf,FINS));
|
||||||
|
|
||||||
|
close(client_sock);
|
||||||
|
unlink(cpath);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <error.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||||
|
|
||||||
|
#define SOCK_PATH "/tmp/unix_sock.server"
|
||||||
|
#define DATA "Hello from server"
|
||||||
|
#define BUF 128
|
||||||
|
#define FIN "exit"
|
||||||
|
|
||||||
|
int session(int client_sock, struct sockaddr_un server_sockaddr, struct sockaddr_un client_sockaddr){
|
||||||
|
int rc;
|
||||||
|
char inbuf[BUF];
|
||||||
|
char outbuf[BUF];
|
||||||
|
int bytes_rec = 0;
|
||||||
|
size_t bs = BUF;
|
||||||
|
socklen_t len;
|
||||||
|
len = sizeof(client_sockaddr);
|
||||||
|
rc = getpeername(client_sock, (struct sockaddr *) &client_sockaddr, &len);
|
||||||
|
if (rc == -1){
|
||||||
|
close(client_sock);
|
||||||
|
errExit("getpeer");
|
||||||
|
} else {
|
||||||
|
printf("Client socket filepath: %s\n", client_sockaddr.sun_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("waiting to read...\n");
|
||||||
|
memset(inbuf, 0, bs);
|
||||||
|
bytes_rec = recv(client_sock, inbuf, bs, 0);
|
||||||
|
if (bytes_rec == -1){
|
||||||
|
close(client_sock);
|
||||||
|
errExit("recv");
|
||||||
|
} else {
|
||||||
|
printf("DATA RECEIVED = %s\n", inbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(outbuf, 0, bs);
|
||||||
|
strcpy(outbuf, DATA);
|
||||||
|
printf("Sending data...\n");
|
||||||
|
rc = send(client_sock, outbuf, strlen(outbuf), 0);
|
||||||
|
if (rc == -1) {
|
||||||
|
close(client_sock);
|
||||||
|
errExit("send");
|
||||||
|
} else {
|
||||||
|
printf("Data sent!\n");
|
||||||
|
}
|
||||||
|
close(client_sock);
|
||||||
|
return strcmp(inbuf,FIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
|
||||||
|
int server_sock, client_sock, rc;
|
||||||
|
socklen_t len;
|
||||||
|
struct sockaddr_un server_sockaddr;
|
||||||
|
struct sockaddr_un client_sockaddr;
|
||||||
|
int backlog = 10;
|
||||||
|
|
||||||
|
len = sizeof(server_sockaddr);
|
||||||
|
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
|
||||||
|
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (server_sock == -1) errExit("socket");
|
||||||
|
|
||||||
|
server_sockaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(server_sockaddr.sun_path, SOCK_PATH);
|
||||||
|
|
||||||
|
unlink(SOCK_PATH);
|
||||||
|
rc = bind(server_sock, (struct sockaddr *) &server_sockaddr, len);
|
||||||
|
if (rc == -1){
|
||||||
|
close(server_sock);
|
||||||
|
errExit("bind");
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = listen(server_sock, backlog);
|
||||||
|
if (rc == -1){
|
||||||
|
close(server_sock);
|
||||||
|
errExit("listen");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("socket listening...\n");
|
||||||
|
|
||||||
|
do {
|
||||||
|
client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len);
|
||||||
|
if (client_sock == -1){
|
||||||
|
close(server_sock);
|
||||||
|
close(client_sock);
|
||||||
|
errExit("accept");
|
||||||
|
}
|
||||||
|
} while (session(client_sock,server_sockaddr,client_sockaddr));
|
||||||
|
|
||||||
|
|
||||||
|
close(server_sock);
|
||||||
|
unlink(SOCK_PATH);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <error.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||||
|
|
||||||
|
#define SOCK_PATH "/tmp/unix_sock.server"
|
||||||
|
#define DATA "Hello from server"
|
||||||
|
#define BUF 128
|
||||||
|
#define FINS "exit_s"
|
||||||
|
#define FINC "exit_c"
|
||||||
|
|
||||||
|
void child_handler(int sig) {
|
||||||
|
pid_t pid;
|
||||||
|
while((pid = waitpid(-1, NULL, WNOHANG)) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void session(int client_sock, struct sockaddr_un server_sockaddr, struct sockaddr_un client_sockaddr){
|
||||||
|
int rc;
|
||||||
|
char inbuf[BUF];
|
||||||
|
char outbuf[BUF];
|
||||||
|
int bytes_rec = 0;
|
||||||
|
size_t bs = BUF;
|
||||||
|
socklen_t len;
|
||||||
|
pid_t ppid, pid;
|
||||||
|
|
||||||
|
ppid=getppid();
|
||||||
|
pid=getpid();
|
||||||
|
|
||||||
|
len = sizeof(client_sockaddr);
|
||||||
|
rc = getpeername(client_sock, (struct sockaddr *) &client_sockaddr, &len);
|
||||||
|
if (rc == -1){
|
||||||
|
close(client_sock);
|
||||||
|
errExit("getpeer");
|
||||||
|
} else {
|
||||||
|
printf("[%d] Client socket filepath: %s\n", pid, client_sockaddr.sun_path);
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
printf("waiting for %s...\n",client_sockaddr.sun_path);
|
||||||
|
memset(inbuf, 0, bs);
|
||||||
|
bytes_rec = recv(client_sock, inbuf, bs, 0);
|
||||||
|
if (bytes_rec == -1){
|
||||||
|
close(client_sock);
|
||||||
|
errExit("recv");
|
||||||
|
} else {
|
||||||
|
printf("[%s]: %s\n", client_sockaddr.sun_path, inbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(outbuf, 0, bs);
|
||||||
|
strcpy(outbuf, DATA);
|
||||||
|
printf("Sending data...\n");
|
||||||
|
rc = send(client_sock, outbuf, strlen(outbuf)+1, 0);
|
||||||
|
if (rc == -1) {
|
||||||
|
close(client_sock);
|
||||||
|
errExit("send");
|
||||||
|
} else {
|
||||||
|
printf("Data sent!\n");
|
||||||
|
}
|
||||||
|
if (strcmp(inbuf,FINS) == 0 ) {kill(ppid,SIGTERM);break;}
|
||||||
|
} while (strcmp(inbuf,FINC));
|
||||||
|
memset(outbuf, 0, bs);
|
||||||
|
sprintf(outbuf,"Exit %d\n",pid);
|
||||||
|
rc = send(client_sock, outbuf, strlen(outbuf)+1, 0);
|
||||||
|
if (rc == -1) {
|
||||||
|
close(client_sock);
|
||||||
|
errExit("send");
|
||||||
|
}
|
||||||
|
close(client_sock);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
|
||||||
|
int server_sock, client_sock, rc;
|
||||||
|
socklen_t len;
|
||||||
|
struct sockaddr_un server_sockaddr;
|
||||||
|
struct sockaddr_un client_sockaddr;
|
||||||
|
int backlog = 10;
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
|
struct sigaction new_act, old_act;
|
||||||
|
sigemptyset (&new_act.sa_mask);
|
||||||
|
new_act.sa_flags = SA_RESTART;
|
||||||
|
new_act.sa_handler = child_handler;
|
||||||
|
sigaction (SIGCHLD, &new_act, &old_act);
|
||||||
|
|
||||||
|
len = sizeof(server_sockaddr);
|
||||||
|
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
|
||||||
|
|
||||||
|
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (server_sock == -1) errExit("socket");
|
||||||
|
|
||||||
|
server_sockaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(server_sockaddr.sun_path, SOCK_PATH);
|
||||||
|
|
||||||
|
unlink(SOCK_PATH);
|
||||||
|
rc = bind(server_sock, (struct sockaddr *) &server_sockaddr, len);
|
||||||
|
if (rc == -1){
|
||||||
|
close(server_sock);
|
||||||
|
errExit("bind");
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = listen(server_sock, backlog);
|
||||||
|
if (rc == -1){
|
||||||
|
close(server_sock);
|
||||||
|
errExit("listen");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("socket listening...\n");
|
||||||
|
|
||||||
|
do {
|
||||||
|
client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len);
|
||||||
|
if (client_sock == -1){
|
||||||
|
close(server_sock);
|
||||||
|
close(client_sock);
|
||||||
|
errExit("accept");
|
||||||
|
}
|
||||||
|
if ((pid = fork()) == -1 ) {
|
||||||
|
close(server_sock);
|
||||||
|
close(client_sock);
|
||||||
|
errExit("fork");
|
||||||
|
}
|
||||||
|
if (pid == 0) session(client_sock,server_sockaddr,client_sockaddr);
|
||||||
|
close(client_sock);
|
||||||
|
} while (1);
|
||||||
|
|
||||||
|
|
||||||
|
close(server_sock);
|
||||||
|
unlink(SOCK_PATH);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,86 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <time.h>
|
||||||
|
#define DATA 20
|
||||||
|
#define NUM 4
|
||||||
|
|
||||||
|
struct Data {
|
||||||
|
char s[DATA + 1];
|
||||||
|
pid_t cpid;
|
||||||
|
unsigned char i, t;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
int pipefd[4][2];
|
||||||
|
pid_t cpid[4];
|
||||||
|
unsigned char i, tt;
|
||||||
|
int rr, wt;
|
||||||
|
|
||||||
|
if (argc != 2){
|
||||||
|
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
for(i = 0; i < NUM; i++){
|
||||||
|
if (pipe(pipefd[i]) == -1){
|
||||||
|
perror("Pipe");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
tt = (unsigned char)rand()%8;
|
||||||
|
dprintf(STDOUT_FILENO, "Time[%X]\n", tt);
|
||||||
|
|
||||||
|
cpid[i] = fork();
|
||||||
|
if (cpid[i] == -1){
|
||||||
|
perror("Fork");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cpid[i] == 0){
|
||||||
|
struct Data data;
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
for(int j = 0; j < i; j++){
|
||||||
|
close(pipefd[j][0]);
|
||||||
|
close(pipefd[j][1]);
|
||||||
|
}
|
||||||
|
data.cpid = getpid();
|
||||||
|
data.i = i;
|
||||||
|
data.t = tt;
|
||||||
|
strncpy(data.s, argv[1], DATA);
|
||||||
|
write(pipefd[i][1], &data, sizeof(struct Data));
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
sleep(tt);
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
} else {
|
||||||
|
dprintf(STDOUT_FILENO, "Start [%d]\n", cpid[i]);
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Waiting...\n");
|
||||||
|
|
||||||
|
while((wt = waitpid(-1, NULL, WNOHANG)) > -1){
|
||||||
|
if (wt > 0) for(i = 0; i < NUM; i++){
|
||||||
|
if (cpid[i] == wt){
|
||||||
|
struct Data data;
|
||||||
|
while((rr = read(pipefd[i][0], &data, sizeof(struct Data))) > 0){
|
||||||
|
dprintf(STDOUT_FILENO, "[%d] num = %d, time = %d [%s]\n", data.cpid, data.i, data.t, data.s);
|
||||||
|
}
|
||||||
|
if (rr == 0) close(pipefd[i][0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
usleep(3000000);
|
||||||
|
}
|
||||||
|
if (errno == ECHILD)
|
||||||
|
perror("Finish:");
|
||||||
|
else
|
||||||
|
perror("Wait error:");
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
be be be be
|
||||||
|
aaaaaaaaaaa
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
int fd_src, fd_dst;
|
||||||
|
caddr_t addr_src, addr_dst;
|
||||||
|
struct stat filestat;
|
||||||
|
fd_src = open(argv[1], O_RDONLY);
|
||||||
|
fd_dst = open(argv[2], O_RDWR | O_CREAT);
|
||||||
|
fstat(fd_src, &filestat);
|
||||||
|
lseek(fd_dst, filestat.st_size - 1, SEEK_SET);
|
||||||
|
write(fd_dst, "", 1);
|
||||||
|
addr_src = mmap((caddr_t)0, filestat.st_size, PROT_READ, MAP_SHARED, fd_src, 0);
|
||||||
|
addr_dst = mmap((caddr_t)0, filestat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_dst, 0);
|
||||||
|
memcpy(addr_dst, addr_src, filestat.st_size);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <time.h>
|
||||||
|
#define NUM 4
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
int pipefd[4][2];
|
||||||
|
pid_t cpid[4];
|
||||||
|
unsigned char i, tt;
|
||||||
|
char buf;
|
||||||
|
int rr, wt;
|
||||||
|
|
||||||
|
if (argc != 2){
|
||||||
|
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
for(i = 0; i < NUM; i++){
|
||||||
|
if (pipe(pipefd[i]) == -1){
|
||||||
|
perror("Pipe");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
tt = (unsigned char)rand()%8;
|
||||||
|
dprintf(STDOUT_FILENO, "Time[%X]\n", tt);
|
||||||
|
|
||||||
|
cpid[i] = fork();
|
||||||
|
if (cpid[i] == -1){
|
||||||
|
perror("Fork");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cpid[i] == 0){
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
write(pipefd[i][1], argv[1], strlen(argv[1]));
|
||||||
|
write(pipefd[i][1], &i, 1);
|
||||||
|
write(pipefd[i][1], &tt, 1);
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
sleep(tt);
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
} else {
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Waiting...\n");
|
||||||
|
|
||||||
|
while((wt = waitpid(-1, NULL, WNOHANG)) > -1){
|
||||||
|
if (wt > 0) for(i = 0; i < NUM; i++){
|
||||||
|
if (cpid[i] == wt){
|
||||||
|
while((rr = read(pipefd[i][0], &buf, 1)) > 0){
|
||||||
|
write(STDOUT_FILENO, &buf, 1);
|
||||||
|
dprintf(STDOUT_FILENO, "[%X]", buf);
|
||||||
|
}
|
||||||
|
if (rr == 0){
|
||||||
|
write(STDOUT_FILENO, "\n", 1);
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (errno == ECHILD)
|
||||||
|
perror("Finish:");
|
||||||
|
else
|
||||||
|
perror("Wait error:");
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#define NUM 4
|
||||||
|
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||||
|
#define errExit1(msg) { perror(msg); _exit(EXIT_FAILURE); }
|
||||||
|
|
||||||
|
void child_handler(int sig) {
|
||||||
|
pid_t pid;
|
||||||
|
while((pid = waitpid(-1, NULL, WNOHANG)) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
struct sigaction new_act, old_act;
|
||||||
|
sigemptyset (&new_act.sa_mask);
|
||||||
|
new_act.sa_flags = 0;
|
||||||
|
new_act.sa_handler = child_handler;
|
||||||
|
|
||||||
|
sigaction (SIGCHLD, &new_act, &old_act);
|
||||||
|
|
||||||
|
int pipefd[NUM][2];
|
||||||
|
pid_t cpid[NUM];
|
||||||
|
|
||||||
|
int nfds, num_open_fds;
|
||||||
|
struct pollfd *pfds;
|
||||||
|
int ready;
|
||||||
|
|
||||||
|
unsigned char i,tt;
|
||||||
|
char buf;
|
||||||
|
int rr;
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
num_open_fds = nfds = NUM;
|
||||||
|
pfds = calloc(nfds, sizeof(struct pollfd));
|
||||||
|
if (pfds == NULL) errExit("malloc");
|
||||||
|
|
||||||
|
for (i=0;i<NUM;i++){
|
||||||
|
tt=(unsigned char)rand()%9;
|
||||||
|
dprintf(STDOUT_FILENO,"child %d life time is %X\n",i,tt);
|
||||||
|
|
||||||
|
if (pipe(pipefd[i]) == -1) errExit("pipe");
|
||||||
|
pfds[i].fd = pipefd[i][0];
|
||||||
|
pfds[i].events = POLLIN;
|
||||||
|
if ((cpid[i] = fork()) == -1) errExit("fork");
|
||||||
|
|
||||||
|
if (cpid[i] == 0) {
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
for (int j=0; j<i; j++){
|
||||||
|
close(pipefd[j][0]);
|
||||||
|
close(pipefd[j][1]);
|
||||||
|
}
|
||||||
|
write(pipefd[i][1], argv[1], strlen(argv[1]));
|
||||||
|
write(pipefd[i][1], &i, 1);
|
||||||
|
write(pipefd[i][1], &tt, 1);
|
||||||
|
sleep(tt);
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
_exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j=0; j<i; j++) close(pipefd[j][1]);
|
||||||
|
|
||||||
|
dprintf(STDOUT_FILENO,"Opened %d pipes, waiting...\n",num_open_fds);
|
||||||
|
|
||||||
|
while (num_open_fds > 0) {
|
||||||
|
|
||||||
|
do{
|
||||||
|
dprintf(STDOUT_FILENO,"\ntry poll:\n");
|
||||||
|
for (i = 0; i < NUM; i++) pfds[i].revents=0;
|
||||||
|
ready = poll(pfds, nfds, -1);
|
||||||
|
if(ready == -1)
|
||||||
|
switch (errno) {
|
||||||
|
case EINTR: dprintf(STDOUT_FILENO,"\nPoll interrupted, repeat.\n");
|
||||||
|
continue;
|
||||||
|
default: errExit("poll");
|
||||||
|
}
|
||||||
|
} while (ready <= 0);
|
||||||
|
|
||||||
|
for (i = 0; i < NUM; i++) {
|
||||||
|
if (pfds[i].revents != 0) {
|
||||||
|
dprintf(STDOUT_FILENO," fd=%d; events: %7s%8s%9s%8s%8s ", pfds[i].fd,
|
||||||
|
(pfds[i].revents & POLLIN) ? "POLLIN " : "",
|
||||||
|
(pfds[i].revents & POLLHUP) ? "POLLHUP " : "",
|
||||||
|
(pfds[i].revents & POLLNVAL) ? "POLLNVAL " : "",
|
||||||
|
(pfds[i].revents & POLLPRI) ? "POLLPRI " : "",
|
||||||
|
(pfds[i].revents & POLLERR) ? "POLLERR " : "");
|
||||||
|
if (pfds[i].revents & POLLIN) {
|
||||||
|
|
||||||
|
do {
|
||||||
|
rr=read(pfds[i].fd, &buf, 1);
|
||||||
|
if (rr < 0)
|
||||||
|
switch (errno) {
|
||||||
|
case EINTR: dprintf(STDOUT_FILENO,"\nRead interrupted, repeat.\n");
|
||||||
|
continue;
|
||||||
|
default: errExit("read");
|
||||||
|
}
|
||||||
|
if(rr == 0 )
|
||||||
|
dprintf(STDOUT_FILENO,"\n");
|
||||||
|
else
|
||||||
|
dprintf(STDOUT_FILENO,"[%2X]",buf);
|
||||||
|
} while (rr > 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if ((pfds[i].revents & (POLLHUP | ~POLLIN))) {
|
||||||
|
dprintf(STDOUT_FILENO,"Closing [%d], pipefd %d, pfds.fd %d\n",i,pipefd[i][0],pfds[i].fd);
|
||||||
|
if (close(pfds[i].fd) < 0 ) errExit("close pfds");
|
||||||
|
pfds[i].fd = -1;
|
||||||
|
pfds[i].events=0;
|
||||||
|
num_open_fds--;
|
||||||
|
dprintf(STDOUT_FILENO,"Still opened fds %d\n",num_open_fds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <poll.h>
|
||||||
|
|
||||||
|
#define NUM 4
|
||||||
|
#define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0)
|
||||||
|
#define errExit1(msg) { perror(msg); _exit(EXIT_FAILURE); }
|
||||||
|
|
||||||
|
struct Child {
|
||||||
|
unsigned char lifetime;
|
||||||
|
char* message;
|
||||||
|
};
|
||||||
|
|
||||||
|
void child_handler(int sig) {
|
||||||
|
pid_t pid;
|
||||||
|
while((pid = waitpid(-1, NULL, WNOHANG)) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
struct sigaction new_act, old_act;
|
||||||
|
sigemptyset (&new_act.sa_mask);
|
||||||
|
new_act.sa_flags = 0;
|
||||||
|
new_act.sa_handler = child_handler;
|
||||||
|
|
||||||
|
sigaction (SIGCHLD, &new_act, &old_act);
|
||||||
|
|
||||||
|
int pipefd[NUM][2];
|
||||||
|
pid_t cpid[NUM];
|
||||||
|
|
||||||
|
int nfds, num_open_fds;
|
||||||
|
struct pollfd *pfds;
|
||||||
|
int ready;
|
||||||
|
|
||||||
|
unsigned char i,tt;
|
||||||
|
char buf;
|
||||||
|
int rr;
|
||||||
|
|
||||||
|
struct Child children[NUM];
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
num_open_fds = nfds = NUM;
|
||||||
|
pfds = calloc(nfds, sizeof(struct pollfd));
|
||||||
|
if (pfds == NULL) errExit("malloc");
|
||||||
|
|
||||||
|
for (i=0;i<NUM;i++){
|
||||||
|
tt=(unsigned char)rand()%9;
|
||||||
|
dprintf(STDOUT_FILENO,"child %d life time is %X\n",i,tt);
|
||||||
|
|
||||||
|
children[i].lifetime = tt;
|
||||||
|
children[i].message = argv[1];
|
||||||
|
|
||||||
|
if (pipe(pipefd[i]) == -1) errExit("pipe");
|
||||||
|
pfds[i].fd = pipefd[i][0];
|
||||||
|
pfds[i].events = POLLIN;
|
||||||
|
if ((cpid[i] = fork()) == -1) errExit("fork");
|
||||||
|
|
||||||
|
if (cpid[i] == 0) {
|
||||||
|
close(pipefd[i][0]);
|
||||||
|
for (int j=0; j<i; j++){
|
||||||
|
close(pipefd[j][0]);
|
||||||
|
close(pipefd[j][1]);
|
||||||
|
}
|
||||||
|
write(pipefd[i][1], children + i, sizeof(struct Child));
|
||||||
|
sleep(tt);
|
||||||
|
close(pipefd[i][1]);
|
||||||
|
_exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j=0; j<i; j++) close(pipefd[j][1]);
|
||||||
|
|
||||||
|
dprintf(STDOUT_FILENO,"Opened %d pipes, waiting...\n",num_open_fds);
|
||||||
|
|
||||||
|
while (num_open_fds > 0) {
|
||||||
|
|
||||||
|
do{
|
||||||
|
dprintf(STDOUT_FILENO,"\ntry poll:\n");
|
||||||
|
for (i = 0; i < NUM; i++) pfds[i].revents=0;
|
||||||
|
ready = poll(pfds, nfds, -1);
|
||||||
|
if(ready == -1)
|
||||||
|
switch (errno) {
|
||||||
|
case EINTR: dprintf(STDOUT_FILENO,"\nPoll interrupted, repeat.\n");
|
||||||
|
continue;
|
||||||
|
default: errExit("poll");
|
||||||
|
}
|
||||||
|
} while (ready <= 0);
|
||||||
|
|
||||||
|
for (i = 0; i < NUM; i++) {
|
||||||
|
if (pfds[i].revents != 0) {
|
||||||
|
dprintf(STDOUT_FILENO," fd=%d; events: %7s%8s%9s%8s%8s ", pfds[i].fd,
|
||||||
|
(pfds[i].revents & POLLIN) ? "POLLIN " : "",
|
||||||
|
(pfds[i].revents & POLLHUP) ? "POLLHUP " : "",
|
||||||
|
(pfds[i].revents & POLLNVAL) ? "POLLNVAL " : "",
|
||||||
|
(pfds[i].revents & POLLPRI) ? "POLLPRI " : "",
|
||||||
|
(pfds[i].revents & POLLERR) ? "POLLERR " : "");
|
||||||
|
if (pfds[i].revents & POLLIN) {
|
||||||
|
|
||||||
|
do {
|
||||||
|
rr=read(pfds[i].fd, &buf, 1);
|
||||||
|
if (rr < 0)
|
||||||
|
switch (errno) {
|
||||||
|
case EINTR: dprintf(STDOUT_FILENO,"\nRead interrupted, repeat.\n");
|
||||||
|
continue;
|
||||||
|
default: errExit("read");
|
||||||
|
}
|
||||||
|
if(rr == 0 )
|
||||||
|
dprintf(STDOUT_FILENO,"\n");
|
||||||
|
else
|
||||||
|
dprintf(STDOUT_FILENO,"[%2X]",buf);
|
||||||
|
} while (rr > 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if ((pfds[i].revents & (POLLHUP | ~POLLIN))) {
|
||||||
|
dprintf(STDOUT_FILENO,"Closing [%d], pipefd %d, pfds.fd %d\n",i,pipefd[i][0],pfds[i].fd);
|
||||||
|
if (close(pfds[i].fd) < 0 ) errExit("close pfds");
|
||||||
|
pfds[i].fd = -1;
|
||||||
|
pfds[i].events=0;
|
||||||
|
num_open_fds--;
|
||||||
|
dprintf(STDOUT_FILENO,"Still opened fds %d\n",num_open_fds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <error.h>
|
||||||
|
|
||||||
|
|
||||||
|
int mtx(int *m, int s){
|
||||||
|
srand(time(NULL));
|
||||||
|
for(int i = 0; i < s; i++)
|
||||||
|
*m++ = rand()%0xffff;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
int fd;
|
||||||
|
int n,s;
|
||||||
|
size_t ms;
|
||||||
|
int *m;
|
||||||
|
int *fa;
|
||||||
|
|
||||||
|
if (argc < 3){
|
||||||
|
printf("Usage: a.out size filename\n");
|
||||||
|
}
|
||||||
|
s = n*n;
|
||||||
|
ms = s*sizeof(int);
|
||||||
|
|
||||||
|
if((fd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0664)) == -1){
|
||||||
|
perror("open: ");
|
||||||
|
exit(errno);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
be be be be
|
||||||
|
aaaaaaaaaaa
|
||||||
Reference in New Issue
Block a user