Question

Write a program that opens a file (with the open() system call) and then calls fork) to create a new process. Can both the ch
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
int fd;

fd = open("output",O_CREAT|O_TRUNC|O_WRONLY, 0666);

if(!fork()) {
/* child process */
  
char msg[1024];
sprintf(msg,"This is child with fd=%d\n",fd);
write(fd, msg , strlen(msg));
_exit(0);
} else {
/* parent process */
// int status;

// wait(&status);
char msg[1024];
sprintf(msg,"This is Parent with fd=%d\n",fd);
  
write(fd, msg , strlen(msg));
}
return 0;
}

1) When a fork system call is called. a new child process is created which has a copy of all variables of the parent process which called it.

So, When a child is forked then it inherits parent's file descriptors. So a copy of file descriptors of parent is made in the child process.

So, both the values of file descriptors are same in both the processes.

2) Both processes can access the same file descriptor concurrently.

After parent calls forks, parents process executes as is unless we mention wait() which makes parent to wait until the child process completes its execution. Otherwise both the processes run simultaneously.

while writing into a file, the offset address of file in file descriptor(or in File Table )is incremented automatically by write() function cal.

The writing can only be done by one processes at a time.

Now since the both processes are running simultaneosly and they share same fd and file offset, both processes tries to write at the same time one after the other.

So the output  contains data from both child and parent intermixingly.

To be more clear. run the below code which writes multiple lines.

CODE: -

#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
int fd;

fd = open("output",O_CREAT|O_TRUNC|O_WRONLY, 0666);
  

if(!fork()) {
/* child process */
  
char msg[1024];
sprintf(msg,"This is child with fd=%d\n",fd);
for (int i=0;i<100;i++)
write(fd, msg , strlen(msg));
_exit(0);
} else {
/* parent process */
// int status;

// wait(&status);
  
char msg[1024];

sprintf(msg,"This is Parent with fd=%d\n",fd);
for (int i=0;i<100;i++)
write(fd, msg , strlen(msg));
}
return 0;
}

Output file :

This is Parent with fd=3
This is child with fd=3
This is Parent with fd=3
This is child with fd=3

***Thank you . For further clarifications, please comment***

Add a comment
Know the answer?
Add Answer to:
Write a program that opens a file (with the open() system call) and then calls fork)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program using the fork() system call to do thefollowing. The parent process (main program)...

    Write a program using the fork() system call to do thefollowing. The parent process (main program) forks a process(CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. Have the parent invoke the wait () call to wait for both the child processesto complete before exiting the program. IN A C program

  • Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...

    Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • 2. capture This solution to this problem will require using some system calls that will be...

    2. capture This solution to this problem will require using some system calls that will be discussed in week 3. This program should accept the name of a file that includes a list of commands (one per line) to execute. Each line of the file should be executed by a subprocess and the output written to the file capture.txt For example, given the command file: /bin/ls-1 /bin/cat apple banana usr/bin/wc -1-wcanteloupe The output saved to capture.txt might be: W1 schubert...

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

  • Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...

    Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorthm: n={n / 2 , if n is even 3 * n + 1 , if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is:  35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,...

  • Complete the following C code by filling all “???”sto write a C program to create a...

    Complete the following C code by filling all “???”sto write a C program to create a child process. Before the child process is created, an integer variable A is defined and assigned value 10. Then in the child process, A is reduced by 5. After the child process completes, the parent process increases A by 5. Then what will A’s value be just before the parent process completes? Why? Write the code and run it to verify your answer. #include...

  • Questions: 1) Write a simple program to create three processes using fork() commands. Use any three...

    Questions: 1) Write a simple program to create three processes using fork() commands. Use any three of the six system calls to show how each child process executes new sub-programs using exec’s minions. Show the output of each of the child process with different data samples.   2) Write a simple program in your Linux environment using C++ to identify PID, PPID and GID for the processes created in Question 1 and display the real and effective user ID.   3) Modify...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the...

    Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the fork function to produce two processes. In the child process print out the lower case version of the string. In the parent process print out the reversed upper case version of the string (e.g. child: “abc”; parent: ”CBA”). You may call the toupper and tolower functions in the header <ctype.h> if you wish. Specify in the output whether the parent or child process is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT