162k views
3 votes
Create a program which will input data into a pipe one character at a time. Count the number of characters as they are written into the pipe. Print out a message after every 1K (1024) characters are written to the pipe. If the process does not read from the pipe, eventually the pipe will fill and the next attempted write to the pipe will block the process. Use the alarm function to prevent the process from hanging indefinitely after the pipe gets full, but be sure to set the time limit large enough so that you can be sure the pipe is completely filled. In the interrupt handler function, print out a message showing the final count of characters that can be input to the pipe (i.e., the maximum size of pipes on your system). [15 pts]

1 Answer

1 vote

Answer:

a)

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<unistd.h>

#define BUFSIZE 16

int main(){

//pipe descriptors

char msg[BUFSIZE];

char buf[BUFSIZE];

int pipefd[2];

if(pipe(pipefd) == -1){

//pipe creation error

perror("pipe");

exit(EXIT_FAILURE);

}

//pipe creation successfull

//write four messages

sprintf(msg , "apple");

write(pipefd[1], msg, BUFSIZE);

printf("Written %s\\", msg);

sprintf(msg , "boy");

write(pipefd[1], msg, BUFSIZE);

printf("Written %s\\", msg);

sprintf(msg , "cat");

write(pipefd[1], msg, BUFSIZE);

printf("Written %s\\", msg);

sprintf(msg , "dog");

write(pipefd[1], msg, BUFSIZE);

printf("Written %s\\", msg);

//read

read(pipefd[0], buf, BUFSIZE);

printf("Read: %s\\", buf);

read(pipefd[0], buf, BUFSIZE);

printf("Read: %s\\", buf);

read(pipefd[0], buf, BUFSIZE);

printf("Read: %s\\", buf);

read(pipefd[0], buf, BUFSIZE);

printf("Read: %s\\", buf);

close(pipefd[0]);

close(pipefd[1]);

return 0;

}

(b)

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<unistd.h>

#include<wait.h>

#define BUFSIZE 16

int main(){

//pipe descriptors

char msg[BUFSIZE];

char buf[BUFSIZE];

int pipefd[2];

if(pipe(pipefd) == -1){

//pipe creation error

perror("pipe");

exit(EXIT_FAILURE);

}

//pipe creation successfull

//write four messages

if(fork() == 0){

//this is child

//close unused write end

close(pipefd[1]);

read(pipefd[0], buf, BUFSIZE);

printf("This is child process reading first message. Content is %s\\", buf);

read(pipefd[0], buf, BUFSIZE);

printf("This is child process reading second message. Content is %s\\", buf);

read(pipefd[0], buf, BUFSIZE);

printf("This is child process reading third message. Content is %s\\", buf);

read(pipefd[0], buf, BUFSIZE);

printf("This is child process reading fourth message. Content is %s\\", buf);

close(pipefd[0]);

//exit from child

exit(EXIT_SUCCESS);

}

//this is parent process

//close unused read end

close(pipefd[0]);

sprintf(msg , "apple");

printf("This is parent process. Writing first message into pipe\\");

write(pipefd[1], msg, BUFSIZE);

sprintf(msg , "boy");

printf("This is parent process. Writing second message into pipe\\");

write(pipefd[1], msg, BUFSIZE);

sprintf(msg , "cat");

printf("This is parent process. Writing third message into pipe\\");

write(pipefd[1], msg, BUFSIZE);

sprintf(msg , "dog");

printf("This is parent process. Writing fourth message into pipe\\");

write(pipefd[1], msg, BUFSIZE);

close(pipefd[1]);

//wait for child

wait(NULL);

return 0;

}

(c)

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<unistd.h>

#include<sys/wait.h>

#include<sys/types.h>

#include<signal.h>

typedef struct sigaction Sigaction;

unsigned long long size = 0;

void alarmhandler(int sig){

//alarm fired writing blocked

printf("Write blocked after %llu characters\\", size);

exit(EXIT_SUCCESS);

}

int main(){

//pipe descriptors

int pipefd[2];

if(pipe(pipefd) == -1){

//pipe creation error

perror("pipe");

exit(EXIT_FAILURE);

}

//install handler

sigset_t mask , prev;

sigemptyset(&mask);

sigaddset(&mask , SIGALRM);

sigprocmask(SIG_BLOCK , &mask , &prev);

Sigaction new_action;

sigemptyset(&new_action.sa_mask);

new_action.sa_flags = SA_RESTART;

new_action.sa_handler = alarmhandler;

sigaction(SIGALRM , &new_action , NULL);

sigprocmask(SIG_SETMASK , &prev, NULL);

while(1){

//print size on multiple of 4

if(size != 0 && size % 1024 == 0){

printf("%llu characters in pipe\\", size);

}

//reset previous alarm

alarm(0);

//set new alarm for 5 seconds

alarm(5);

//write to pipe one character

write(pipefd[1], "A", sizeof(char));

size++;

}

return 0;

}

Step-by-step explanation:

Output for a, b, c are pasted accordingly

Create a program which will input data into a pipe one character at a time. Count-example-1
Create a program which will input data into a pipe one character at a time. Count-example-2
Create a program which will input data into a pipe one character at a time. Count-example-3
User Sanju D
by
4.9k points