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

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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