Question

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 )

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

Code:

#include<stdio.h>
#include <unistd.h>
#include<malloc.h>
typedef struct pro
{
char *mes;
}proone;
int main()
{
char *message[3];
int fade[2];
proone one; //creating struct instance
proone two;
proone three;
one.mes="Welcome";
message[0]=one.mes;
two.mes="to";
message[1]=two.mes;
three.mes="Chegg";
message[2]=three.mes;
fork(); //for a process
pipe(fade); // creating a pipe with reading and writing end
int i;
if(fork()!=0)
{
close(fade[0]); //close reading end before writing
for(i=0;i<3;i++)
{
write(fade[1],&message[i],sizeof(message[i]));
printf("parent(%d) sends %s\n",getpid(),message[i]);
}
close(fade[1]); //close writing end of the pipe
}
else
{
close(fade[1]); //close writing end of the pipe
for(i=0;i<3;i++)
{
read(fade[0],&message[i],sizeof(message[i])); // read by reading end of the pipe
printf("child(%d) receives %s\n",getpid(),message[i]);
}
close(fade[0]); //close reading end of the pipe
}
return 0;
}

Output:

parent(18) sends Welcome
parent(18) sends to
parent(18) sends Chegg
child(20) receives Welcome
child(20) receives to
child(20) receives Chegg
parent(19) sends Welcome
parent(19) sends to
parent(19) sends Chegg
child(21) receives Welcome
child(21) receives to
child(21) receives Chegg

Add a comment
Know the answer?
Add Answer to:
How to Write a C program (for Linux/Unix) that: –Creates a struct, where each instance can...
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
  • You are required to write a C program on Unix/Linux in which the parent process creates...

    You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...

  • Using either a UNIX or a Linux system, write a C program that forks a child...

    Using either a UNIX or a Linux system, write a C program that forks a child process that ultimately becomes a zombie process. Process states can be obtained from the command: ps -l The process states are shown below the S column; processes with a state of Z are zombies. The process identifier (pid) of the child process is listed in the PID column, and that of the parent is listed in the PPID column. Because you do not want...

  • *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...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • 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...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

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