Question

10) Unlike a signal, which conveys only the occurrence of a particular event and contains no...

10) Unlike a signal, which conveys only the occurrence of a particular event and contains no information content, a pipe can be thought of as a scratch file created by a system call. It can be used as a communications channel between concurrently running processes. The interface call to a pipe is similar to that for any file. In fact, the process reads and writes to a pipe just like any file. Unlike files, however, pipes do not represent actual devices or areas on disk. Instead, they are transient areas in memory. Just as a pipe is used by the shell to pass information on the command line, a program pipe is used to pass information from one process to another. A pipe's main advantage is that it can provide much higher bandwidths than if the processes read and write from a physically moving media (as would be the case with ordinary disk files).

Pipes have a finite capacity and relay their information on a first-in, first-out (FIFO) basis. The system blocks a process until the read/write can be satisfied. That is, if the reader gets ahead of the writer, the reader just waits for more data. If the writer gets too far ahead of the reader and manages to completely fill the pipe (the size of which is system dependent), it sleeps until the reader has a chance to catch up. Also, once information is read from the pipe, it is erased, thereby freeing up pipe space.

It should be noted that pipes do have some disadvantages. One of the more notable ones is the fact that the processes communicating over the pipe must be related, typically as a parent and child, or as two siblings. The reason for this is because one process has to tell the other what the file (pipe) descriptor is; however, the descriptor

is only valid in the context of a particular execution environment. Therefore, two unrelated processes generally cannot share a pipe. This can be a constraint for many applications where the potential reader and writer of the pipe originate from different, unrelated processes. On the other hand, if a pipe is established in a process before creating (forking) another process, the new process will inherit the pipe file descriptor thereby making it valid in both execution environments.

A sample initialization sequence for a pipe that will enable messages of 15 characters to be exchanged between processes is of the form:

#include <stdio.h>
#define MSGSIZE 16 /* This figure should include room for a terminating null */

int pfd[2], retval;
char msg [MSGSIZE], buffer[MSGSIZE];

retval = pipe(pfd);
write (pfd[1], msg, MSGSIZE); read (pfd[0], buffer, MSGSIZE);

The call creates a pipe represented by two file descriptors that are returned in the pfd array. Writing to pfd[1] puts data in the pipe; reading from pfd[0] gets data out.

a) Write a single program which will create a pipe, write at least four messages down it, and then read them back. Although this particular exercise does not demonstrate IPC (since there is only one process), it demonstrates how pipes can be used as a scratchpad FIFO buffer within a process. [9 pts]

b) Write a program which will spawn a child process and establish a one-way communication pipe from the parent to the child. The parent process should write at least four messages down the pipe. The child process should read all the messages from the pipe. Insert process ID statements so that the progress of the two processes can be traced. [9 pts] For example:

"This is the parent process. Writing first message into pipe."
"This is the child process. Reading first message from pipe. Contents is:"

c) Using the timer capability of one of the previous problems, design a program which will determine the size limit of a pipe on the system you are using. Only a certain number of bytes can be put in the pipe without it being read. Knowing this limit is important. Normally, if there is room in the pipe, the write function will return immediately. However, if a write is attempted that would overfill the pipe, process execution is suspended until room is made by another process which can read from the pipe.

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]

So, the printout will appear something like: 1024 characters in pipe 2048 characters in pipe xxxx characters in pipe

Write blocked after yyyy characters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answers to all parts are provided below. If you have any queries, feel free to comment.

a)

Program Screenshot:

Sample Output:

Program Code to copy:

#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)

Program Screenshot:

Sample Output:

Program code to copy:

#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)

Program Screenshot:


Sample Output:

Program code to copy:

#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;
  
}

Add a comment
Know the answer?
Add Answer to:
10) Unlike a signal, which conveys only the occurrence of a particular event and contains no...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Allow the main process to generate a text file containing the text of this assignment. The main p...

    Allow the main process to generate a text file containing the text of this assignment. The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”. The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe...

  • How to Write a C program (for Linux/Unix) that: –Creates a struct, where each instance can...

    How to Write a C program (for Linux/Unix) that: –Creates a struct, where each instance can store one message. –Forks a process. –Creates 2+ messages, with contents of your choice, in the parent and sends them to the child via a named or anonymous pipe. –Prints the contents of all received messages in the child process. –If necessary, cleans up after the pipe. Note (the subjects about signal and Pipes )

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • In modern operating systems, kernel processes messages are communicated securely by using several encryption mechanisms such...

    In modern operating systems, kernel processes messages are communicated securely by using several encryption mechanisms such as Zig-Zag cipher which divides the message into two parts: the first part contains the odd-positions characters, while the second part contains the even-positions characters. Eventually, the cipher is constructed by concatenating the first part with second part. Using traditional pipes, write a program to produce a full duplex parent/child relationship in which processes can send/receive user data encrypted with Zig-Zag cipher. The communication...

  • Modify the template program below and include the following additional functionalities: - Instead of 3 children...

    Modify the template program below and include the following additional functionalities: - Instead of 3 children processes, create 6 children processes. - 6 children processes are connected to the parent process via 6 pipes, one for each pair, child1-parent, child2-parent, child3-parent, .... - Child keeps writing to the pipe with 1 second apart between consecutive writes (as in child() in p. 167). - Keyboard inputs need to be monitored as in the original code. - Parent keeps reading from the...

  • Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array...

    Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client...

  • C homework help pipes

    Round 2: pipeline.cThis program takes the same input as sequence.c, but executes the commands as a pipeline, where the output of each command is piped to the input of the next command in line. The input of the first command, and the output of the final command, should not be altered. For example, if the file "cmdpipe" contains the linesls -s1sort -ntail -n 5then running./pipeline < cmdpipeshould output the 5 largest files in the current directory, in order of size.Suggested approach: set...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8,...

    ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8, Figure 3.30. DO NOT compile and ron these programs on Windows Write the 3.16, Figare 317 and Figure 3 programs in Notepadt+, for example, then compile and run them at the command pr apt. Provide screenshots of your programs compilation, execution, and the results. 144 6 7 8 9 ry-maps a shared-memory object of the ws writing to the object. The flag shared-memory object...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT