Here is my code it is written on Xcode on mac. I keep getting the 'Expected Expression' error message and Expected ')' message. Pls help. Thanks! It occurs in the line where it starts
For (i = 0; i <3; i++;);
#include<stdio.h>
#include <unistd.h>
int main() {
int i;
For (i = 0; i < 3; i++;);
fork();
printf("Hello World\n");
return 0;
}
For (i = 0; i < 3; i++;);
1. Case matters. It's "for", not "For". for (i = 0; i < 3; i++;);
2. The structure of for is for(initialization; condition; increment). Only two semicolons between the parentheses. for (i = 0; i < 3; i++);
3. That semicolon at the end of the parentheses say "this loop is empty". Nothing that follows the loop is inside it. I don't know what you but I'll take a guess.
1 2 3 4
for (i = 0; i < 3; i++){
fork();
printf("Hello World\n");
}
On success, the PID of the child process is returned in the parent, and 0 is returned in the child.
The common usage pattern is
1 2 3 4 5
if (fork()){
//Code that the child must execute.
}else{
//Code that the parent must execute.
}
Finally, this isn't an issue necessarily, but note that your code will cause up to eight processes to run simultaneously, and fifteen processes to run in total.
#include<stdio.h>
#include <unistd.h>
int main() {
int i, p;
for (i = 0; i < 3; i++) {
p=fork();
printf("Hello World. level %i child %i \n",i,p);
}
return 0;
}