12

I'm trying to run my first "process" program, but I get the following error :

./fork.c: line 4: syntax error near unexpected token `('
./fork.c: line 4: `int main()'

I'm pretty sure that the code is correct:

 #include <sys/types.h>
 #include <stdio.h>
 int main() {
     pid_t pid;
     printf("Hello fork()\n");
     switch(pid=fork())  {
         case -1: printf("Error by fork().....\n"); exit(0);
         case 0: printf("I'm the child process \n"); break;
         default: printf("I'm the dad \n"); break;
     }
  exit(0);
}

What is wrong?

Renan
  • 16,976
  • 8
  • 69
  • 88
Engine
  • 261
  • 2
  • 5
  • 16
    Just for the sake of your own sanity later, put a "break;" on the "case -1:" line. You'll thank yourself for it later. Also, have the child process call _exit(0), and the parent call exit(0). –  Aug 13 '12 at 01:22
  • 4
    @BruceEdiger Why the need for `_exit`? What's wrong with doing any cleanup that's been registered? – OrangeDog Aug 13 '12 at 12:49
  • 5
    exit(0) will flush stdout and stderr. _exit(0) will not. You can end up with double outputs if there's some bytes on stdout when your program does the fork(), and the child calls exit(0). Because you're learning how fork() works why confuse yourself? –  Aug 13 '12 at 14:38
  • @BruceEdiger learning how `fork()` works includes learning that it can copy buffered output. Being a reasonably complicated system call, some confusion is probably necessary in the learning process. – OrangeDog Aug 14 '12 at 09:36

2 Answers2

68

You can't just run ./fork.c. It's not a program; it's the source for a program. Using ./ assumes that the file is a script (which it isn't) and treats it accordingly.

However, as noted in another answer, there are compilers (like Tiny C Compiler) that can execute C code without explicitly compiling it.

Since it's a C program, you have to compile the program. Try cc -o fork fork.c then ./fork; it worked here.

wjandrea
  • 658
  • 7
  • 19
Renan
  • 16,976
  • 8
  • 69
  • 88
23

That's not a program, that's the source code for a program.

C is a compiled language, meaning it must be "compiled" into machine-readable instructions before you can run it. As you are using C, the "C Compiler" (cc) can do this.

cc -o fork for.c   # compile the code
chmod +x fork      # ensure it it executable
./fork             # run the compiled program

As you move on to more complicated programs, using multiple source files and external libraries, you'll likely move on to using the "GNU Compiler Collection" (gcc) and make to describe how to turn the source code into a working executable.

This question has various information on the difference between scripts (as you are attempting to treat your source code) and compiled programs.

OrangeDog
  • 871
  • 6
  • 14
  • If "compiler" is part of the name of the tool, I would call that reasonably explicit. – OrangeDog Aug 13 '12 at 10:24
  • Just a note that the tools I named are not the only ones available, but are likely to be available by default on a GNU/Linux system and are widely used. – OrangeDog Aug 13 '12 at 10:33
  • 4
    Regarding that "must" word, `tcc` ([Tiny C Compiler](http://bellard.org/tcc/)) can execute C code without explicitly compiling it. http://pastebin.com/5FZiMpEn (Edited comment by re-adding it. Sorry for messing the order.) – manatwork Aug 13 '12 at 10:36