求: 用c语言实现进程控制的源码

来源:百度知道 编辑:UC知道 时间:2024/05/30 05:24:56
有没有用c语言实现进程控制的源码?
只要实现进程的p v 操作即可!

/******************************************/
/* pipe.c 程序清单 */
/******************************************/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include<stdlib.h>

main(){
pid_t pid; /*进程名为pid*/
int fds1[2],fds2[2] ,i=0, j=0;
char buf1[50],buf2[50],buf3[50],buf4[50];/*创建四个缓冲区*/

pipe(fds1); /*创建管道1*/
pipe(fds2); /*创建管道2*/

if((pid = fork()) < 0) /*进程创建失败,调用退出函数*/
{
printf("\n *****fork error \n");
exit(1);
}
else if(pid == 0) /*创建的是父进程*/
{
while(1){
sprintf(buf1,"%d child process print!" , rand()); /*把字符串child process print!写道缓冲区1 中,rand是随机数产生器*/
write(fds1[1],buf1,50); /*把buf1中的信息写道管道1中*/
read(fds2[0],buf2,50); /*从管道2中读取信息到buf2中*/
i=(int)buf2[0]%10;
sleep(i); /*睡1~10中随机的一个秒数*/
printf("%s,%d sleeping %d/n