Question
(Please hurry, I need it ASAP, I will be sure to give a thumb up) (Operating System) Using the codes, explain what the code does
(Please hurry, I need it ASAP, I will be sure to give a thumb up)
(Operating System)
Using the codes, explain what the code does and the system calls that make it happen. Also describe the different stages of the program as seen in the system call trace.
Write a program in C to demonstrate the working of Fork and Exec.
Demonstrate how do pipes enable IPC.
Expectation: codes and report stating the functionality, results, and the interpretation of the results.
code 1:
/* ----------------------------------------------------------------- */
/* PROGRAM fork-02.c */
/* This program runs two processes, a parent and a child. Both of */
/* them run the same loop printing some messages. Note that printf()*/
/* is used in this program. */
/* ----------------------------------------------------------------- */
#include
#include
#define MAX_COUNT 200
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d ", i);
printf(" *** Child process is done *** ");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d ", i);
printf("*** Parent is done *** ");
}
Code 2:
/* ----------------------------------------------------------------- */
/* PROGRAM fork-01.c */
/* This program illustrates the use of fork() and getpid() system */
/* calls. Note that write() is used instead of printf() since the */
/* latter is buffered while the former is not. */
/* ----------------------------------------------------------------- */
#include
#include
#include
#define MAX_COUNT 200
#define BUF_SIZE 100
void main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
fork();
pid = getpid();
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "This line is from pid %d, value = %d ", pid, i);
write(1, buf, strlen(buf));
}
}
code 3
#include
main() {
FILE *fp;
char buff[255];
fp = fopen("/tmp/test.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s ", buff );
fgets(buff, 255, (FILE*)fp);
printf("2: %s ", buff );
fgets(buff, 255, (FILE*)fp);
printf("3: %s ", buff );
fclose(fp);
}
code 4
#include
#include
#include
extern int errno;
int main()
{
// if file does not have in directory
// then file foo.txt is created.
int fd = open("foo.txt", O_RDONLY | O_CREAT);
printf("fd = %d/n", fd);
if (fd ==-1)
{
// print which type of error have in a code
printf("Error Number % d ", errno);
// print program detail "Success or failure"
perror("Program");
}
return 0;
}
code 5
#include
main() {
FILE *fp;
fp = fopen("./test.txt", "w+");
fprintf(fp, "This is testing for fprintf... ");
fputs("This is testing for fputs... ", fp);
fclose(fp);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started