Lab 3 - Inter-Process Communication
Lab 3 - Inter-Process Communication
Matric No:
Group:
Signals, to be short, are various notifications sent to a process in order to notify it of various
"important" events. Each signal has an integer number that represents it (1, 2 and so on), as
well as a symbolic name that is usually defined in the file /usr/include/signal.h or one of the
files included by it directly or indirectly such as:
• INT
• HUP
• TSTP
• ABRT
• Ctrl + Z
Pessing this key causes the system to send a TSTP signal (SIGTSTP) to the running
process. By default, this signal causes the process to suspend execution
• Ctrl + \
Pressing this key causes the system to send a SIGQUIT signal (SIGQUIT) to the
running process.
• Ctrl + D
Pressing this key causes the system to send a EOF signal (EOF) to the running
process.
Please compile and run the following code on your Ubuntu / Linux. Press Ctrl + C on your
keyboard will the program running. Observe the output
Page | 1
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
int main(void)
char s[200];
perror("signal");
exit(1);
printf("Enter a string:\n");
perror("gets");
else
return 0;
What is the terminal output when you press Ctrl + C on your keyboard? Please provide
screenshot. Please briefly explain why
Page | 2
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
Now based on code above, write a program that will capture all these Signals: SIGINT,
SIGTSTP and SIGQUIT. When a signal is received, program will output: “This is a special
signal handler for <signal>” substitute <signal> with the correct signal. Please provide
screenshot of your program output
Page | 3
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
A pipe is a form of redirection (transfer of standard output to some other destination) that is
used in Linux and other Unix-like operating systems to send the output of one command /
program / process to another command / program / process for further processing.
We can use pipe to redirect the output of one command to another command in our terminal
/ shell. The linkage between the two commands is facilitated by the GNU / Linux kernel,
which takes care of connecting the two together.
Please run all of the following command on your linux terminal and observe the output.
$ ls –l | more
Please provide screenshot for the output for all the command stated above
Now use what you have learn about pipe in terminal, please provide the command to find a
line in ifconfig command manual page which match a word “down” and find the word count
from the line output
Page | 4
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
The pipe() system function is used to open file descriptors, which are used to communicate
between different Linux processes. The syntax of the pipe() function is:
The first element of the pipefd array, pipefd[0] is used for reading data from the pipe. The
second element of the pipefd array, pipefd[1] is used for writing data to the pipe. On success,
the pipe() function returns 0. If an error occurs during pipe initialization, then the pipe() function
returns -1.
We can use pipe to send an integer value or even a float value. Please compile and run the
following code on your Ubuntu / Linux. Observe the output
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(void) {
int pipefds[2];
int buffer;
if(pipe(pipefds) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
Observe how the number is saved and retrieve back from the pipe. Please provide
screenshot for the output of the program above.
Page | 5
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
We can also use pipe to send a character or a string value. Please compile and run the
following code on your Ubuntu / Linux. Observe the output
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(void) {
int pipefds[2];
char buffer[5];
if(pipe(pipefds) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
Observe and try to understand how the character is saved and retrieve back from the pipe.
Please provide screenshot for the output of the program above
Page | 6
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
In code above, we used to write and read data from pipe in the same process. But usually
pipe is used for inter-process communication.
Please compile and run the following code on your Ubuntu / Linux. Observe how the child
process generate a value and send it to the parent process.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define PIN_LENGTH 4
#define PIN_WAIT_INTERVAL 2
pin[0] = 49 + rand() % 7;
pin[PIN_LENGTH] = '\0';
}
int main(void) {
while(1) {
int pipefds[2];
char pin[PIN_LENGTH + 1];
char buffer[PIN_LENGTH + 1];
pipe(pipefds);
pid_t pid = fork();
if(pid == 0) {
getPIN(pin); // generate PIN
close(pipefds[0]); // close read fd
write(pipefds[1], pin, PIN_LENGTH + 1); // write PIN to pipe
exit(EXIT_SUCCESS);
}
if(pid > 0) {
wait(NULL); // waiting for child to finish
return EXIT_SUCCESS;
}
Page | 7
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
You can stop the program from continuing creating a child and terminate the program by
sending SIGINT signal by pressing Ctrl + C on your keyboard. Please provide screenshot for
the output of the program above.
Page | 8
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
In real life scenario, we to use computer to calculate complex equation or create a server to
handle multiple connection. Instead of computing things like that in the same process as the
main program, you can just calculate the hash on a child process and return the hash to the
main process. The child can return the value to the parent function by using pipes.
Please compile and run the following code on your Ubuntu / Linux. Observe how the child
process generate a value and send it to the parent process.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int getPIN() {
// use PPID and PID as the seed
srand(getpid() + getppid());
int secret = 1000 + rand() % 9000;
return secret;
}
int main(void) {
int fd[2];
pipe(fd);
pid_t pid = fork();
if(pid > 0) {
close(0);
close(fd[1]);
dup(fd[0]);
int secretNumber;
size_t readBytes = read(fd[0], &secretNumber, sizeof(secretNumber))
;
return EXIT_SUCCESS;
}
Observe how the child process do some calculation and return the value to parent process
using a pipe. Please provide screenshot for the output of the program above
Page | 9
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
Now based on what you have learn in this lab, modify above code or create a new code to
create a program which a child process will ask user to enter a number, then using a pipe the
child pass the number to parent process to check whether it is prime number or not. After the
parent process has printed out the checking result, it can end the process. Moreover, your
program must have a signal handler for SIGINT signal. Please provide screenshot of your
output.
References
https://linuxhint.com/pipe_system_call_c/
Page | 10
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.