fork() and pipe() unknown issues (UNIX/C)

I am getting a 141 error code, on this program which is supposed to co

Page 1 of 1

2 Replies - 2633 Views - Last Post: 24 March 2009 - 09:09 PM Rate Topic: -----

#1 endomlic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-March 09

fork() and pipe() unknown issues (UNIX/C)

Post icon  Posted 24 March 2009 - 02:59 PM

I'm having trouble with this program, when I run it I get an error that says: "failed with return code 141 and message." -- it doesn't give me an error message.

The code is supposed to count integers from 1 to 10000 and if its divisible by 5 then the parent counts how many times this happens, if it's not the number is sent via a pipe to the child to see if the number is divisible by 3.
At the end of the program the child is supposed to send its results to the parent which is supposed to print the counts of both the parent and child and print both process ids. I can only use one pipe.

I've been trying to figure out the problem with this for a while.
Here is the code. btw I'm using gcc.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
	//our file descriptor
	int fd[2];
	//parent count, and child count
	int pcount = 0, ccount = 0,  x = 0, i = 0;
	//if childread is 0 then the child reads, other wise it writes
	int childread = 0;
	int temp;
	pid_t childpid;
	int *buffer;	

	ssize_t nread;



	int pid;

	//create a pipe
	pipe(fd);

	//fork the process
	pid = fork();

	// parent process
	if(pid > 0) 
	{
		if (childread != 0)
		{
			//close fd for writing to read what the child has
			if(close(fd[1]) == -1)
				printf("Closing error.\n");
			nread = read(fd[0], &temp, sizeof(int));
			//ccount = *buffer;
				
			printf("Parent count: %d\n", pcount);
			printf("Child count: %d\n", temp);

			//wait for child to exit
			waitpid(childpid);
			exit(1);
		}
		else
		{
			//close fd for reading
			if(close(fd[0]) == -1)
				printf("Closing error.\n");
			//start counting
			for(i = 0; i < 10000; i++)
			{
				//if multiple of 5 increment parent count			
				if( (i % 5) == 0 )
				{
					pcount++;
				}
				else
				//write i to pipe for child to process
				{	
									
					write(fd[1], &i, sizeof(int));
	
					wait();			
				}
			}
			//the child is now going to send its count to parent
			childread = 1;
		}
	}
	else if(pid == 0) //child process
	{
		if (childread != 0)
		{			
			//close file descriptor for reading
			if(close(fd[0]) == -1)
				printf("Closing error.\n");

			//*buffer = ccount;

			//send result to parent and exit
			write(fd[1], &ccount, sizeof(int)); //change to &buffer
			

			//print the parent id
			printf("\nThe parent pid is: %d\n", getppid());
			//print the child pid
			printf("The child pid is: %d\n", getpid());
			
			childpid = getpid();
			
			//exit the child process 
			exit(1);
		}
		else
		{
			//close file descriptor for writing
			if(close(fd[1]) == -1)
				printf("Closing error.\n"); exit(1);

			//read the integer passed from the parent
			nread = read(fd[0], &x, sizeof(int));
			
			//x = *buffer;

			//if the number is a multiple of 3 increment the child counter
			if( (x % 3) == 0)
			{
				ccount++;			
			}	
		}
	}
	else
	{
		printf("Error forking.\n");
		exit(1);
	}

	//printf("\nParent id: %d\nChild id: %d\nMultiples of 5 counted: %d\nMultiples of 3 counted: %d", pid, cid, pcount, ccount);
close(fd[0]);
close(fd[1]);
	return 0;
}
 

This post has been edited by endomlic: 24 March 2009 - 03:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: fork() and pipe() unknown issues (UNIX/C)

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: fork() and pipe() unknown issues (UNIX/C)

Posted 24 March 2009 - 08:49 PM

Welcome to DIC, endomlic. I don't know the answer to that.
But I'm sure if you stick around and check in a few days from now, somebody'll have given you an answer. :)
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5314
  • View blog
  • Posts: 27,220
  • Joined: 10-May 07

Re: fork() and pipe() unknown issues (UNIX/C)

Posted 24 March 2009 - 09:09 PM

1st, I've changed your else structure to the following :

	else
	{
		printf("Error forking.\n");
		int i=0;
		for(i=0;i<10;i++) {
				if(fd[i]) {
						printf("Closing fd %d\n",i);
						close(fd[i]);
				}
		}
		//exit(1);
		exit(EXIT_FAILURE);
	}



& that performs as such ....

Quote

/home/code/c >$./fork
Error forking.
Closing fd 0
Closing fd 1
Closing fd 2
Closing fd 3
Closing fd 4


I'm still looking at it... :D :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1