You are to write a C or C++ program that will spawn X processes cooperating with
ID: 3888167 • Letter: Y
Question
You are to write a C or C++ program that will spawn X processes cooperating with each other to execute two steps of a given task. You will have to set up a pipe to allow the output of the first X-1 processes (the producers) to become the input of the other one (the consumer). The producer processes access and modify a shared resource (a file) and thus need to be synchronized. Your program should: 1. use a pipe() system call to create a communication channel having one read file descriptor and one write file descriptor (the pipe), 2. divide itself into X processes using a fork() system call, creating X-1 child processes (the producers). 3. redirect the standard output of the child processes to the write field descriptor of the pipe and the standard input of the parent process to the read field descriptor of the pipe, 4. have each child process execute the producer program while its parent executes the consumer program. Your producer processes should read data from an arbitrary text file passed as first argument of your main program and echo it after having done a minimum amount of text formatting detailed below. The second argument to your main program is the value of X (indicating the total number of processes). Whenever a number (integer) is found in the text, each of the X-1 child (producer) processes should increment/decrement that number by the number specified as the third argument to your main program. Thus the number 50 becomes 80 if the third argument is 5. Similarly, the number 50 becomes 30 if the third argument is -5. However, the non-numeric text should be left intact. The content should be output by the consumer process with the modification done to each number. This, of course, requires synchronization between the producer processes. Note that the producers can be thought of as X-1 ATM processes making deposits/withdrawal to the same bank account. 1. input lines should be filled and joined to produce output lines of up to 72 characters, 2. blank lines should be preserved, as well as the spacing between words, 3. indentation should be preserved and input lines with differing indentation should not to be joined, and 4. substrings that do not contain blanks or tabs should not be divided or hyphenated at the end of a line. Your consumer program should display its input twenty lines by twenty lines asking the user to hit the return key to let the process continue. Hints Before you start your assignment, familiarize yourself with the way C/C++ programs handle argu- ments that are passed to them by execv() and with the UNIX functions fork(), pipe() and dup(). A Last Word The programs you turn in should be well documented. Each C/C++ func- tion should start by a brief description of its purpose and its parameters. Each variable declaration should contain a brief description of the variable(s) being declared.
Explanation / Answer
//Please execute in visual C#
// CPPProsj.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUFSIZE 1024
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hPipes;
LPTSTR vMessage=TEXT(" message from client.");
TCHAR charBuf[BUFSIZE];
BOOL fboolSuccess = FALSE;
DWORD pbRead, pbToWrite, pbWritten, wMode;
LPTSTR CheggPipename = TEXT("\\.\pipe\pipename");
if( argc > 1 )
vMessage = argv[1];
while (1)
{
hPipes = CreateFile(
CheggPipename, // pipe name
GENERIC_READ | // Read and write access
GENERIC_WRITE,
0, // no sharing
NULL,
OPEN_EXISTING,
0,
NULL);
if (hPipes != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
_tprintf( TEXT("Could not open pipe"), GetLastError() );
return -1;
}
if ( ! WaitNamedPipe(CheggPipename, 10000))
{
printf("error message.");
return -1;
}
}
// The pipe connected; change to message-read mode.
wMode = PIPE_READMODE_MESSAGE;
fboolSuccess = SetNamedPipeHandleState(
hPipes,
&wMode,
NULL,
NULL);
if ( ! fboolSuccess)
{
_tprintf( TEXT("SetNamed failed."), GetLastError() );
return -1;
}
// Send a message to the pipes server.
pbToWrite = (lstrlen(CheggPipename)+1)*sizeof(TCHAR);
_tprintf( TEXT("Sending message: "%s" "), pbToWrite, CheggPipename);
fboolSuccess = WriteFile(
hPipes,
vMessage,
pbToWrite,
&pbWritten,
NULL); // not overlapped
if ( ! fboolSuccess)
{
_tprintf( TEXT("WriteFile to pipe failed. Please check connection GLE=%d "), GetLastError() );
return -1;
}
printf(" Rcv msg: ");
do
{
// Read data from the pipe.
fboolSuccess = ReadFile(
hPipes, // pipe handle
phBuf,
BUFSIZE*sizeof(TCHAR),
&pbRead,
NULL);
if ( ! fboolSuccess && GetLastError() != ERROR_MORE_DATA )
break;
_tprintf( TEXT(""%s" "), chBuf );
} while ( ! fboolSuccess); // repeat loop if ERROR_MORE_DATA
if ( ! fboolSuccess)
{
_tprintf( TEXT("ReadFile from pipe failed. GLE=%d "), GetLastError() );
return -1;
}
printf(" <End of message, press ENTER to exit >");
_getch();
CloseHandle(hPipes);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.