fork1.c

来自「嵌入式Linux程序设计与应用案例 电子书源码 中国电力出版社」· C语言 代码 · 共 55 行

C
55
字号
/**************// name : fork1.c// author : pyy// date : 2007-11-22***************/#include<unistd.h>#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){    pid_t child=2;    int ans;    char str0[240],str1[60],str2[60],str3[60],str4[60];    printf("是否要执行复制进程? (1 yes 2 no):");    scanf("%d",&ans);    if(ans == 1)    {        child = fork(); //copy process        if(child == -1) { perror("fork error\n"); exit(1);}    }        strcpy(str1,"one :what a beautiful view!\n");    strcpy(str2,"two : Three buildings are worth seeing.\n");    strcpy(str3,"three: I'd like to look around the city.\n");    strcpy(str4,"four: Would you recommend some famous \n");    strcpy(str0,"");   /* child process*/    if(child == 0)    {        puts(" child process.....:");        printf("\t child pid = %d\n",getpid());        printf("\t child ppid = %d\n",getppid());        strcat(str0,str1);        strcat(str0,str2);        printf("%s",str0);        printf(" the length of string is %d\n ",strlen(str0));        exit(0);    }    else    {        puts(" father process.....:");        printf("\t father pid = %d\n",getpid());        printf("\t father ppid = %d\n",getppid());        strcat(str0,str3);        strcat(str0,str4);        printf("%s",str0);        printf(" the length of string is %d\n ",strlen(str0));        exit(0);           }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?