Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topic is Operating Systems - Please code in C LAB INSTRUCTIONS . Please do your work on Odin in folder 3600/4/ Name your program: 3600/4/lab4.c

Topic is Operating Systems - Please code in C

LAB INSTRUCTIONS.

Please do your work on Odin in folder 3600/4/ Name your program: 3600/4/lab4.c

BEFORE THE FORK...

STEP 1. Generate an IPC key and open a log file.

To generate an IPC key, create a file named 'foo' in the directory in which you compile and execute your programs.

 $ cd $ cd 3600/4 $ touch foo 

For consistency throughout the rest of the course, we will use 'foo' to generate the IPC key. This means 'foo' needs to be in each directory in which you use IPC.

Open a file for writing named log. Only the child will write to this file but you should open it before the fork.

STEP 2. Create a shared memory segment to hold an integer.

You will use the IPC key you just created to do this. The source shared_mem.c communicates between a parent and a forked child using a shared memory segment large enough to hold an integer. Source shared_mem.c has all the code you need to implement shared memory - you just need to take what you need and leave what you don't behind. You can either copy shared_mem.c to lab4.c and delete/modify the existing code or start lab4.c from scratch. In either case, understand each and every line of code for shared memory that you copy. Do not use any code you do not understand! The system call to create the shared memory segment returns a shared memory id.

Create the segment. Attach to the shared memory segment. Initialize its value to 0. After a fork(2) the child inherits the attached shared memory segment so you will not need to attach again in the child.

STEP 3. Get a message queue & fork(2) a child

Use the same IPC key you have already created to get a message queue. Create the structure needed to send and receive messages (the child and the parent will use the same message structure and type of the message). You will be sending a message from parent to child after the fork. Look in msg_send.c for the code needed to get a message queue using the call msqget(2).

Message queues are designed to allow communication between unrelated processes. The only thing the child and the parent need in order to use the same message queue is the same IPC key. The only way for this to happen is to generate the IPC key before the fork since after the fork the child inherits the value of the IPC key.

Call fork(2).

IN THE CHILD...

STEP 1. Writes shared segment value to log

The parent will modify the shared segment to something other than zero but the child does not know when this might happen. The child must loop continuously on that value until it exceeds 0. This behavior is called busy-waiting. Not the best way to code but good enough for now. After the value change the child writes the shared memory value to its log file.

STEP 2. Write mesg from message queue to log

The child blocks on the queue until the parent sends a message. The code to do this is in msg_rcv.c. The child grabs the message and writes its contents to log.

STEP 3. Exits

The child closes log, detaches from shared memory and exits with status code 0. Note that the child does not clean up IPC objects. It is safer for the parent to do this after the wait.

IN THE PARENT...

STEP 1. Write user input to shared memory

The parent prompts the user to enter a 2-digit integer using the write() call; e.g.,

 Enter a number from 10 - 99: 

The prompt is a write call:

 write(1, prompt, strlen(prompt)); 

The parent reads the integer using read():

 read(0, buf, 4); // 0=stdin; 4 is how many characters you want the user to type 

These calls are in readwrite.c in the examples from last week. You should always clear out the buffer before you do a read:

memset(buf, 0, BUFSIZE); 

Since the integer will come in as a string it must be converted to an integer using atoi. The parent writes the integer to its shared memory segment.

STEP 2. Send user input as a message to the queue

The parent prompts the user to enter a word using write(). The parent reads the word using read(2). The parent constructs a message that holds this word and sends the message to the queue. The code to do this is in msg_send.c.

STEP 3. After child terminates remove IPC objects

The parent calls wait(2), grabs the child's exit code and writes the code to stdout. After that the parent cleans up all IPC objects.

To save time when testing your code create an input file that holds user input:

77 hieroglyphics

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Public Finance Fundamentals

Authors: K. Moeti

3rd Edition

148512946X, 9781485129462

Students also viewed these Databases questions