mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
restructure
This commit is contained in:
@@ -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.
Executable
BIN
Binary file not shown.
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