How to use fork to create processes

Assignment Help Computer Engineering
Reference no: EM131451357

In this assignment, you will learn how to use fork to create processes, and how to use Linux pipes and shared memory for process synchronization. Specifically, you will create a program called trans. The program takes two command line parameters, an input file and an output file. It copies the input file to the output file using the shared memory. For example, to invoke the program, type

trans input-file output-file

at the command line prompt.

Here is how the program works: your program will create two processes. a parent process and a child process, using fork. These two processes share two pipes, one pipe carries data from the parent to the child and the other carries data in the opposite direction (a regular pipe is unidirectional.) The parent and the child also shares a 4KB memory. To transfer the file, the parent first reads a block of 4KB data from the input file into the shared memos y (the last block of the data might be less than 4KB if the file size is not a multiple of 4K13). and sends the block number and the block length to the child through its pipe. After receiving the block number and the block length, the child writes the data from the shared memory to the output file, and the sends the block number back to the parent as an acknowledgment through the other pipe. After receiving the correct block number from the child, the parent continues to transfer the next block. This process continues until the whole input file has been written to the output file. The following are some specific requirements:

1. Put all your code in a single file, called trans.c. Note that programs using the POSIX shared memory API must be compiled with cc-lrt to link against the real-time library, librt. You can use the following command to compile your code:

gcc -sbd=c99 - Wall  -Wextra -D_XOPEN_SOURCE=700 -o trans trans.c -lrt

2. Your code must work with both binary and text files. For example, you can test it with the generated trans file, which is a binary file. You may use fread/fwrite to access files. It should also print out a helpful error message. It should also promote the user whether to overwrite the file if the output file already exists.

3. Make sure the input and output files are the same after running trans. You can compare their checksums using shasum.

4. Use Linux pipe to create pipes.

5. Use the POSIX shared memory. NOT Sys V shared memory. That is, your program should NOT use SysV APIs such as shmget and shmat. Instead, use shm_open and mmap. POSIX shared memory is the preferred way to use shared memory on Linux.

The shared memory must be created after the process has forked.

When you name your shared memory object, include yourNAME to avoid conflicts. For example, use just use "NAME" as the name in shin open.

6. The data is transferred in blocks. Each block is 4KB bytes (except maybe the last one). The block number counts how many blocks have been transferred. It starts from 1. So, if you are going to transfer a file of 10KB, the block numbers are 1, 2, and 3.

The block numbers and the block lengths should be sent and received as the standard C int. On linprog, it will be a 64-bit integer since linprog uses 64-bit linux. On a 32-bit Linux system, they will be 32-bit integers.

After all the blocks have been transferred, the parent process sends block number 0 and length 0 to the child. After receiving block number 0. the child replies the parent with block number 0 and then exit. The parent receives the child's acknowledgment to block 0 and exits too.

Therefore, the protocol to transfer the file is similar to the tftp protocol. For example, to copy a 6KB file, the parent writes the first 4KB data of the file to the shared memory and send two integers, 1 and 4096, to the child via the pipe. The child receives these two numbers, copies 4096 bytes from the shared memory to the output file. and sends back 1 to the parent via the other pipe. After receiving 1, the parent copies the left 2KB data to the shared memory and send 2 and 2048 to the child. The child receives them from the pipe, copies 2048 bytes to the output file. and replies with 2 to the parent. The parent then send 0, 0 to the child. The child receives 0 and replies with a 0 and then exit. The parent receives 0 and exits too. It is clear that in this protocol only one process can access the shared memory at a time.

7. Your code should include necessary error handling. For example, it should check the errors for fork pipes, and shared memory. Both examples given previously have the necessary error handling.

Attachment:- Assignment Files.zip

Reference no: EM131451357

Questions Cloud

What is the expected return of the portfolio : What is the standard deviation? What is the variance of this portfolio? What is the expected return of the portfolio?
Review the casefor and against terminating employment : An appropriate response. You sit on the board of directors of a major airline that just experienced a horrendous customer service event.
Five major project management processes : Kliem (2012) notes there are five major project management processes
Credibility risk premium to the required return : She decides to add an extra 1% “credibility” risk premium to the required return as part of her valuation analysis.
How to use fork to create processes : In this assignment, you will learn how to use fork to create processes, and how to use Linux pipes and shared memory for process synchronization
What is the crossover rate : Cummings Products Company is considering two mutually exclusive investments whose expected net cash flows are as follows. What is the crossover rate?
How each model represents best interests of the organization : Each group must map out its ideal model for corporate governance of an organization-for example, the number of people on the board of directors.
Investment to cover cost of your child college education : What annual rate of interest must you earn on your investment to cover the cost of your child's college education?
Federal reserve reacting to the strength economic activity : which one is directly related to the Federal Reserve reacting to the strength (or lack there of ) overall economic activity?

Reviews

len1451357

4/5/2017 4:32:21 AM

I have included the instructions. Within the instructions there is mention of sample codes. I have also included these sample codes. These codes pretty much have everything needed. There are some things needed to be changed to fit the assignment.

Write a Review

Computer Engineering Questions & Answers

  Which parts of the assignment were you not able to complete

Which parts of the assignment were you not able to complete fully? For each, explain why you were unable to complete this part and what steps you took to attempt to complete it. Give me as much detail as possible such that I may award partial cred..

  Explain the operation of the modified register

One input of the NAND gate receives the clock pulses from the clock-pulse generator. The other input of the NAND gate provides a parallel-load control. Explain the operation of the modified register.

  Find out the total average response time

Now assume a cache is installed is the institutional LAN. Suppose the hit rate is 0.5. Find the total response time.

  Describe a use case dependency for making an account deposit

Describe (in a one to two page narrative) a use case dependency for making an account deposit. Illustrate this use case with Visio or a similar product.

  How disaster protocols will mitigate threats to organization

How the disaster response protocols will mitigate the threats to and vulnerabilities of the organization. Support your justification with information assurance research and best practices.

  Problem on human computer interface

You are booking a flight from Port of Spain Trinidad to VC Bird airport in Antigua. You will be using LIAT and Caribbean Airlines websites to do so and noting a few comparisons along the way.

  Cbc-mode encryption

Let a variant of CBC-mode encryption in which the sender simply increments the IV by 1 each time a message is encrypted in place of choosing the IV at random each time.

  Make a phonebook application using c++

make a Phonebook application using C++ STL (standard template library). Use the telephone number as the key and the name as the value.

  Write down a shell program

-Check if there is exactly 2 filenames, if not, printout error message

  Why are most programs yet in cobol

Leibs, (2008) reported on a novel form of outsourcing software development. express the approach and share your thoughts about this new approach to software development.

  Write a reflective essay of 750-1000 words on the

write a reflective essay of 750-1000 words on the historical development of policing and the unique challenges faced

  Write separate function of type void that accepts the amount

Write a separate function of type void that accepts the amount to dispense and returns the number of bills for each denomination.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd