1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Furkat [3]
3 years ago
9

Create a program which will input data into a pipe one character at a time. Count the number of characters as they are written i

nto 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]

Computers and Technology
1 answer:
LenKa [72]3 years ago
4 0

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\n", msg);

   sprintf(msg , "boy");

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

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

   sprintf(msg , "cat");

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

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

   sprintf(msg , "dog");

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

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

   //read

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

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

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

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

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

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

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

   printf("Read: %s\n", 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\n", buf);

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

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

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

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

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

       printf("This is child process reading fourth message. Content is %s\n", 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\n");

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

   sprintf(msg , "boy");

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

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

   sprintf(msg , "cat");

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

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

   sprintf(msg , "dog");

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

   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\n", 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\n", 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;

}

Explanation:

Output for a, b, c are pasted accordingly

You might be interested in
Crashing almost always accelerates the schedule while increasing project risk, whereas fast tracking almost always accelerates t
Ivahew [28]

Answer: False

Explanation: Because fast-tracking doesn’t involve additional resources, it’s usually best to start with this technique while Crashing requires careful planning to prevent “panic mode” among the project crews and to avoid acceleration claims.

4 0
3 years ago
What are the Positive and negative sites of the internet
Lynna [10]

Answer:

A positive of the sites of internet is that it can be very helpful to you like brainly. It can also educate you and help when you cant do you something like we have online classes since we can't go to school because of Corona

A negative of the sites of the internet is that sometimes people will try to trick you to do irresponsible things. Some will trick you for your address, credit card number, or using things unwisely. Because people don't know who is behind the screen they might be a criminal but they also don't know who you are until they get your address or information.

Explanation:

8 0
2 years ago
Your company has a team of remote workers that need to use Windows-based software to develop company applications, but your team
timofeeve [1]

Operating systems are the software that supports the basic functioning of the computer. Windows Virtual Desktop is an Azure compute service that will help solve the problem.

<h3>What is Windows Virtual Desktop?</h3>

Windows Virtual Desktop is an app virtualization service of Azure that operates on the cloud platform. It includes standard operating procedures that can be used by users with multiple OS.

Azure's Virtual Desktop supports different versions of window OS. This type of system is majorly used in remote work demands and specialized workloads.

Therefore, Windows Virtual Desktop is an Azure compute service.

Learn more about Azures service here:

brainly.com/question/13144160

#SPJ1

5 0
1 year ago
Question :
BlackZzzverrR [31]

Answer:

D

Explanation:

Search engine optimization (SEO)

4 0
3 years ago
Kim agrees. You get the equipment from the truck and begin working. Kim approaches you as you are installing the modem and is a
Angelina_Jolie [31]
Ask your friends if they are doing something wrong with your friends and friends
6 0
1 year ago
Other questions:
  • If reading or writing a cache line of size 64 bytes to Flash requires 2.56 μJ and DRAM requires 0.5 nJ, and if idle power consum
    9·1 answer
  • Which is a function of network media?
    11·2 answers
  • A ________ is a user-interface component with two states: checked and unchecked. radio button check box menu item submenu item
    5·1 answer
  • You are manually configuring a tcp/ip host. another administrator gives you the router's ip address. what is the tcp/ip term for
    6·2 answers
  • What your computer can do to you?
    14·2 answers
  • What does aperture control? A)amount of light the image sensor captures when taking a photo. B)how sensitive the camera is to in
    10·2 answers
  • Two categories of payroll deductions are required deductions and ___ deductions.
    9·2 answers
  • What sorts of changes have you been observing in your society in your society in comparison in last 3 years​
    13·1 answer
  • You and a friend have just started a small business. How could you use the Internet to make your business successful?
    15·2 answers
  • Which item can be added to Outlook messages, like a Word document or PowerPoint presentation, that takes standard bulleted lists
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!