c - Error running this fork code in my eclipse, and also have some concept confusion around this code -
so simple c code on fork professor gave us, i'm trying output code, when tried run in eclipse following error:
info: internal builder used build gcc -o0 -g3 -wall -c -fmessage-length=0 -o forkdemo1.o "..\\forkdemo1.c" ..\forkdemo1.c: in function 'main': ..\forkdemo1.c:18:1: warning: implicit declaration of function 'fork' [-wimplicit-function-declaration] if( (pid = fork()) = -1) { ^ ..\forkdemo1.c:18:20: error: lvalue required left operand of assignment if( (pid = fork()) = -1) {
but basic confusion around code is: pid_t pid's value? 11 or 10, creating child parent?
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int global = 10; int main(int argc, char* argv[]) { int local = 0; pid_t pid; printf("parent process: (before fork())"); printf(" local = %d, global = %d \n", local, global); if( (pid = fork()) = -1) { perror("fork"); exit(1); } else if (0 == pid) { /* child executes branch */ printf("after fork in child:"); local++; global++; printf("local = %d, global = %d\n", local, global); } else { /* parent execute branch */ sleep(2); /* sleep long enough child's output appear */ } /* both process excute print statement */ printf("pid = %d. local = %d, global = %d\n", getpid(), local, global); return 0; }
change this:
if( (pid = fork()) = -1) {
to this:
if( (pid = fork()) == -1) {
as warning implicit declaration of fork
, might happen if work in windows os , using gcc
toolchain mingw
, providing unistd.h
not implementing fork
. in such case should switch environment. ideally - linux, can use gcc
cygwin
, providing fork
Comments
Post a Comment