
在Linux系统中编写 C程序,创建一个子进程。父进程和子进程交替运行。父进程先显示一次“Current time:”, 20
2个回答
2012-12-19
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int status;
pid_t pid;
pid_t subpid;
pid=fork();
if(pid > 0) {
while(1) {
fprintf(stdout, "parent process working ... \n");
sleep(1);
}
}else if(pid == 0) {
while(1) {
subpid=getpid();
sleep(1);
fprintf(stdout, "child process working ... \n");
}
}else if(pid == -1) {
fprintf(stderr, "%s: create subprocess failed. pid=%d\n",
__func__, getpid());
}
}
自己编译试试?
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int status;
pid_t pid;
pid_t subpid;
pid=fork();
if(pid > 0) {
while(1) {
fprintf(stdout, "parent process working ... \n");
sleep(1);
}
}else if(pid == 0) {
while(1) {
subpid=getpid();
sleep(1);
fprintf(stdout, "child process working ... \n");
}
}else if(pid == -1) {
fprintf(stderr, "%s: create subprocess failed. pid=%d\n",
__func__, getpid());
}
}
自己编译试试?
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询