Last active
March 16, 2017 08:16
-
-
Save akwodkiewicz/a5a93a42ae3aad1c308c300545286d94 to your computer and use it in GitHub Desktop.
Rozwiązanie zadania 1_2B z SOP2
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 <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <ctype.h> | |
#include <sys/wait.h> | |
#include <string.h> | |
#include <time.h> | |
#include <limits.h> | |
#define ERR(source) (perror(source),\ | |
fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\ | |
exit(EXIT_FAILURE)) | |
void usage(char*); | |
void create_children(int, int**, int); | |
void child_work(int**, int, int); | |
void parent_work(int**, int, int); | |
int main(int argc, char** argv) | |
{ | |
if(argc!=3) | |
usage(argv[0]); | |
int n = atoi(argv[1]); | |
int k = atoi(argv[2]); | |
int** pipes; | |
if((pipes = (int**)malloc(sizeof(int*)*(n+1))) == NULL) | |
ERR("malloc pipes**\n"); | |
for(int i =0; i<n+1; i++) | |
{ | |
if((pipes[i] = (int*)malloc(sizeof(int)*2))==NULL) | |
ERR("malloc pipes*\n"); | |
pipe(pipes[i]); | |
} | |
create_children(n, pipes, k); | |
return EXIT_SUCCESS; | |
} | |
void create_children(int n, int** pipes, int k) | |
{ | |
pid_t pid; | |
printf("[%d](PARENT) - Process created\n", getpid()); | |
for(int i=1; i<n+1; i++) | |
{ | |
if((pid = fork())<(pid_t)0) ERR("fork failed\n"); | |
if(pid==0) //child | |
{ | |
printf("[%d] - Process created\n", getpid()); | |
for(int j=1; j<n+1; j++) | |
{ | |
if(j!=i) | |
{ | |
//printf("[%d] - closing read_fd nr: [%d]\n", getpid(), j); | |
close(pipes[j][0]); | |
} | |
} | |
//printf("[%d] - closing write_fd nr: [%d]\n", getpid(), i); | |
close(pipes[i][1]); | |
child_work(pipes, i, n); | |
break; | |
} | |
else //parent | |
{ | |
//printf("[%d](PARENT) - closing read_fd nr: [%d]\n", getpid(), i); | |
close(pipes[i][0]); | |
} | |
} | |
if(pid!=0)//PARENT | |
{ | |
//printf("[%d](PARENT) - closing write_fd nr: [%d]\n", getpid(), 0); | |
close(pipes[0][1]); | |
parent_work(pipes, n, k); | |
} | |
} | |
void child_work(int **pipes, int id, int n) | |
{ | |
char message[PIPE_BUF], temp[3]; | |
memset(message, 0, sizeof(message)); | |
srand(getpid()); | |
int rand_id; | |
while(1) | |
{ | |
read(pipes[id][0], message, PIPE_BUF); | |
if(0==strcmp(message, "[-]")) | |
break; | |
do | |
{ | |
rand_id = rand()%(n+1); | |
}while(rand_id==id); | |
sprintf(temp, "%d ", id); | |
strcat(message, temp); | |
write(pipes[rand_id][1], message, PIPE_BUF); | |
} | |
printf("[%d] - Closing all left pipes\n", getpid()); | |
for(int i=0; i<n+1; i++) | |
{ | |
if(i!=id) | |
close(pipes[i][1]); | |
} | |
close(pipes[id][0]); | |
printf("[%d] - Stopping process\n", getpid()); | |
} | |
void parent_work(int** pipes, int n, int k) | |
{ | |
int rand_id; | |
char message[PIPE_BUF]; | |
for(int i=1; i<k+1; i++) | |
{ | |
memset(message, 0, sizeof(message)); | |
sprintf(message, "[%d] 0 ", i); | |
rand_id = (rand()%n)+1; | |
write(pipes[rand_id][1], message, PIPE_BUF); | |
read(pipes[0][0], message, PIPE_BUF); | |
printf("Message: %s\n", message); | |
} | |
memset(message, 0, sizeof(message)); | |
strcat(message, "[-]"); | |
for(int i =1; i<n+1; i++) | |
write(pipes[i][1], message, PIPE_BUF); | |
while(wait(NULL)>0); | |
printf("[%d](PARENT) - Closing all left pipes\n", getpid()); | |
for(int i=1; i<n+1; i++) | |
close(pipes[i][1]); | |
close(pipes[0][0]); | |
printf("[%d](PARENT) - Stopping process\n", getpid()); | |
} | |
void usage(char *name) | |
{ | |
fprintf(stderr,"USAGE: n - number of processes, k - number of messages\n"); | |
exit(EXIT_FAILURE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment