Created
May 10, 2017 21:45
-
-
Save morsoinferno/83e4e67a518cd5bfa0d91c146acdc902 to your computer and use it in GitHub Desktop.
anillo.c
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
// lista circular - procesos y pipes | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#define MAX_P 2 | |
#define MAX_BUFFER 100 | |
void main (int argc, char ** argv) | |
{ | |
pid_t pid, pid_in, pid_out; | |
int index; | |
int fd [MAX_P][2]; | |
int *fd_in, *fd_out; | |
char *msg; | |
int msg_len; | |
char buffer[100]; | |
pid_in = getpid (); | |
for (index = 0; index < MAX_P; index++) | |
pipe (fd[index]); | |
for (index = 1; index < MAX_P; index++) | |
{ | |
pid = fork (); | |
if (pid != 0) | |
{ | |
close (fd [index-1][0]); | |
fd_out = &fd [index-1][1]; | |
} | |
else | |
{ | |
close (fd [index-1][1]); | |
fd_in = &fd [index-1][0]; | |
} | |
if (pid != 0) break; | |
} | |
pid = getpid (); | |
if (index == MAX_P) pid_out = getpid (); | |
//printf("%d\n", getpid ()); | |
//conectamos el ultimo pipe | |
if (pid == pid_in) | |
{ | |
close (fd [MAX_P-1][1]); | |
fd_in = &fd [MAX_P-1][0]; | |
} | |
if (pid == pid_out) | |
{ | |
close (fd [MAX_P-1][0]); | |
fd_out = &fd [MAX_P-1][1]; | |
} | |
if (pid == pid_in) | |
{ | |
msg = "raiz "; | |
write (*fd_out, msg, strlen (msg)+1); | |
read (*fd_in, buffer, sizeof (buffer)); | |
printf("raiz imprime: %s\n", buffer); | |
} | |
else if (pid == pid_out) | |
{ | |
read (*fd_in, buffer, sizeof (buffer)); | |
strcat (buffer, "hoja"); | |
write (*fd_out, buffer, strlen(buffer)+1); | |
} | |
else | |
{ | |
read (*fd_in, buffer, sizeof (buffer)); | |
strcat (buffer, "nodo "); | |
write (*fd_out, buffer, strlen (buffer)+1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment