Question

In: Computer Science

C++ On linux Write a C++ program using the IPC. Declare two variables a=5 and b=6...

C++ On linux

Write a C++ program using the IPC. Declare two variables a=5 and b=6 in writer process. Now their product (a*b) will be communicated to the reader process along with your name. The reader process will now calculate the square root of the number and display it along with your name.

Solutions

Expert Solution

Named pipes (FIFO) can be used to implement this data transfer.Here are two files writer.cpp and reader.cpp

The files can be compiled using linux commands one by one in following order:

g++ writer.cpp -o write
g++ reader.cpp -o read
./read &
./write &

Writer process file:

/****  WRITER PROCESS FILE    *******/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<string>
int main()
{
int a = 5;
int b = 6;
int mul = a*b;

//its easier ti store data first in string so we have implemented string here
std::string s="";
//to_string(mul) converts mul into string
s+=std::to_string(mul) + " ";
//append s with your name
s+="your name";

//store data to be written into data character array
char data[13];
for(int i=0;i<13;i++)
        data[i]=s[i];

// create pipe file
int namedpipe = mkfifo("/tmp/fifo2", 0666); //read write

//open pipe file and store its fd
int fd = open("/tmp/fifo2",O_WRONLY);

//write into file
write(fd,data,sizeof(data));

//close pipe file
close(fd);

return 0;
}

Reader process file:

/****  READER PROCESS FILE    *******/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<string>
#include<math.h>
int main()
{
//variable to store data being read
char buffer[13];

//open pipe file and store its fd
int fd = open("/tmp/fifo2",O_RDONLY);


//read from the file
read(fd,buffer,sizeof(buffer));

//string s is to store integer input i.e. a*b
std::string s="";
int i=0;
while(buffer[i]!=' ' && i<13)
{
        s+=buffer[i];
        i++;
}

//stoi(s) converts a*b into integer and sqrt calculates square root and store in num
int num = sqrt(stoi(s));

//print num
printf("%d ",num);

//print remaining string i.e. your name
while(i<13)
{
        printf("%c",buffer[i]);
        i++;
}

//close pipe file
close(fd);
return 0;
}

Related Solutions

C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
please use c write a program that utilizes pipes for Interprocess Communication (IPC). The program will...
please use c write a program that utilizes pipes for Interprocess Communication (IPC). The program will create a pipe(s) to allow the parent and child to communicate. The parent process will generate a random number based on the pid of the child process created. The parent will send this random number to the child and wait for a response (Received) from the child. After receiving a response from the child, the parent will send another message to the child telling...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c that accepts a list of numbers from the user. There are two threads: Main thread waits for the numbers to be entered by the user and for the second thread to finish sorting. It then asks user whether there are more lists or not. It will do this until the user indicates there are no more lists to be entered, at which point it...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
Write a c statement to declare variables of primitive data types, and assign values to the...
Write a c statement to declare variables of primitive data types, and assign values to the variables. Use common control structure in c, such as if, switch, break, for, while statements. Write a c program that takes command line parameters, and perform some computation based on the command line input. Write c statements to generate random number within a certain range. Write a c statement to declare array variables of primitive data types, and assign values to the elements of...
Write a program that inputs the values of three Boolean variables, a, b, and c, from...
Write a program that inputs the values of three Boolean variables, a, b, and c, from a “cin” operator (user gives the values be sure to prompt user for what they have to give!). Then the program determines the value of the conditions that follow as true or false. 1. !(a&&b&&c) && ! (a||b||c) 2. !(a||b)&&c Output should include the values of a,b,c ie 0 or 1 in the patterns that follow reflecting the Boolean logic being tested. Where “True”...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT