Error code in C

433 Views Asked by At

I am having error with my code below

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
        int i, status;
        pid_t child;
        child=fork();
        if(child == 0){
        for(i=0; i<10; i++){
            printf("\tChild PID = %d\n", getpid());
            printf("\tChild PPID = %d\n", getppid());
            sleep(1);
        }
        exit(0);
        }
        else{
        for(i=0; i<10; i++){
            printf("Parent PID = %d\n", getpid());
            printf("Parent PPID = %d\n", getppid());
        }
        }
        waitpid(child, &status, 0);
        return 0;
}

I code in GCC(Unix) , and get the following error :

test.c:27:1: error: expected identifier '(' before '}' token

Can someone suggest me any help? Thanks in advance :)

1

There are 1 best solutions below

5
On

The man page for waitpid() states:

#include <sys/types.h>
#include <sys/wait.h>

Anyway, the error might due to the usage of pid_t which is defined in sys/types.h.

Using -Wall to turn on all compiler warnings would have pointed one to the missing prototype of waitpid().

Update: This assumes Linux.