linux process study (very easy to know)

本文介绍了一个简单的多进程编程示例,展示了如何使用C语言创建父进程与子进程,并通过子进程进行数值比较运算。该示例包括两个源文件:一个作为父进程负责创建子进程,另一个作为子进程执行具体的数值比较任务。
Today, I want to learn how to write a program using the multi-process! now, I will show a very easy Ex for you to study! here are the codes! //parent process "process.c" #include #include void main(int argc, char *argv[]){ int pid; pid = fork(); printf("pid =%d\n", pid); if(pid < 0){ printf("Error:Cant creat a new process!\n"); } else if(pid == 0){ execlp("/home/daniel/Desktop/hw1/compare",NULL,NULL); } else{ wait(NULL); printf("Succeed:Child Complete!\n"); } return ; } //child process "compare.c" #include void main(void){ /*compare 3 and 5, which one is bigger*/ printf("You are running the child process!\n"); int i=3,j=5; printf("i=%d ,j=%d.\n",i,j); if(i > j){ printf("OK, %d > %d!\n",i,j); } else{ printf("OK, %d < %d!\n",i,j); } return ; } When you type the code into your vim, save it and using the gcc to compile it ! for Ex: gcc -W compare.c -o compare after you compile the program , you will find you can run the process ,and it can call the campare program to run.

转载于:https://www.cnblogs.com/lzhiq/archive/2010/10/31/1865845.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值