Ask Computer Engineering Expert

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

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92260097

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As