3.多进程例程:process.c

  1.在nfs目录下创建 process.c文件

Code:
  1. #include<stdio.h>  
  2. #include<fcntl.h>  
  3. #include<stdlib.h>  
  4. #include<unistd.h>  
  5. #include<sys/types.h>  
  6. #include<sys/wait.h>  
  7.   
  8. int main(int argc, char* argv[])  
  9. {  
  10.         int  stat;  
  11.         pid_t child;  
  12.   
  13.         printf("/nTry to create new process./n");  
  14.         child = fork();  
  15.   
  16.         switch(child)  
  17.         {  
  18.                 case -1: //fault  
  19.                         perror("fork./n");  
  20.                         exit(EXIT_FAILURE);  
  21.   
  22.                 case 0: //child process  
  23.                         printf("This is child./n");  
  24.                         printf("/tchild pid is %d/n", getpid());  
  25.                         printf("/tchild ppid is %d/n", getppid());  
  26.                         exit(EXIT_SUCCESS);  
  27.   
  28.                 default// father process  
  29.                         waitpid(child,&stat, 0);  
  30.                         printf("this is parent./n");  
  31.                         printf("/tparent pid is %d/n", getpid());  
  32.                         printf("/t parent ppid is %d/n", getppid());  
  33.                         printf("/t child exited with %d/n",  stat);  
  34.                         exit(EXIT_SUCCESS);  
  35.   
  36.         }  
  37.   
  38.         return 0;  
  39.   
  40. }  

2.创建makefile文件

Code:
  1. EXEC = process  
  2. OBJS = process.o  
  3. SRC  = process.c  
  4.   
  5. CC = arm-linux-gcc  
  6. CFLAGS = -o2 -Wall  
  7. DFLAGS +=  
  8.   
  9. all:$(EXEC)  
  10.   
  11. $(EXEC):$(OBJS)  
  12.         $(CC) $(DFLAGS)  -o $@ $^  
  13.   
  14. %.o:%.c  
  15.         $(CC) $(CFLAGS) -o $@ -c $<  
  16.   
  17. clean:  
  18.         @rm -vf $(EXEC) *.o *~  

3.执行make,进行编译该文件。

Code:
  1. -bash-3.2# make  
  2. arm-linux-gcc -o2 -Wall -o process.o -c process.c  
  3. arm-linux-gcc   -o process process.o  
  4. -bash-3.2#  

4.超级终端上,进行测试。

Code:
  1. /mnt/nfs/arm/project/test3 # ls  
  2. makefile   process    process.c  process.o  
  3. /mnt/nfs/arm/project/test3 # ./process  
  4.   
  5. Try to create new process.  
  6. This is child.  
  7.         child pid is 968  
  8.         child ppid is 967  
  9. this is parent.  
  10.         parent pid is 967  
  11.          parent ppid is 924  
  12.          child exited with 0  
  13. /mnt/nfs/arm/project/test3 #  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值