rendered paste body-- pipe.c --
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
int
main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if ((childpid = fork()) == -1) {
perror("fork");
exit(1);
}
if (childpid == 0) {
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
while (scanf("%s", readbuffer) != EOF)
write(fd[1], readbuffer, (strlen(readbuffer) + 1));
exit(0);
} else {
/* Parent process closes up output side of pipe */
close(fd[1]);
/* Read in a string from the pipe */
while (nbytes = read(fd[0], readbuffer, sizeof(readbuffer)))
printf("Received %d bytes: %s\n", nbytes, readbuffer);
}
return (0);
}
-- pthread.c --
-- compile with the -lpthread flag --
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
char buf[1024];
int p = 0;
void printer(){
while (1){
sleep(1);
printf("\nBuffer: %s\n", buf);
p = 0;
memset(buf, '\0', sizeof(buf));
}
}
int main(){
pthread_t prnt;
char x;
buf[0]='\0';
pthread_create(&prnt, NULL, (void *)(printer), NULL);
while ((x = getchar()) != EOF)
buf[p++] = x;
return 0;
}