Member Avatar for muaazab

I am trying to call the fork() system call. It works quite fine until I don't use printf method in the code. My code is as follows.

Platform: Linux Language: Pure C

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

pid_t fork(void)

main()
{
int pid;
pid = fork();
if(pid == 0) 
{
// printf("This is child \n");
}
else {
// printf("This is parent \n");
}
}

As I said earlier it work quite fine but removing comment sign from two printf methods creates lots of problems.

Any idea why fork() method not allowing to use printf? What is the solution?

Thanks!

Recommended Answers

All 2 Replies

Member Avatar for Mouche

That code doesn't compile for me. You're missing a few things:

1) You're missing a semicolon at the end of the prototype for forking. You don't need that prototype at all, so get rid of it.

2) Your main() header should look like this "int main(int argc, char* argv[])". It doesn't even have a return type right now.

3) Once you have the return type as int, you need to have a return at the end of main() like this "return 0;".

Once you have those issues fixed, your code should show nothing (but still run) when the printf statements are commented out, and you should get those two print messages in a row if you uncomment the printf functions out.

Member Avatar for muaazab

ok thank you LaMouche, my problem solved.
The problem was that I was using wrong quotation marks round the printf statement. This was due to the program "OpenOffice.org".

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.