feat: Initial commit
This commit is contained in:
56
Lab05/pipes.c
Normal file
56
Lab05/pipes.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#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);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user