fork () erro de programa de função executado usando putty [duplicate]

-2
#include<stdio.h>
#include<sys/wait.h>

int main()
{
int p1,p2;
p1=fork();
if(p1==-1)
{
printf(“Error”);
}
else
{
printf(“Parent is %d, Child is %d n”,getppid(),getpid());
}
p2=fork();
printf(“Parent is %d, Child is %d n”,getppid(),getpid());

return 0;
abcd.c: In function ‘main’:
abcd.c:7:4: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
 p1=fork();
    ^
abcd.c:10:1: error: stray ‘2’ in program
 printf(“Error”);
 ^
abcd.c:10:1: error: stray ‘0’ in program
abcd.c:10:1: error: stray ‘4’ in program
abcd.c:10:1: error: stray ‘2’ in program
abcd.c:10:1: error: stray ‘0’ in program
abcd.c:10:1: error: stray ‘5’ in program
abcd.c:10:11: error: ‘Error’ undeclared (first use in this function)
 printf(“Error”);
           ^
abcd.c:10:11: note: each undeclared identifier is reported only once for each function it appears in
abcd.c:14:1: error: stray ‘2’ in program
 printf(“Parent is %d, Child is %d n”,getppid(),getpid());
 ^
abcd.c:14:1: error: stray ‘0’ in program
abcd.c:14:1: error: stray ‘4’ in program
abcd.c:14:11: error: ‘Parent’ undeclared (first use in this function)
 printf(“Parent is %d, Child is %d n”,getppid(),getpid());
           ^
abcd.c:14:18: error: expected ‘)’ before ‘is’
 printf(“Parent is %d, Child is %d n”,getppid(),getpid());
                  ^
abcd.c:14:18: error: stray ‘2’ in program
abcd.c:14:18: error: stray ‘0’ in program
abcd.c:14:18: error: stray ‘5’ in program
abcd.c:17:1: error: stray ‘2’ in program
 printf(“Parent is %d, Child is %d n”,getppid(),getpid());
 ^
abcd.c:17:1: error: stray ‘0’ in program
abcd.c:17:1: error: stray ‘4’ in program
abcd.c:17:18: error: expected ‘)’ before ‘is’
 printf(“Parent is %d, Child is %d n”,getppid(),getpid());
                  ^
abcd.c:17:18: error: stray ‘2’ in program
abcd.c:17:18: error: stray ‘0’ in program
abcd.c:17:18: error: stray ‘5’ in program
    
por Dushyant Chawda 20.07.2017 / 12:24

1 resposta

2

Você precisa adicionar

#include <unistd.h>

para usar fork , alterar todos os para " e adicionar um } final no final:

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

int main()
{
int p1,p2;
p1=fork();
if(p1==-1)
{
printf("Error");
}
else
{
printf("Parent is %d, Child is %d n",getppid(),getpid());
}
p2=fork();
printf("Parent is %d, Child is %d n",getppid(),getpid());

return 0;
}
    
por Florian Diesch 20.07.2017 / 12:38