我想寫的是創建一個子進程AC腳本,並根據返回它將打印一個消息,我寫到目前爲止代碼:意外的標記「(」在C腳本
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
int main()
{
pid_t child_pid;
printf ("the main program process ID is %d\n", (int) getpid());
child_pid = fork() ;
if (child_pid==0){
printf("this is the child process,with the id %d\n",(int) child_pid);
}
else
printf("this is the parent process with id %d\n",(int) getpid());
return 0;
}
當我嘗試編譯它,它似乎是OK可是當我在終端上運行它,我得到以下錯誤:?
./lab7.c: line 6: syntax error near unexpected token `('
./lab7.c: line 6: `int main()'"
有人可以幫我告訴我什麼,我做錯了
下一個問題:爲什麼「的主要程序...」字符串獲得兩次印刷? – 2015-04-04 15:07:56
你如何編譯?在我的linux-box這個代碼編譯成功 – 2015-04-04 15:08:01
與你的問題無關,但你應該檢查你的邏輯更接近一點......如果'child_pid'等於零,你有'if'檢查,如果是你打印出來的值'child_pid'。,這意味着孩子將始終打印它有pid零。 – 2015-04-04 15:08:43