1
0
Files
Laboratori-OS/Lab05/pipes.c
2024-03-22 16:54:05 +01:00

57 lines
863 B
C

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/wait.h>
void receiver(int fd);
void sender(int fd);
int main() {
int fds[2];
pipe(fds);
if (fork()) {
receiver(fds[0]);
} else if (fork()) {
sender(fds[1]);
}
wait(NULL);
}
void sender(int fd) {
char * str;
int len;
int q = 0;
while (q != EOF) {
q = scanf(" %ms", &str);
len = strlen(str);
printf("%d\n",len);
if (len == 1) break;
write(fd, &len, sizeof(int));
write(fd, str, len);
free(str);
}
close(fd);
}
void receiver(int fd) {
char * str;
int len, q = 0;
while (1) {
q = read(fd, &len, sizeof(int));
if (q == EOF) break;
str = malloc(len * sizeof(char));
q = read(fd, str, len);
if (q == EOF) break;
for (char * c = str; *c != '\0'; c++)
*c = toupper(*c);
puts(str);
free(str);
}
}