Question

Give code to set up a pipe or a FIFO for a parent process and it child process. have the child pr...

give code to set up a pipe or a FIFO for a parent process and it child process. have the child process send a message("i'm ok") to the parent process. the parent process should read the message into a buffer.

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

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
  
int main()
{
// pipe() is used to transfer information from one process to another in unidirectional way
  
int fd[2]; // Used to store reading and writing ends of pipe

  
char fixed_str[] = "i'm ok";
pid_t p;
  
if (pipe(fd)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}

p = fork();
  
if (p < 0)
{
fprintf(stderr, "fork Failed" );
return 1;
}
  
// Parent process
else if (p > 0)
{
// Read string into buffer from child and print the message.
char result_str[100];
  
//Wait for child to send message
wait(NULL);
  
//Read message from child and print the message
read(fd[0], result_str, 100);
printf("Message from child : %s\n", result_str);
close(fd[0]); //Close reading end
}
  
// child process
else
{
// Write message to be sent to parent
write(fd[1], fixed_str, strlen(fixed_str)+1);
close(fd[1]); //close writing end
  
exit(0);
}
}

Add a comment
Know the answer?
Add Answer to:
Give code to set up a pipe or a FIFO for a parent process and it child process. have the child pr...
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
  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Is a TV set bought by a parent for his 12-year old child and set up...

    Is a TV set bought by a parent for his 12-year old child and set up in her bedroom considered support? answer and which Applicable Internal Revenue Code Section it is related to.

  • How it is possible for the child process to have access to the same pipe which...

    How it is possible for the child process to have access to the same pipe which was created and later is used by the parent

  • Rewrite the program as a set of two programs that utilize a named pipe “ages” in...

    Rewrite the program as a set of two programs that utilize a named pipe “ages” in the current folder for IPC and implement the same function as discussed above. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) {         int     fd[2], nbytes;         pid_t   childpid;         int users = 95;         char mystring[80];         char    readbuffer[80];         char digit1,digit2;         digit1 = (char) users%10;         digit2 = (char ) users/10;         mystring[0] = digit1;         mystring[1] = digit2;...

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

  • (In Unix) Make a C program that creates a zombie child process. Both parent and child...

    (In Unix) Make a C program that creates a zombie child process. Both parent and child processes should execute the command "ps" to show the current process list. (1) ** Copy and paste your code in your report. ** (2) Take a screen shot that shows the defunct information generated by "ps".

  • Please read the question carefully and in full and then answer it (using C language and...

    Please read the question carefully and in full and then answer it (using C language and any necessary libraries). (I am not sure what the expert's mean by when to move to using pipe. I guess if you need to, then you can use a random time or any exact time but make sure to put a comment next to it). you might use the below code to give you some idea: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<string.h> #include<sys/wait.h> void main()...

  • This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process...

    This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process and then use the parent for reading and the child for writing.  This is just a way of sending and receiving messages asynchronously. /* ************************************************************* * Utility functions * ************************************************************** */ static void usageError(const char * progName, const char *msg) {...

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

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