I'm trying to write a short code that will take in a string of text, and tell me how many vowels are in it, with the information being passed between a parent and child process using pipes (I am only testing for "a" at the moment until I get that working, then I'll finish the others).

I keep getting segmentation fault (Core Dumped).

Would you mind checking out my code and pointing me in the right direction?

Thank you in advance for any and all assistance.

#include <stdio.h>

#define READ 0
#define WRITE 1

char* phrase;

main ()
{
int fd [2], vCount;
char message[80];
int i, j, k = 0;
char c;
char vowels[80];

printf ("Enter a phrase up to a maximum of 80 characters.\n");
//phrase = gets (message);

while (i < 80 && (c = getchar()) != '\n')
{
 message[i] = c; 
 switch(c)
 {
 case 'a':
 vowels[j] = c;
 j++;
 }
i++;
}
pipe (fd);
if (fork () == 0)
 {
 close(fd[READ]);
 write (fd[WRITE], vowels, (j + 1));
 close (fd[WRITE]);
 }

else
 {
 close (fd[WRITE]);
 vCount = read (fd[READ], vowels, 80);
 printf ("There are %d vowels. They are: %s\n", vCount, vowels);
 close (fd[READ]);
 }
}

Recommended Answers

All 7 Replies

your "i" and "j" are uninitialized indexes to a finite array.

when you start the program, they're probably eleventysevenbillion.

whereas you probably want them to be zero

:P

No, sir. On line 11, variables int i, j,k are all set to zero.

Oh I see the mistake! I guess in C you cannot initialize multiple variables the value of zero at the same time?

I changed line 11 from int i, j, k = 0; to int i = 0; int k = 0 and it works!

Thank you!!

- Jim

One last question:

If the input is short (3 chars or so) the output is fine. However, if the output grows larger (20 or so chars) then after the listing of the 20 "a's", there are some odd symbols such as a heart, a musical note, and some others.

Where do these symbols come from?

Thank you


- Jim

standard ascii chars as you know them run from decimal values 0-127... the alpha, numerics, and traditional symbols.

extended ascii codes run from decimal values 128-255, and include all sorts of icons and squiggly bits... problem is they can differ from platform to platform.

do a search for "extended ascii codes"

Firstly in C++ and in C, I know these only right now, you intialize the variables in this way in line.

int x = 0, y = 0, z = 2;

In the following way, only z will be initialized.

int x, y, z = 0;

or

int x, z = 0, y;

Now the problem of yours with output.

Actually the processor is not doing work in the linear way. You get process1, then the process2, then process3, for example. Now suppose if process2 is busy, then the processor will take process1 and process3. Then may be process2 or may not depending on its status of completeness. I think you better search on TIME SLICING.

So you get the characters in the parent process, you send it to pipe and the child gets it from the pipe. Now before the child show some output on the screen (before child goes to processor), parent again goes to processor and gets processed because may be child needed sometime to check whether the character it got is a vowel or not.

So it is possible for a child and parent to execute anytime.
Take the following code for example;

#include <stdio.h>

void doit()
{
	if (fork() == 0)
	{
		fork();
		printf ("hello_child\n");
		return;
	}
	
	return;
}

int main()
{
	doit();
	printf ("hello_parent\n");
	return 0;
}

The output can be
samran@ubuntu:~/Desktop$ ./prog
hello_child
hello_parent
hello_parent
hello_child
hello_parent
samran@ubuntu:~/Desktop$

or can be
samran@ubuntu:~/Desktop$ ./prog
hello_child
hello_parent
hello_child
hello_parent
hello_parent
samran@ubuntu:~/Desktop$

Now I would tell, what I did to solve this problem. I asked the parent to SLEEP for sometime, till the child does its work. After child has completed his work, the parent wakes up and does its work and sleep again till the child finishes its work.

Check this thread out. The third post. First post states the problem.
http://www.daniweb.com/forums/thread185378.html


Hope you'll get your code right now.

>Where do these symbols come from?
Like the Origin of Life, some call it a mystery; others call it a miracle.

printf ("There are %d vowels. They are: %s\n", vCount, vowels);

vowels is never terminated with the '\0' which would have made it a string. Forcing printf() to keep displaying beyond the range of vowels or until by luck it hits a '\0' left in memory.

commented: Well said +29
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.