跑滴滴买什么电动车:求: 用c语言实现进程控制的源码

来源:百度文库 编辑:高校问答 时间:2024/05/02 02:31:35
有没有用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",buf2,getpid(),i);
}
}
else /*创建的是子进程*/
{
while(1){
read(fds1[0],buf3,50); /*从管道1中读取信息到buf3中*/
j=(int)buf3[0]%10;
printf("%s,%d sleeping %d/n",buf3,getpid(),j);
sleep(j); /*睡1~10中随机的一个秒数*/
sprintf(buf4,"%d Parent process print!" , rand()); /*把字符串child process print!写道缓冲区4中,rand是随机数产生器*/
write(fds2[1],buf4,50); /*把buf4中的信息写道管道2中*/

}
}
}

有 很麻烦的啦 才10分我才不干

createthread(...)等等有一系列windowsAPI
你愿意用吗?
pv操作是利用信号量等的变量实现的,也有专门的api函数用于操作信号量等