1. Implement a process that creates a child process, and communicates with it using pipe.
Parent process sends to child process 2 numbers and the child process returns through pipe their sum.
Possible solution (Be aware that some solutions contain mistakes, as explained at the lab).
----------------
#include....
int main() {
int p2c[2]; // pipe parent to child
int c2p[2]; // pipe child to parent
int a, b, s, pid;
pipe(p2c);
pipe(c2p);
pid = fork();
if(pid == 0) { //child process
read(p2c[0], &a, sizeof(int));
read(p2c[0], &b, sizeof(int));
s = a+b;
write(c2p[1], &s, sizeof(int));
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
exit(0);
}
//parent process
a = 3;
b = 7;
write(p2c[1], &a, sizeof(int));
write(p2c[1], &b, sizeof(int));
read(c2p[0], &s, sizeof(int));
printf("%d + %d = %d\n", a, b, s);
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
wait(0);
return 0;
}
Process hierarchy:
P
|
C
Have you found the code issues:
1. Close pipe ends as soon as possible, especially those that will not be used: eg. in the child process (if fork()==0 { close p2c[1]; close c2p[0]; read (p2c[0]....) and the same in the parent process
2. Check if fork fails (pid== -1), otherwise issues in main.
FIFO
2. Implement a process that creates two child processes A and B, that communicate using fifo. Process A sends to process B 2 numbers and process B returns to A a random number between the two.
(Fifo created in the main c program)
---------------------------------------------- ------------------------
#include ...
int main(int argc, char **argv){
mkfifo("fifob2a", 0666);
mkfifo("fifoa2b", 0666);
int fa2b, fb2a;
int fa = fork();
if (fa < 0)
{
printf("error");
exit (1);
}
if (fa == 0) // process A
{
int x1, x2;
scanf("%d", &x1);
scanf("%d", &x2);
fa2b = open("fifoa2b", O_WRONLY);
write(fa2b, &x1, sizeof(int));
write(fa2b, &x2, sizeof(int));
close(fa2b);
fb2a = open("fifob2a", O_RDONLY);
int d;
read (fb2a, &d, sizeof(int));
printf("%d", d);
close(fb2a);
exit(0);
}
else
{
int fb = fork();
if (fb < 0)
{
printf("error");
exit (1);
}
if (fb == 0) //process B
{
fa2b = open("fifoa2b", O_RDONLY);
int x, xx;
read(fa2b, &x, sizeof(int));
read(fa2b, &xx, sizeof(int));
close(fa2b);
fb2a = open("fifob2a", O_WRONLY);
int r = rand();
if (x > xx)
{int aux = xx;
xx=x;
x = aux;}
while (!(r>x && r<xx))
{
r = rand();
}
write(fb2a,&r, sizeof(int));
close(fb2a);
exit(0);
}
}
wait(0);
wait(0);
return (0);
}
Process hierarchy:
P
/ \
A B
Code issues:
1. Unlink missing to destroy fifo just after wait.
2. Could use srand(time(null)) - see manual - and something of the form (rand()%xx +x )%xx instead of while...rand...
3. Implement a program that creates 2 child processes A and B, which communicate using FIFO.
Process A sends to process B a number 70>n>10; process B receives this number, subtracts 4 and sends it to process A;
process A reads the number from B, decreases the number by 1 and sends it to B... and so on, until the number n has 1 digit.
(Fifo created in the command line using mkfifo)
Solution can be of the form:
------------------------------------------------------------------------------------------------
#include ...
int main()
{
//declarations, read n...
if ((pa = fork()) == 0) // process A
{
fa2b = open("fifoa2b", O_WRONLY); //open fifos
fb2a = open("fifob2a", O_RDONLY);
write(fa2b, &n, sizeof (int)); //start the game: process A performs the first writing operation
while (n > 9) {
//read n from B...
//computations, n--;
//write new n to B...
printf("A: n=%d\n", n);
}
close(fa2b); close(fb2a); exit(0); //close fifos and exit
}
if ((pb = fork()) == 0) // process B
{
//open fifos ...
while (n/10 > 0) {
//read n from A...
//n-=4;
//write new n to A...
printf("B: n=%d\n", n);
}
// close fifos and exit...
}
//check pa and pb in case we have fork error...
//wait for processes and return 0...
}
Training: Implement the entire problem. Print data (number n) received/send by each process, to better follow the execution flow of the program.
Other problems
4. Write a program that creates a child process. The two communicate through a pipe. The parent reads a string with >25 characters and sends it to the child, which removes 1 vowel and sends it to the parent, which removes the first and the last character and sends it to the child back which removes again a vowel and sends it back .... and so on untill the string contains 3 or less characters.
Print the intermediary results and consider the initial string does not contain spaces or tabs, but only alphanumeric characters. You can use strstr() function.
NOTE ABOUT STRINGS: If you have issues when sending strings through pipe/fifo
(1) add \0 (null) at the end of the string to mark its end or/and send a string with strlen+1
(2) make sure buffer is correctly allocated (read about char s[15]="MyString"; fgets, scanf, malloc, strcpy, memset - to set buffer to 0 )
(3) check the return value of read/write - the actual number of bytes written - as it might be different than expected.
5. Write two independent programs A and B that communicate using fifos. Program A reads words from keyboard and send them to process B, receiving back the word in uppercase letters and a number representing the number of letters of the word. Program B received from A a word, computes the corresponsing word with uppercase letters and number of letters and sends these to to program A. This continues in a loop, untill program A sends word "000" and receives back the same word and number 0 and terminates. So doesa program B, when received "000", sends to A "000" and number 0 and terminates.
6. Six children are in a circle, playing the alphabet game.
One of them starts with A, the next continues with B and so on, until they reach Z.
A dice determines the player that starts the game.
Write a program with 6 processes to model this game: each process receives a letter, prints out their ID and the next letter in the alphabet and sends it to the next process.
The transmission of data is unidirectional throughout the game.
Use a random function to determine the player that starts the game (players are named: 1, 2, 3, 4, 5, 6).
Print out the number of the player that finishes the alphabet.
Solve the problem using pipes.
7. Model problem 6 such that one of the 6 children randomly skips an alphabet letter.
When this mistake happens, the direction of the game is changed.
You will need an additional set of pipes, in order to have one for each direction of the game (clockwise/counterclockwise).
Solve the problem using fifos.