To start off with this is a homework assignment for my Operating Systems course and I am stumped.
The prof wants us to read in a source code file and take out all the comment lines and then output the file back into a new source code file.
We can only use unbuffered input and output using the read() and write() commands. The only problems are that 1) I am getting caught when trying to get the process ID when I am trying to do parallel processes and I am not sure how to take certain characters out of the buffer or at least keep them from getting into the buffer in the first place.
Here is my source code. If you have any questions please ask and I will try to answer as best as I can. The openFilesSequential function is left blank on purpose because I have not gotten to it yet.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#define READ_FLAGS O_RDONLY
#define WRITE_FLAGS (O_WRONLY | O_CREAT | O_EXCL)
#define WRITE_PERMS (S_IRUSR | S_IWUSR)
#define BUFFER_SIZE 200
// Function Prototypes
void displayUsageMessage(void);
void openFilesSequential(int argc, char *argv[]);
void openFilesParallel(int argc, char *argv[]);
//#######################################################################
int main(int argc, char *argv[])
{
int i;
if(argc < 2)
{
displayUsageMessage();
return 1;
} // End if
else
{
if(strncmp(argv[1], "-s", 2) == 0)
{
openFilesSequential(argc, argv);
} // End if
else if(strncmp(argv[1], "-p", 2) == 0)
{
openFilesParallel(argc, argv);
} // End else if
else
{
printf("get here");
displayUsageMessage();
} // End else
} // End else
return 0;
} // End main
//########################################################################
void displayUsageMessage(void)
{
perror("Usage: a.exe {-s | -p} <file_name> [<file_name> ...]");
} // End displayUsageMessage
//########################################################################
void openFilesSequential(int argc, char *argv[])
{
} // End openFilesSequential
//#########################################################################
void openFilesParallel(int argc, char *argv[])
{
pid_t childpid = 0;
int i, nbrOfFiles;
char inputFileName[20];
char outputFileName[20];
nbrOfFiles = argc - 2;
for(i = 0; i < nbrOfFiles; i++)
{
strcpy(inputFileName, argv[2+i]);
strcpy(outputFileName, "nc-");
strncat(outputFileName, inputFileName, 20);
childpid = fork();
if(childpid == -1)
{
perror("Fork failed");
exit(1);
}// End if
else if(childpid == 0)
{
printf("\n\n(%6ld) Reading from %s and writing to %s ...\n", getpid(), inputFileName, outputFileName);
removeComments(i+2, argv, inputFileName, outputFileName);
exit(0);
}// End else if
else
{
exit(0);
}
} // End for
} // End openFilesParallel
//##########################################################################
int removeComments(int fileNum, char *argv[], char *inputFile, char *outputFile)
{
char myBuffer[BUFFER_SIZE];
int bufferIndex = 0;
int returnValue;
ssize_t bytesRead;
while(bufferIndex < BUFFER_SIZE - 1)
{
returnValue = read(argv[fileNum], myBuffer + bufferIndex, 1);
if(returnValue == -1)
{
return -1;
} // End if
if(returnValue == 0)
{
if(bufferIndex == 0)
{
return 0;
} // End if
else
{
errno = EINVAL;
return -1;
} // End else
} // End if
bufferIndex++;
if(myBuffer[bufferIndex - 1] == '\n')
{
myBuffer[bufferIndex] == '\0';
return bufferIndex;
} // End if
} // End while
bytesRead = returnValue;
if(bytesRead < 201)
{
write(outputFile, myBuffer, bytesRead);
}
errno = EINVAL;
return -1;
} // End removeComments
Thank you