Professional Documents
Culture Documents
Accelerometer Specifications
Accelerometer Specifications
Accelerometer Specifications
Submission:
- One compressed file that contains all C and pdf files
- Make sure to do the following:
- Check the success of every system call you use
- When using pipes, make sure to close the pipe-side the process will not use
Description
1. Send messages to child processes:
Write a C program that creates 2 child processes (C1 and C2) and 2 pipes. The parent process will
facilitate sending any number of messages from the user to either of the child processes as shown in
Figure 1 below.
Parent
C2
C1
The parent process reads the ID of the destination process as well as the message itself from the user
and sends the message through the correct pipe to its intended destination. The parent, being the
sender process, ignores any messages that are destined to neither of the two child processes. As soon as
a child process receives a message it prints it to the screen as shown in the sample run below (Figure 2).
The parent process will keep on reading messages from the user and sending them to the destination
processes until the user types -1 for the ID of the destination. It then closes all open pipes and
terminates. Make sure to test for the success of the different system calls you use in your code and that
the parent process waits for its children before it terminates.
Test your code by changing the pipe() system call to pipe2() and test the behavior of your program
when you set the non-blocking flag O_NONBLOCK.
Note: All labs are due before next week’s lab starts
Lab 08 CIS 370 Due: Week of 11/12/2018
- After opening the pipes and creating the children, the parent process should prompt the user
for the messages in the form: "<child_to_receive_msg> <one_word_message>":
- You may assume that messages are only one word in length (you do not need to handle spaces
in messages).
- The parent will then use pipes P1 and P2 to send all the messages to the appropriate children.
- Each process should ensure that its pipes are unidirectional.
- Once received, a message is printed out in the form:
---> Child 1 received: msg_1_to_child_1
- 3 messages are sent to child process 1. These messages are received and printed by process 1.
- 2 messages were sent to child process 2. The messages were received and printed by process 2.
- A message was sent to process 7. It was ignored and an error message was printed by the
parent process.
- The process terminates as soon as the user types -1 and hits enter.
Hints:
- To avoid blocking reads in the children, you should consider what happens when processes close
one end of a pipe.
- To guarantee the order of printing to the screen make sure that the parent process sleeps for a
fraction of second. Investigate using the function usleep().
- Assume a maximum message size of 64 bytes.
Note: All labs are due before next week’s lab starts
Lab 08 CIS 370 Due: Week of 11/12/2018
Parent
C1 C2
Hints:
- Create 4 pipes in the parent process.
- Be careful when closing the pipe ends your process does not need.
- Keep in mind that pipes are byte streams so make sure to properly cast the values you are
sending through the different pipes. See example below:
int C1_PID, someIntVar;
write(c1_to_p[1], (int *) &C1_PID, sizeof(C1_PID));
……….
read(c1_to_p[0], (int *) &someIntVar, sizeof(someIntVar));
Use the following data structure to represent the data that will be shared between the producer and
the consumer processes:
Note: All labs are due before next week’s lab starts
Lab 08 CIS 370 Due: Week of 11/12/2018
#define MAX_SIZE 8
typedef struct bufferStruct {
int in;
int out;
int content[MAX_SIZE];// will hold ASCII calues of characters
}bufferStruct;
- Every time the user hits enter in the producer process, it should generate random integer value
and store it into the shared buffer until the buffer is full. If the user hits enter after that then a
“buffer is full …” error message should be printed.
- Every time the user hits enter in the consumer process, it should consume the oldest item in the
buffer and print it to the screen, until the buffer is empty. If the user hits enter after that then a
“buffer is empty …” error message should be printed.
- Both programs should terminate if the user enters a ‘0’;
#ifndef BUFFER
#define BUFFER
#define MAX_SIZE 8
int in;
Time
int out;
int content[MAX_SIZE];
}bufferStruct;
Note: All labs are due before next week’s lab starts