Click here to Skip to main content
15,887,328 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionJust a minor KMDF concern Pin
Mattzimmerer30-Dec-09 10:47
Mattzimmerer30-Dec-09 10:47 
AnswerRe: Just a minor KMDF concern Pin
JudyL_MD30-Dec-09 11:10
JudyL_MD30-Dec-09 11:10 
QuestionIs there any way to execute XQuery through MFC or C++ (This question Previously left with no answer) Pin
A&Ms30-Dec-09 8:21
A&Ms30-Dec-09 8:21 
QuestionRe: Is there any way to execute XQuery through MFC or C++ (This question Previously left with no answer) Pin
David Crow30-Dec-09 9:40
David Crow30-Dec-09 9:40 
JokeRe: Is there any way to execute XQuery through MFC or C++ (This question Previously left with no answer) Pin
enhzflep30-Dec-09 12:00
enhzflep30-Dec-09 12:00 
GeneralRe: Is there any way to execute XQuery through MFC or C++ (This question Previously left with no answer) Pin
Tim Craig30-Dec-09 19:59
Tim Craig30-Dec-09 19:59 
AnswerRe: Is there any way to execute XQuery through MFC or C++ (This question Previously left with no answer) Pin
A&Ms31-Dec-09 0:20
A&Ms31-Dec-09 0:20 
QuestionPipe & Fork code - need help Pin
SummerBulb30-Dec-09 7:24
SummerBulb30-Dec-09 7:24 
Hi there.

I am required to write an application in C for Unix that does the following:
The program will create 3 child processes which read from 3 different files and write
to the same pipe in the parent process. Each child should wait a random amount of time (3 -10
seconds) between writing each 50 characters. The father should read from the pipe and write
everything he gets (from all 3 files) into one new file.

Here is my Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char* argv[])
{
	if (argc != 5)
	{
		printf("Error!\nUsage: tar1.exe <input file 1> <input file 2> <input file 3> <output file>\n");
		exit(0);
	}
	
	int fd[2];
	int fileOpen;
	int pids[3];
	int i;
	char buf[51];
	pipe(fd);	

	close(fd[1]); //father only reads from pipe

	for (i=1; i < 4; i++)
	{
		switch ((pids[i] = fork()))
		{
			case -1:
				printf("Fork error");
				break;
			case 0:
				close(fd[0]); //child only writes to pipe
				printf("Child %d created. File: %s\n",i ,argv[i]);
				if ((fileOpen = open(argv[i],O_RDONLY)) == -1)
				{
					printf("File %s does not exist.\n",argv[i]);
					exit(0);
				}
				printf("Child %d writing to pipe\n",i);
				while (read(fileOpen,buf,50) > 0)
				{
					buf[50] = NULL;
					printf("Child %d. Text: %s",i,buf);
					write(fd[1], buf, 50);
					sleep(rand()%8+3);
				}
				close(fd[1]); //child done writing
				close(fileOpen);
				printf("Child %d done writing to pipe\n",i);
				exit(0);
				break;
			default:
				break;
		}	
	}

	wait();
	wait();
	wait();

	if ((fileOpen = open(argv[4],O_WRONLY)) == -1)
	{
		printf("File %s does not exist.\n",argv[i]);
		exit(0);
	}
	printf("Father reading from pipe.\n");
	//sleep(rand()%8+3);
	while (read(fd[0],buf,50) != 0)
	{
		printf("Read line...\n");
		buf[50] = NULL;
		printf(buf);
		write(fileOpen, buf, 50);
		//sleep(rand()%8+3);
	}
	close(fd[0]);
	close(fileOpen);
	printf("Done reading!\n");
		
	return 0;
}


Running the application results with lots of rubbish in the output file.

Help will be highly appreciated.

Thanks!
AnswerRe: Pipe & Fork code - need help Pin
CPallini30-Dec-09 8:02
mveCPallini30-Dec-09 8:02 
QuestionRe: Pipe & Fork code - need help Pin
SummerBulb30-Dec-09 8:14
SummerBulb30-Dec-09 8:14 
AnswerRe: Pipe & Fork code - need help Pin
CPallini30-Dec-09 9:57
mveCPallini30-Dec-09 9:57 
QuestionRe: Pipe & Fork code - need help Pin
SummerBulb30-Dec-09 19:44
SummerBulb30-Dec-09 19:44 
AnswerRe: Pipe & Fork code - need help Pin
CPallini30-Dec-09 22:02
mveCPallini30-Dec-09 22:02 
GeneralRe: Pipe & Fork code - need help Pin
SummerBulb31-Dec-09 23:33
SummerBulb31-Dec-09 23:33 
GeneralYou are welcome Pin
CPallini1-Jan-10 7:15
mveCPallini1-Jan-10 7:15 
AnswerRe: Pipe & Fork code - need help Pin
markkuk30-Dec-09 13:02
markkuk30-Dec-09 13:02 
GeneralRe: Pipe & Fork code - need help Pin
SummerBulb30-Dec-09 19:39
SummerBulb30-Dec-09 19:39 
QuestionWhere can I find a forum about windows device driver development with WinDDK? Pin
sashoalm29-Dec-09 23:20
sashoalm29-Dec-09 23:20 
AnswerRe: Where can I find a forum about windows device driver development with WinDDK? Pin
David Crow30-Dec-09 3:02
David Crow30-Dec-09 3:02 
AnswerRe: Where can I find a forum about windows device driver development with WinDDK? Pin
Richard MacCutchan30-Dec-09 3:16
mveRichard MacCutchan30-Dec-09 3:16 
QuestionWindows Service Pin
S p k 52129-Dec-09 23:09
S p k 52129-Dec-09 23:09 
AnswerRe: Windows Service Pin
Madhu Nair29-Dec-09 23:15
Madhu Nair29-Dec-09 23:15 
GeneralRe: Windows Service Pin
S p k 52130-Dec-09 16:42
S p k 52130-Dec-09 16:42 
QuestionUsing delegates in vc++ Pin
Anu_Bala29-Dec-09 21:49
Anu_Bala29-Dec-09 21:49 
AnswerRe: Using delegates in vc++ Pin
Maxwell Chen29-Dec-09 22:01
Maxwell Chen29-Dec-09 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.