#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t pid1,pid2;
pid1=fork();
if(pid1==0)//进程1运行
{
printf("b\n");
fflush(stdout);
}
else if(pid1>0)//此时父进程在运行
{
pid2=fork();
if(pid2>0)
{
printf("a\n");
fflush(stdout);
}
else if(pid2==0)
{
printf("c\n");
fflush(stdout);
}
else
{
printf("进程2创建失败\n");
}
}
else
{
printf("进程1创建失败\n");
return 1;
}
return 0;
}
由于fork函数会复制一份父进程的内容形成一个子进程,对于父进程fork会返回子进程的pid(pid>0),
对于子进程fork会返回0,若返回了pid<0则子进程创建失败.
但是对于子进程和父进程的运行顺序是不确定的,一般取决于操作系统的调度算法,所以上面代码输出的字符顺序是不定的,但是实际上父进程和子进程应该是并发执行的。
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t pid1,pid2;
pid1=fork();
if(pid1==0)//进程1运行
{
printf("I am the first child process. my pid:%d\n",getpid());
}
else if(pid1>0)//此时父进程在运行
{
pid2=fork();
if(pid2>0)
{
printf("I am the parent process. my pid:%d\n",getpid());
}
else if(pid2==0)
{
printf("I am the second child process. my pid:%d\n",getpid());
}
else
{
printf("进程2创建失败\n");
return 1;
}
}
else
{
printf("进程1创建失败\n");
return 1;
}
return 0;
}
依旧同上原因会出现执行顺序不一的问题
lockf(fc,lock_state,len) fc是文件描述符 lock_state是开锁和解锁 len是锁定的字节数
当lockf(1,1,0)时,此时资源被该进程锁定,其他进程无法抢占,需要等到该进程解锁资源后,其他进程才有机会抢占。
如下:当父进程通过fork申请新的子进程,父进程会锁定资源输出
"I am the parent process."
直到将资源解锁,同理,当子进程通过lockf锁定资源,也会输出后解锁。实现了进程的互斥。
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t pid1,pid2;
pid1=fork();
if(pid1==0)//进程1运行
{
lockf(1,1,0);
printf("I am the first child process. my pid:%d\n",getpid());
lockf(1,0,0);
}
else if(pid1>0)//此时父进程在运行
{
pid2=fork();
if(pid2>0)
{
lockf(1,1,0);
printf("I am the parent process. my pid:%d\n",getpid());
lockf(1,0,0);
}
else if(pid2==0)
{
lockf(1,1,0);
printf("I am the second child process. my pid:%d\n",getpid());
lockf(1,0,0);
}
else
{
printf("进程2创建失败\n");
return 1;
}
}
else
{
printf("进程1创建失败\n");
return 1;
}
return 0;
}
实验3.1
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<wait.h>
#include<stdlib.h>
int wait_signal=1;
void wait_signal_switch()
{
wait_signal=0;
}
int main()
{
pid_t p1,p2;
wait_signal=1;
signal(2,wait_signal_switch);
p1=fork();
while(p1==-1);
if(p1>0)
{
p2=fork();
if(p2>0)
{
while(p2==-1);
wait_signal=1;
while(wait_signal==1);
//sleep(1)//如果只输出了一个进程,则可能是父进程太快执行完毕导致子进程没时间进行
kill(p1,40);
kill(p2,41);
wait(NULL);
wait(NULL);//等待两个子进程被killed
printf("Parent Process is killed\n");
}
else
{
wait_signal=1;
signal(41,wait_signal_switch);
while(wait_signal==1);
printf("Child Process2 is killed by Parent\n");
exit(0);
}
}
else
{
wait_signal=1;
signal(40,wait_signal_switch);
while(wait_signal==1);
printf("Child Process1 is killed by Parent\n");
exit(0);
}
}
如果在fork()生成子进程后在通过signal(2,func())获取信号执行,那么子进程会运行不完整
要解决这个问题:需要后面打进程需要加上signal(2,SIG_IGN);否则当按下Del键时将终止进程导致子进程运行不完整。
实验4-1
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<wait.h>
#include<stdlib.h>
int main()
{
char temp[100];
pid_t p1,p2;
int fd[2];//1是写入端 0是读取端
pipe(fd);
p1=fork();
if(p1>0)
{
p2=fork();
if(p2>0)
{
wait(0);
read(fd[0],temp,100);
printf("%s\n",temp);
wait(0);
read(fd[0],temp,100);
printf("%s\n",temp);
}
else if(p2==0)
{
char* childtemp="Child2 is sending message!";
lockf(fd[1],1,0);
write(fd[1],childtemp,100);
lockf(fd[1],0,0);
exit(0);
}
}
else if(p1==0)
{
char* childtemp="Child1 is sending message!";
lockf(fd[1],1,0);
write(fd[1],childtemp,100);
lockf(fd[1],0,0);
exit(0);
}
}
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<wait.h>
#include<stdlib.h>
int main()
{
char temp[100];
pid_t p1,p2;
int fd[2];//1是写入端 0是读取端
pipe(fd);
p1=fork();
if(p1>0)
{
p2=fork();
if(p2>0)
{
waitpid(p1,NULL,0);
read(fd[0],temp,100);
printf("%s\n",temp);
waitpid(p2,NULL,0);
read(fd[0],temp,100);
printf("%s\n",temp);
}
else if(p2==0)
{
sleep(0.1);
char* childtemp="Child2 is sending message!";
lockf(fd[1],1,0);
write(fd[1],childtemp,100);
lockf(fd[1],0,0);
exit(0);
}
}
else if(p1==0)
{
char* childtemp="Child1 is sending message!";
lockf(fd[1],1,0);
write(fd[1],childtemp,100);
lockf(fd[1],0,0);
exit(0);
}
}