Fork() with FIFO - c

I am having the toughest time with this assignment. So this assignment I have two children(two separate programs) and they have to write to the parent (main). The parent has to read both data from the kids and then print it out. I have to use named pipes. Well so my FIFO keeps giving me "USAGE: NAMEPIPECLIENT[String]" message and I don't know why. The message is on the client side by the way. Also if someone can point me in a good direction on how to use fork with multiply children on separate files that would be really appreciated.
Thanks in advance!Using GNU C
My Reader
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO" //default is current directory
int main(void){
FILE *fpoint;
char readbuffer[80];
int again = 1;
mknod(FIFO_FILE, S_IFIFO | 0666, 0);
while(again){
fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;
return(0);
}//exit main
}
My Writer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO"
int main(int argc, char *argv[]){
FILE *fpoint;
int again =1;
char strIn[80] = "Use message from command line";
if(argc !=2){
printf("USAGE: NamedPipeClient[string]\n");
exit(1);
}
strcpy(strIn, argv[1]);
while(again == 1){
if((fpoint = fopen (FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
fputs(strIn, fpoint);
fclose(fpoint);
printf("Enter message to send: ");
scanf("%s", strIn);
again = strcmp(strIn, "Stop");
}
if((fpoint = fopen(FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
fputs(strIn,fpoint);
fclose(fpoint);
return(0);
}

Here is an extensively corrected version of the writer process
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//#include<sys/stat.h>
//#include<linux/stat.h>
#define FIFO_FILE "MYFIFO"
int main(int argc, char *argv[])
{
if(argc !=2)
{
printf("USAGE: NamedPipeClient[string]\n");
exit(1);
}
FILE *fpoint;
if((fpoint = fopen (FIFO_FILE, "w")) == NULL)
{
perror("fopen");
exit(1);
}
char strIn[80];
strcpy(strIn, argv[1]);
int again =1;
while(again == 1)
{
fputs(strIn, fpoint);
printf("Enter message to send: ");
scanf("%79s", strIn);
again = strcmp(strIn, "Stop");
}
fputs(strIn,fpoint);
fclose(fpoint);
return(0);
}
This is the main reason that only one string is read:
while(again)
{
fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;
return(0);
}
Note that the function returns after only one pass through the loop.
Suggest:
while(again)
{
fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;
}
return 0;
Note: return is not a function, (similar to sizeof is not a function), so no parens needed.
Note: the continually opening and closing of the FIFO is not a good idea.
Suggest only open once in any one process.
Suggest only close once in any one process.
When calling fopen(), always check the returned value to assure the operation was successful.

Related

Unable to save a char array using fprintf()

I am unable to save a char array using fprint() and i cannot figure out why. The below codes compliles correctly but saves nothing to file. Please advise.
static char bitSpecial[100];
int main(int argc, char *argv[]) {
FILE *fp
fp = fopen(thefilename, "w+");
if (fp == NULL) {
printf("I couldn't open file for writing.\n");
exit(0);
}
/* populate bitSpecial one character at the time and verify array is full */
fprintf(fp,"%s", bitSpecial);
if (fclose(fp) != 0) puts("Unable to close the file");
return
}
It'll be easier to identify the problem with the full code. I tried the following snippet and it worked:
#include <stdio.h>
#include <string.h> // for strerror
#include <errno.h> // for errno
static char bitSpecial[100];
int main(int argc, char *argv[])
{
char * thefilename = "test";
FILE *fp;
fp = fopen(thefilename, "w+");
if (fp == NULL) {
printf("I couldn't open file for writing.\n");
return 1;
}
/* populate bitSpecial one character at the time and verify array is full */
bitSpecial[0] = 'a';
bitSpecial[1] = '\n';
bitSpecial[2] = '\0'; // terminator
if (fprintf(fp,"%s", bitSpecial) < 0)
printf("[+] fprintf failed with '%s'\n", strerror(errno));
if (fclose(fp) != 0)
puts("Unable to close the file");
return 0;
}
Verify that you put a null terminator (\0) at the end of bitSpecial, and check the return value of fprintf.

Segmentation fault instead of showing message - reading from a file by using pointers in c

I wrote a program, which reads from a file. I use a condition in which I print that the array is too big, but when I use a too big array instead of showing this message I have segmentation fault.
This is my program
#include <stdio.h>
#include <stdlib.h>
#define N 10000 // Maximum array size
int _strlen(char *array) {
int i;
for (i = 0; array[i] != '\0'; ++i);
return i;
}
int readText(FILE *wp, char *s, int max) {
int sum = 0;
if (_strlen(s) > max) {
printf("This array is too big. Maximum size is %d", max);
} else {
while ((*s++ = fgetc(wp)) != EOF) {
sum++;
}
*(s-1) = '\0';
}
return sum;
}
int main(int argc, char *argv[]) {
FILE *wz, *wc;
char *s;
char array[N];
s = array;
if (argc != 3) {
printf("Wrong arguments number\n");
printf("I should run this way:\n");
printf("%s source result\n",argv[0]);
exit(1);
}
if ((wz = fopen(argv[1], "r")) == NULL) {
printf("Open error %s\n", argv[1]);
exit(1);
}
if ((wc = fopen(argv[2], "w")) == NULL) {
printf("Open error %s\n", argv[2]);
exit(2);
}
fprintf(wc, "Read text from file source.txt");
readText(wz, s, 10000);
return 0;
}
In output I want to have: This array is too big. Maximum size is %d
Instead of Segmentation fault core dumped
In addition, I want to say that the program is when I use a smaller array, but I want to show the user a proper message when he uses too big array instead of segmentation fault.
Thanks, I change my program in that way. The only problem is that this program check the if condition in every while loop so this program could be slow.
int readText(FILE *wp, char *s, int max) {
int sum = 0;
if (_strlen(s) > max) {
printf("This array is too big. Maximum size is %d", max);
} else {
while ((*s++ = fgetc(wp)) != EOF) {
sum++;
if (sum > max) {
printf("This array is too big. Maximum size is %d", max);
break;
}
}
*(s-1) = '\0';
}
return sum;
}
The remarks / other answer solve your undefined behavior (segmentation fault in your case).
The only problem is that this program check the if condition in every while loop so this program could be slow.
Your program is not slow because of a 'if' but because you read the file char per char.
Using stat or equivalent function you can get the size of the file to read it throw only one fread :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#define N 10000 // Maximum array size
int main(int argc, char *argv[]) {
char array[N];
FILE *wz, *wc;
struct stat st;
off_t sz;
if (argc != 3) {
printf("Wrong arguments number\n"
"I should run this way:\n"
"%s source result\n", argv[0]);
exit(1);
}
if ((wz = fopen(argv[1], "r")) == NULL) {
printf("Cannot open %s to read : %s\n", argv[1], strerror(errno));
exit(1);
}
if (stat(argv[1], &st) == -1) {
printf("Cannot get stat of %s : %s\n", argv[1], strerror(errno));
exit(1);
}
if (st.st_size > N-1) {
printf("This array is too big. Maximum size is %d", N-1);
sz = N-1;
}
else
sz = st.st_size;
if (fread(array, 1, sz, wz) != sz) {
printf("cannot read %s : %s", argv[1], strerror(errno));
fclose(wz); /* for valgrind end test etc */
exit(1);
}
array[sz] = 0;
fclose(wz);
if ((wc = fopen(argv[2], "w")) == NULL) {
printf("Cannot open %s to write : %s\n", argv[2], strerror(errno));
fclose(wz); /* for valgrind end test etc */
exit(2);
}
/* ... */
fclose(wc);
return 0;
}
Knowing the size of the file allows to remove that limitation to a constant size and try to read the file while you can allocate enough memory for :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
char * array;
FILE *wz, *wc;
struct stat st;
if (argc != 3) {
printf("Wrong arguments number\n"
"I should run this way:\n"
"%s source result\n", argv[0]);
exit(1);
}
if ((wz = fopen(argv[1], "r")) == NULL) {
printf("Cannot open %s to read : %s\n", argv[1], strerror(errno));
exit(1);
}
if (stat(argv[1], &st) == -1) {
printf("Cannot get stat of %s : %s\n", argv[1], strerror(errno));
exit(2);
}
if ((array = malloc(st.st_size + 1)) == NULL) {
printf("Not enough memory to memorize the file %s\n", argv[1]);
exit(3);
}
if (fread(array, 1, st.st_size, wz) != st.st_size) {
printf("cannot read %s : %s", argv[1], strerror(errno));
fclose(wz); /* for valgrind end test etc */
free(array); /* for valgrind etc */
exit(4);
}
array[st.st_size] = 0;
fclose(wz);
if ((wc = fopen(argv[2], "w")) == NULL) {
printf("Cannot open %s to write : %s\n", argv[2], strerror(errno));
free(array); /* for valgrind etc */
exit(5);
}
/* ... */
fclose(wc);
free(array); /* for valgrind etc */
return 0;
}
Anyway because of the usage of the program "source result" may be you want to copy the file specified by argv[1] in the file specified by argv[2], in that case better to read and write block by block rather than to read all to not use a lot of memory for nothing and to manage the case the input file size is greater than the memory size.
You cannot measure the length of the destination array with _strlen(s), the size is given as an argument and reading an uninitialized array with _strlen() has undefined behavior.
Furthermore, you store fgetc(fp) to *s++ before testing for EOF. This is incorrect in all cases:
if char type is signed, EOF cannot be distinguished from a valid byte value of \377.
if char is unsigned, EOF cannot be tested because it has been converted as a char value of 0xff, hence the loop runs forever, writing beyond the end of the destination array until this causes a crash.
You simply want to add a test in the reading loop to stop reading bytes from the file when the buffer is full and read the bytes into an int variable so you can test for end of file reliably.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#define N 10000 // Maximum array size
int readText(FILE *wp, char *s, int max) {
int i = 0, c;
while (i < max - 1 && (c = fgetc(wp)) != EOF) {
s[i++] = c;
}
s[i] = '\0';
return i;
}
int main(int argc, char *argv[]) {
FILE *wz, *wc;
char array[N];
int nread;
if (argc != 3) {
printf("Wrong arguments number\n");
printf("I should run this way:\n");
printf("%s source result\n", argv[0]);
exit(1);
}
if ((wz = fopen(argv[1], "r")) == NULL) {
printf("Open error %s\n", argv[1]);
exit(1);
}
if ((wc = fopen(argv[2], "w")) == NULL) {
printf("Open error %s\n", argv[2]);
exit(2);
}
fprintf(wc, "Read text from file source.txt\n");
nread = readText(wz, array, N);
printf("Read %d bytes\n", nread);
return 0;
}

Trying to use CreateProcess to output a file, but hangs on output?

For part of a project, I have to be able to output a file through stdout using CreateProcess. However, it seems to be waiting for input after it outputs. It works when I just run source.exe by itself.
Main Program:
#include <winbase.h>
#define DELAY_A_WHILE() {volatile int j; for (j=0; j < 10000000; ++j) ; }
#define BUFSIZE 4096;
int main(int argc, char *argv[])
{
///VARS///
char cmdline1[MAX_PATH]; //Stores the command line for source.
char cmdline2[MAX_PATH]; //Stores the command line for sink.
STARTUPINFO StartupInfo; //Stores the startup information.
PROCESS_INFORMATION ProcInfoSource, ProcInfoFilter, ProcInfoSink; // Filled in by CreateProcess
// New Process Creation Flags:
DWORD CreateFlags = 0;
BOOL createStatus;
HANDLE sourceFilterRead, sourceFilterWrite, filterSinkRead, filterSinkWrite; //Handles for the two pipes.
//////////
//SECURITY_ATTRIBUTES PipeAttributes; //Pipe security attributes.
//PipeAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
//PipeAttributes.lpSecurityDescriptor = NULL; // ignore
//PipeAttributes.bInheritHandle = TRUE; // child can inherit
//////////
//////////SOURCE//
if (argc == 3)
{
strcpy(cmdline1, "Source.exe");
strcat(cmdline1, " ");
strcat(cmdline1, argv[1]); //Parse the first command line for source.exe.
strcpy(cmdline2, "sink.exe");
strcat(cmdline2, " ");
strcat(cmdline2, argv[2]); //Parse the second command line for sink.exe.
//if (!CreatePipe(&sourceFilterRead, &sourceFilterWrite, &PipeAttributes, 0)) { //Source | Filter
// printf("Error creating pipe: %d\n", GetLastError());
//}
///STARTUP///
GetStartupInfo(&StartupInfo);
/* StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
printf("%s\n", cmdline1);
printf("%s\n", cmdline2);
*/
createStatus = CreateProcess( NULL, cmdline1, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcInfoSource);
if(createStatus == 0)
{
printf("Source process failed.\n");
return(-1);
}
CloseHandle(ProcInfoSource.hProcess);
CloseHandle(ProcInfoSource.hThread);
printf("Source success.\n");
}
else
printf("Arg error\n");
return(0);
}
Source.exe:
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#define DELAY_A_WHILE() {volatile int j; for(j=0; j<10000000; j++);}
int main(int argc, char *argv[])
{
int ch;
FILE *fin;
if(argc != 2 || fopen_s(&fin, argv[1], "rb"))
{
fprintf(stderr, "Usage: source inputfilename\n");
return(1);
}
//Read chars and copy to stdout.
while(1)
{
if ((ch = fgetc(fin)) == EOF)
break;
fputc(ch, stdout);
DELAY_A_WHILE();
}
fclose(fin);
return(0);
}
EDIT: I found a solution. If I use WaitForSingleObject() right after I start the process, it works properly. Not sure if there's a way to do it without it. The goal of the assignment is to have a bunch of processes working simultaneously without waiting for one another, but I have not found another way to do it without waiting for source.exe to finish.

Error validating the contents of a file using regex in C

I am having a problem with the below code validating a file using regex. My file must only contain letters or numbers. My regular expression is:
#define to_find "^[a-zA-Z0-9]+$"
which is located in my main.h file. The below code is in my main.c
#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "main.h"
int main(int argc, char * argv[])
{
int ret_val;
regex_t regex;
FILE *fp;
char line[1024];
if (regcomp(&regex, to_find, REG_EXTENDED) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
return EXIT_FAILURE;
}
if (argc > 2)
{
printf("Usage: tree OR tree [filename]\n");
return EXIT_FAILURE;
}
else if (argc == 2)
{
fp = fopen(strcat(argv[1],".dat"), "r");
printf("file opened\n");
while ((fgets(line, 1024, fp)) != NULL)
{
line[strlen(line) - 1] = '\0';
if ((ret_val = regexec(&regex, line, 0, NULL, 0)) != 0);
{
printf("Error: %s\n", line);
return EXIT_FAILURE;
}
}
fclose(fp);
printf("File closed\n");
}
return 0;
}
My file I am reading is called names.dat and contains:
int
char
[
double
What is happening is it is kicking out at the very first line which it should kick out at the 3rd line. I am sure this is pretty simple to solve but it seems I have not figured it out. I would appreciate any help. Also, how do I deal with the
\n
character in the file? this file will have several lines. Thanks in advance.
You have some small errors but the one that cause the error is:
// Do you see this sweet little semicolon :P ----------------+
if ((ret_val = regexec(&regex, line, 0, NULL, 0)) != 0); // <+
{
printf("Error: %s\n", line);
return EXIT_FAILURE;
}
beside this line:
fp = fopen(strcat(argv[1],".dat"), "r");
You cannot add to argv, you need to create a new buffer to hold the data, create a buffer with PATH_MAX size add append the path to it. Here an improved version:
#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>
#define to_find "^[a-zA-Z0-9]+$"
int main(int argc, char * argv[])
{
int ret_val;
regex_t regex;
FILE *fp;
char file[PATH_MAX];
char line[1024];
if (regcomp(&regex, to_find, REG_EXTENDED) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
return EXIT_FAILURE;
}
if (argc > 2)
{
printf("Usage: tree OR tree [filename]\n");
return EXIT_FAILURE;
}
else if (argc == 2)
{
sprintf(file, "%s.dat", argv[1]);
fp = fopen(file, "r");
if( fp == NULL ) {
perror("Error");
return -1;
}
printf("file opened\n");
while (fscanf(fp, "%1023s", line) > 0)
{
if ((ret_val = regexec(&regex, line, 0, NULL, 0)) != 0)
{
printf("Not match: %s\n", line);
//return EXIT_FAILURE;
} else {
printf("Match: %s\n", line);
}
}
regfree(&regex);
fclose(fp);
printf("File closed\n");
}
return 0;
}
See the diff: http://www.diffchecker.com/8itbz5dy
test:
$ gcc -Wall sample.c
$
$ cat name.dat
int
char
[
double
$ ./a.out name
file opened
Match: int
Match: char
Not match: [
Match: double
File closed
$

Running a FIFO Simulation

I am trying to run a simulation program to test the FIFO algorithm, however my program is just crashing. this is the main, other functions not shown. Can anyone spot for me the problem.Am not so familiar with using the main Argument[ int main(int argc, char *argv[])]
I have the testing files in a folder
int main(int argc, char *argv[])
{
FILE *stream;
if (argc != 3)
{
printf("The format is: pager file_name memory_size.\n");
//exit(1);
}
printf("File used %s, resident set size %d\n", argv[1], atoi(argv[2]));
if ((stream = fopen(argv[1], "r")) == NULL)
{
perror("File open failed");
//exit(1);
}
mem_size = atoi(argv[2]);
start_simulation(stream);
fclose(stream);
system("pause");
}
Uncomment the calls to exit.
if (argc != 3) {
// insufficient arguments passed..print error and exit.
printf("The format is: pager file_name memory_size.\n");
exit(1);
}
In your case (exit commented) if you don't provide command line arguments, argv[1] will be NULL and this can cause crash when used in fopen

Resources