Requirements : written in C in bash shell • For this project I want you to write
ID: 3919694 • Letter: R
Question
Requirements: written in C in bash shell
• For this project I want you to write, in C, 2 programs to pass information from one to the other.
• First write a C program main.c to create a child process; then store pid, ppid (of the parent and child process) and current time into a file called log.txt. Parent process waits for the child to terminate, when that happens write the patent and child’s status code to the file (use WEXITSTATUS()). Name the executable file math.
• Create another C program square.c to find square of a number that is passed from command line argument when executing math and store the answer into log.txt as well. Name the executable file square. This should be done by the child process and use of exec() family call .
Make sure to create just one child process. I’d like to see the use of perror()and git. I also want a Makefile with a target ‘all’ that makes both of the executables (math and square.) I do not want you to turn in any .o file or the executables.
Program runs as below:
$ math 9
$ cat log.txt
9 squared is: 81
Child pid: 6956 ppid: 6955 date: Sun Jul 29 17:35:12 2018
Child Exit code: 37
Parent pid: 6955 ppid: 12400 date: Sun Jul 29 17:35:12 2018
Parent Exit code: 117
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int file_des[2];
int value = 0;
int val;
// pipe function create pipe descriptors one for read and one for write
pipe(file_des);
// fork() function returns 0 for child process, child-pid for parent process.
if (fork() != 0)
{
printf("Enter the integer value that send to child process:");
scanf("%d",&value);
//write data
write(file_des[1], &value, sizeof(value));
printf("Parent(%d) send value: %d ", getpid(), value);
// close the write descriptor
close(file_des[1]);
}
else
{
// child: reading only, so close the write-descriptor
close(file_des[1]);
// now read the data
read(file_des[0], &val, sizeof(val));
printf("Child(%d) received value: %d ", getpid(), val);
// close the read-descriptor
close(file_des[0]);
}
return 0;
}
$ gcc process.c
MyPc@DESKTOP-1NJB2AL //desktop-1njb2al/Users/ MyPc/Desktop/US /A/task3
$ ./a.exe
Enter the integer value that send to child process:230
Parent(16132) send value: 230
Child(13772) received value: 230
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int file_des[2];
int value = 0;
int val;
// pipe function create pipe descriptors one for read and one for write
pipe(file_des);
// fork() function returns 0 for child process, child-pid for parent process.
if (fork() != 0)
{
printf("Enter the integer value that send to child process:");
scanf("%d",&value);
//write data
write(file_des[1], &value, sizeof(value));
printf("Parent(%d) send value: %d ", getpid(), value);
// close the write descriptor
close(file_des[1]);
}
else
{
// child: reading only, so close the write-descriptor
close(file_des[1]);
// now read the data
read(file_des[0], &val, sizeof(val));
printf("Child(%d) received value: %d ", getpid(), val);
// close the read-descriptor
close(file_des[0]);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.