Hi
I am programming in Unix. My task is really simple, but I don't know and where to debug this problem.

My C program opens up the "ls" manual and makes an exact copy to a new file.
However my program stops/freezes in the middle of read. (It works fine with smaller manual such as the "date" manual.


Why is there such problem and how can I fix it?

Thank you :)

Recommended Answers

All 12 Replies

Maybe you can post your code, or the bit that reads the file so we can see what you're doing. Maybe its a buffer overflow or something somewhere, not that it's a huge file, but still. (checked: 7.2 KB)

Black Box

Child 1 opens the file and read a character, it then passes the a character to Child 2. Child 2 now take the received character and writes it a file.

Child 1 stops reading when the read is EOF.
Child 2 stops writing when it receives a '\0'.

Thank You

if((child_1 = fork()) < 0) {
		printf ("Fork Child 1 Error\n");
		exit(1);
	} else if (child_1 == 0) {
		char charBuf = '\0';
		FILE *filePointer;
		int item;
		char m;
		if ((filePointer = fopen(filename, "r")) == NULL) {
			printf("Cannot open file \n");
			exit(1);
		}
		while ((charBuf = getc(filePointer)) != EOF) {
			mySend(&mailbox_consumer, &charBuf);
			fflush(stdin);
		}
		charBuf = '\0';
		mySend(&mailbox_consumer, &charBuf);
		fclose(filePointer);
		exit(0);
	} else {
		if ((child_2 = fork()) < 0) {
			printf("Fork child 2 error\n");
			exit(1);
		} else if (child_2 == 0) {
			/*****Comsumer*****/
			char charBuf = 'a';
			FILE *filePointer;
			int item, i;
			char m;
			if ((filePointer = fopen("out" , "w")) == NULL) {
				printf ("Cannot create file\n");
				exit(1);
			}
			while (charBuf != '\0') {
				myReceive(&mailbox_consumer, &charBuf);	//Check its owns mailbox
				fprintf(filePointer, "%c", charBuf);
			}
			exit(0);
		} else {
			if (waitpid(child_2, &status, 0) != child_2) {
				printf("Waitpid child 2 error\n");
			}
			myMessageClose(&mailbox_consumer);
		}

		if (waitpid(child_1, &status, 0) != child_1) {
			printf("Waitpid child 1 error\n");
		}
	}

Child 1 opens the file and read a character, it then passes the a character to Child 2. Child 2 now take the received character and writes it a file.

Child 1 stops reading when the read is EOF.
Child 2 stops writing when it receives a '\0'.

Thank You

And? Are you proud of the code and want us to marvel at it or do you have a problem?

Euhm, I think he outlined his problem well enough in the first post actually...?

However, I'm not immediately seeing a possible cause for the error.

Euhm,

What the heck is this?? Clearing your throat so you can type? :icon_rolleyes:

I think he outlined his problem well enough in the first post actually...?

Really? No code. No mention of child processes. Nothing to go on. Outlined, yes, you're right. Enough to help, no.

Post 2:
Code -- that's good.

Child 1 & 2 implies Parent code. Which of the three is the code posted? Only a vague description of the difficulty. All information in the post looks like it's the way the code should work for text files.

We need to know details of the problem, not just "it don't work, why?" And he would have known that if he bothered looking at Jishnu's link.

However, I'm not immediately seeing a possible cause for the error.

See? You don't know where to look nor what to look for.

I rest my case. :icon_razz:

Ok, first of all, that "euhm" thing is typically Dutch (it's a habit I forgot to turn off for that one post).

Second.
> Are you proud of the code and want us to marvel at it ...
Ok, he might not have made it very clear, that's true, but judging from your post, it was like you just didn't saw his first one. :)

Finally, I dedicate the fact that I'm not able to see the problem rather to my own ignorance than to the other one's complexity...

Overall, I think you're right, but throwing that sentence to him wasn't very supportive. :)

Black Box

Overall, I think you're right, but throwing that sentence to him wasn't very supportive. :)

Maybe not, but it's quite tiring day after day, week after week, to get "what's wrong" or "help me" posts with no information nor code. Especially when multiple times during their registration new members are requested to read the rules. And on top of that, right at the top of the forum is a post titled "Read Me: Read This Before Posting". If they can't be bothered, sometimes they have to take the heat, IMAO. We're volunteers, after all.

Thank you Black Box for your reply.
Yeah.. I dont see any problem as well, since this is a very basic code. Sigh....

The "ls" manual in unix is around 1.5 MB
while the "date" manual is around 200 KB i think

So where is the rest of your program? I guess this is a homework assignment? Because you could just use:

int main()
{
    system("man ls > newfile.txt");
    return 0;
}

So where is the rest of your program? I guess this is a homework assignment?

Yes this is a homework thing that I have to do. I can't figure out where to problem comes from :(

Thank You

And now back to the original problem: If all you want to do is make a copy of a file you don't need to do all that forking and other stuff. This is a very very basic problem that can be done in a short few lines of code.

Using google to initially research your problem would have saved you four days of wasted time. I will not duplicate the code here because you can easily find it in one of the google links I posted. But be warned that some of the code you find on the net isn't very well written. For example using while( !feof(in) ) is not the recommnended way of coding a loop like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.